Thursday, May 17, 2012

How to Create New Sharepoint List Programmatically

Creation of new list includes following things.
- Create new list
- Create new field to newly created list
- Create new field which will set default value when new item created
- Show newly created field in view list of list
- Create lookup column to newly created list
- If list is already exist then create column to it


Here is the steps to create product list and columns in it.
(1) First, get list with name “Product” and check whether list is not null.

SPList ListProduct = web.Lists.TryGetList(“Product”);
if (ListProduct == null)
{

(2) if “Product” list is null then following step will create new list for sharepoint
web.Lists.Add(“Product”, "Product List Instance", SPListTemplateType.GenericList);
web.Update();

(3) After Creation of list “Product”, again, Get newly create list “Product”
SPList ProductList = web.Lists[“Product”];

(4) if you want to set product list to be appear on quick launch bar of sharepoint site then make it true/false accordingly
ProductList.OnQuickLaunch = false;

(5) Create field with name “Active”, if you want to add default value for field this way you can add default value

SPFieldBoolean fldActive = (SPFieldBoolean)ProductList.Fields.CreateNewField(SPFieldType.Boolean.ToString(), "Active");
fldActive.DefaultValue = "1";
ProductList.Fields.Add(fldActive);

(6) Simply, if you want to add field and there is no need to put any default value on the time of creation of new item you can add field by following way.
ProductList.Fields.Add("Description", SPFieldType.Note, false);

(7) After adding fields to List, update list.
ProductList.Update();

(8) After creation of list, then adding new fields to list, if you want to add newly added fields to view list of list then call following function.
CommonMethods.SetFieldInViewField(ProductList, "Active");

CommonMethods.SetFieldInViewField(ProductList, "Description");
(9) from above code we have added those column which are simple to add, but if you want to add any lookup column to list then use following step.
- Get list which you want to use as reference to lookup column
SPList ListCategory = web.Lists.TryGetList(“Category”);

- Call following function which will add new column named “Category” to “Product” list and adds reference of list “Category” with its field “Title”
CommonMethods.CreateLookupColumn(ProductList , ListCategory, "Category", "Title");

(10) Again, after creation of field, just put “Category” field to view list of list

//========== put field in view fields ==========//
CommonMethods.SetFieldInViewField(ProductList , "Category");
}

- To Set field in view list of list, use following function
public static void SetFieldInViewField(SPList ListName, string _Str_Column_Name)
{
SPView oView = ListName.Views["All Items"];
SPViewFieldCollection collViewFields = oView.ViewFields;
collViewFields.Add(_Str_Column_Name);
oView.Update();
}

- To add lookup column, use following function
public static void CreateLookupColumn(SPList Lst_Source_List, SPList Lst_Lookup_List, string _Str_Target_Column_Name, string _Str_Lookup_Column_Name)
{
SPList ListTest = Lst_Source_List;
ListTest.Fields.AddLookup(_Str_Target_Column_Name, Lst_Lookup_List.ID, false);
SPFieldLookup lkp = (SPFieldLookup)ListTest.Fields[_Str_Target_Column_Name];
lkp.LookupField = Lst_Lookup_List.Fields[_Str_Lookup_Column_Name].InternalName;
lkp.Update();
}

- To create new column when list is exist follow steps given below.
(1) SPList ListProduct = web.Lists.TryGetList(“Product”);
(2) if(ListProduct != null)
{
- Check whether the column with name “SortOrder” is not exist then add new column
if (!ListProduct .Fields.ContainsFieldWithStaticName("SortOrder"))
{
- Add new column and update list
ListProduct .Fields.Add("SortOrder", SPFieldType.Number, false);
ListProduct .Update();
- Add newly created column to view list of the list

//========== put field in view fields ==========//
CommonMethods.SetFieldInViewField(ListProduct , "SortOrder");
}
}

so, that’s it for all. 

No comments:

Post a Comment