Showing posts with label SharePoint List with code. Show all posts
Showing posts with label SharePoint List with code. Show all posts

Wednesday 14 March 2012

Create a SPlist and its SPView in SharePoint writing C# code against WSS object model


Create a SPlist and its SPView in SharePoint writing C# code against WSS object model

How to create a SPlist and its SPView in SharePoint writing C# code against the WSS object model ?

Assume you want to create the list and its view when activating a SharePoint Feature.
        public class myFeatureReceiver : Microsoft.SharePoint.SPFeatureReceiver
        {
            public override void FeatureActivated(SPFeatureReceiverProperties properties)
            {
                using (SPWeb web = properties.Feature.Parent as SPWeb)
                {
                    System.Diagnostics.Debug.WriteLine("Creating the list");
                    //web.AllowUnsafeUpdates = true; //use this in an application page no need in a dll
                    web.Lists.Add("Customers""Store informations about my Company Customers",SPListTemplateType.GenericList);
                    web.Update();

                    System.Diagnostics.Debug.WriteLine("Creating the Fields");
                    SPList myNewList = web.Lists["Customers"];
                    myNewList.Fields.Add("First Name"SPFieldType.Text, false);
                    myNewList.Fields.Add("Last Name"SPFieldType.Text, false);
                    myNewList.Fields.Add("Adress"SPFieldType.Text, false);
                    myNewList.Fields.Add("City"SPFieldType.Text, false);
                    myNewList.Fields.Add("Latest Purchase Date"SPFieldType.DateTime, false);
                    myNewList.Fields.Add("Sales Comments"SPFieldType.Note, false);
                    myNewList.Update();

                    System.Diagnostics.Debug.WriteLine("Creating the view");
                    System.Collections.Specialized.StringCollection strColl = newSystem.Collections.Specialized.StringCollection();
                    strColl.Add("Title");
                    strColl.Add("First Name");
                    strColl.Add("Last Name");
                    strColl.Add("Adress");
                    strColl.Add("City");
                    strColl.Add("Latest Purchase Date");
                    strColl.Add("Sales Comments");
                    myNewList.Views.Add("Summary", strColl, @"", 100, truetrue, Microsoft.SharePoint.SPViewCollection.SPViewType.Html, false);
                    myNewList.Update();
                }
            }
        }


References

SPViewCollection.Add Method (MSDN - Microsoft.SharePoint)

Views.AddView Method (MSDN - Views Web Services) 

Pages

Followers