Sep 19, 2009

Creating Custom view of a list programmatically

Objective here is to create a custom view of the list and then setting this custom view of the list as a default view.

Here is the sample code:


SPSite site = new SPSite("http://spvm:2409");

SPWeb oSubWeb = site.OpenWeb();

oSubWeb.AllowUnsafeUpdates = true;

//SPListTemplateType templateType = SPListTemplateType.GenericList;

//Guid listId = oSubWeb.Lists.Add("Sample2 list Library", "This is a sample Document Library", templateType);

SPList list = oSubWeb.Lists["Sample2 list Library"];

SPSite site = new SPSite("Site URL");

SPWeb oSubWeb = site.OpenWeb();

oSubWeb.AllowUnsafeUpdates = true;

SPList list = oSubWeb.Lists["Sample2 list Library"];

//Create a custom view and then set the custom view as the default view of the list

SPView oView = list.DefaultView;

oView.Title = "CustomView";

//Add the available columns in the view

oView.ViewFields.Add("column2");

oView.ViewFields.Add("column1");

oView.Update();

list.Views["CustomView"].DefaultView = true;

list.Update();

Thus this way we can create our custom views.


Enjoy!!!


If you find this post useful then do post your comments....

No comments:

Post a Comment