Sunday, October 5, 2008  by Marc Charmois - About Me / Previous Posts / Archives - Home

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 = new System.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, true, true, Microsoft.SharePoint.SPViewCollection.SPViewType.Html, false);

                    myNewList.Update();

                }

            }

        }



References

SPViewCollection.Add Method (MSDN - Microsoft.SharePoint)

Views.AddView Method (MSDN - Views Web Services)

2 Comments:

Anonymous Bill said...

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

April 9, 2009 at 5:17 PM  
Blogger 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.

April 11, 2009 at 8:46 PM  

Post a Comment

Subscribe to Post Comments [Atom]

Links to this post:

Create a Link

<< Home