Search This Blog

Thursday, September 18, 2014

Creating the List Programmatically.

In the SharePoint sometimes we need to create a List Programmatically. When we are creating the List programmatically we need to define the List Template Types.
The List Templates Types are available in the SharePoint. For that We need to use this Object i.e. SpListTemplateType .

Out of the Box list Templates are :-
Name of the Out of the box list Templates
BaseType(Object)                                  
Events
GenericList
Contacts
GenericList
Tasks
GenericList
PictureLibrary
DocumentLibrary
DiscussionBoard
GenericList
Survey
Survey
DocumentLibrary
DocumentLibrary
Links
GenericList
Document Library
DocumentLibrary
Announcements
GenericList
GenericList
GenericList
WorkflowHistory
GenericList
DataConnectionLibrary
DocumentLibrary
DataSources
DocumentLibrary
GanttTasks
GenericList
CustomGrid
GenericList
NoCodeWorkflows
DocumentLibrary
XMLForm
DocumentLibrary
WebPageLibrary
DocumentLibrary
WorkflowProcess
GenericList
Form Library
DocumentLibrary
wiki
DocumentLibrary
Wiki Page Library
DocumentLibrary
Picture Library
DocumentLibrary
Picture
DocumentLibrary
Contacts
GenericList
Calendar
GenericList
Discussion Board
GenericList
Project Tasks
GenericList
Issue Tracking
Issue
Custom List
GenericList
Custom List in Datasheet View
GenericList
Survey
Survey
Custom Workflow Process
GenericList
Languages and Translators
GenericList
Translation Management Library
DocumentLibrary
DataSources
DocumentLibrary
Data Connection Library
DocumentLibrary
Workflow History
GenericList
No Code Workflows
DocumentLibrary

To Create List Programmatically the below piece of the code will helpful.

private static void createList()
      {
          try
          {
              SPSecurity.RunWithElevatedPrivileges(delegate()
              {
                   //here i am accessing the Site
                  using (SPSite mysite = new SPSite(“http://sp2010:1234”))
                  {
                      //I am opening the my Site
                      using (SPWeb myweb = mysite.OpenWeb())
                      {
 //Using the SPListCollection Object I am Geting the all Lists from the Site
                          SPListCollection lists = myweb.Lists;
                          //create new Generic list called "Employee Registration"
                          lists.Add("Employee Registration""Tracking the All the Employees",SPListTemplateType.GenericList);
 //Using the SPList Object I am getting the Particular List
                          SPList createdlist = myweb.Lists["Employee Registration"];

           //create the column type single line text as a new column called "EmployeeFirstName"
                          createdlist.Fields.Add("EmployeeFirstName"SPFieldType.Text,true);

                 //create the column type Multi line text as a new column called "Employee Address"
                          createdlist.Fields.Add("Employee Address"SPFieldType.Note,true);

                          //create lookup type new column called "States"
                          //Here I am going to get the information from the "Title"
                          //column of a list called "States"
                           
                          SPList lookupList = myweb.Lists["States"];
                          // Creating the Lookup type column
                          createdlist.Fields.AddLookup("Employee State", lookupList.ID,false);


                          SPFieldLookup lookup = (SPFieldLookup)createdlist.Fields["Employee State"];
                          lookup.LookupField = lookupList.Fields["Title"].InternalName;
                          lookup.Update();

                          // get the newly added choice field instance

                          createdlist.Fields.Add("Gender"SPFieldType.Choice, false);

                          SPFieldChoice chFldGender = (SPFieldChoice)createdlist.Fields["Gender"];

                          // set field format type i.e. radio / dropdown
                          chFldGender.EditFormat = SPChoiceFormatType.RadioButtons;

                          // set the choice strings and update the field
                          chFldGender.Choices.Add("Male");
                          chFldGender.Choices.Add("Female");
                          chFldGender.Update();


                          createdlist.Fields.Add("Department"SPFieldType.Choice, false);
                          createdlist.Update();
                          SPFieldChoice objChoiceCol = (SPFieldChoice)createdlist.Fields["Department"];
                          string[] strdata = new string[2];
                          strdata[0] = "IT";
                          strdata[1] = "Marketing";
                          objChoiceCol.Choices.Add(strdata[0]);
                          objChoiceCol.Choices.Add(strdata[1]);
                          objChoiceCol.Update();

                          //making new column visible in default view
                          SPView view = createdlist.DefaultView;
                          view.ViewFields.Add("EmployeeFirstName");
                          view.ViewFields.Add("Employee State");
                          view.Update();
                      }
                  }
              });
          }
          catch (Exception ex)
          {
              ex.ToString();
          }
          finally
          {

          }
      }


No comments:

Post a Comment