Share via


Sharepoint 2007 (MOSS) SP List operations using Object model (progrmatically)

Following are few of the things that I used using Sharepoint 2007 (MOSS) object model, so if you want to programmatically create a list, delete items from the list, etc.

Deleting all items inside List:

SPWeb web = SPContext.Current.Web;

SPList myList = web.Lists ["List_Name"];

SPListItemCollection myListColl = web.Lists["List_Name "].Items;

web.AllowUnsafeUpdates = true;

 

for (int i = myListColl.Count - 1; i >= 0; i--)

{

myListColl [i].Delete();

}web.Update();

 

Creating a Sharepoint List: (I am just covering one type you can try other types)

SPWeb web = SPContext.Current.Web;

web.AllowUnsafeUpdates = true;

// Creating a Sharepoint List and adding Sharepoint Fields SPField to it

web.Lists.Add("AllUserWebpart_SiteUserinfo", "For User of the site", SPListTemplateType.GenericList);

web.Lists["AllUserWebpart_SiteUserinfo"].Fields.Add("Job Title", SPFieldType.Text, false);

web.Lists["AllUserWebpart_SiteUserinfo"].Fields.Add("Department", SPFieldType.Text , false);

web.Lists["AllUserWebpart_SiteUserinfo"].Fields.Add("Phone", SPFieldType.Text, false);

userInfoList = web.Lists["AllUserWebpart_SiteUserinfo"];

//Following code allows you to change the Title of the TITLE field which is added by default whenever you create a //Sharepoint List

SPField TitleField = userInfoList.Fields["Title"];

TitleField.Title = "Name";

TitleField.Update();

//Adding the fields to Default view (If you are adding your own view change the name of the view instead of "All Items"

SPView view = userInfoList.Views["All Items"];

SPViewFieldCollection viewFields = view.ViewFields;

viewFields.Add("Job Title");

viewFields.Add("Department");

viewFields.Add("Phone");

view.Update();

web.Update();

Comments