Sunday, October 5, 2008

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);



                    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);



                    System.Diagnostics.Debug.WriteLine("Creating the view");

                    System.Collections.Specialized.StringCollection strColl = new System.Collections.Specialized.StringCollection();


                    strColl.Add("First Name");

                    strColl.Add("Last Name");



                    strColl.Add("Latest Purchase Date");

                    strColl.Add("Sales Comments");

                    myNewList.Views.Add("Summary", strColl, @"", 100, true, true, Microsoft.SharePoint.SPViewCollection.SPViewType.Html, false);






SPViewCollection.Add Method (MSDN - Microsoft.SharePoint)

Views.AddView Method (MSDN - Views Web Services)


Bill said...

How do you specify the resulting filename of the view (e.g. myview.aspx, default is the name of the title.aspx)?

Marc Charmois said...

Hi Bill,

1 - Regarding the View creation:
The name of the .aspx page of the view is the ViewName parameter of the SPList.Views.Add method ("Summary" in my post).
The View Name that appears in the SharePoint pages is the View Title.

If you want the title of the view and the name of the .aspx page of the view to be different you have to change the view title by programmatically updating the view after having created it.

2 - Regarding changing the file name of the .aspx page of the view.

When you update a view using SharePoint ViewEdit.aspx application page, there is a field

"Web address of this view:"

where you can change the name of the .aspx page of the view.

I have checked if this field is coresponding to a WSS Object Model Class property or Class Method parameter and it seems that there is nothing for that.

Actually, it is like a list. You cannot rename a list rootfolder otherwise than to recreate it with the new name and delete the old list.
You have to create a new view with a new ViewName ("Summary" in my post) that will be the .aspx page file name, then if you want the title of the view and the name of the .aspx page of the view to be different you have to change the view title.
Then you have to delete the old view.
It is what the OWSSVR.dll is doing I think.
I am preparing a new post on that topic.