Thursday, March 25, 2010

Creating, Modifying SharePoint Views Programmatically

Creating a New View
String[] colArray = new String[] { "Column1", "Column2", "Column3” };
StringCollection colCollection = new StringCollection();
colCollection.AddRange(colArray);
String viewQry = “Your CAML Query Here’;
SPView newView = YourList.Views.Add(“View Title”, colCollection, viewQry, 100, true, false);

Note: 100 is the item limit, true is allow paging and false is to set as the default view

Adding Fields to an Existing View
SPList oList = oWebsite.Lists["List_Name"];

SPView oView = oList.Views["All Items"];
SPViewFieldCollection collViewFields = oView.ViewFields;
collViewFields.Add("Created");
oView.Update();

Adding a "Group By" to the View
String query =  "";
view.Query += query;
view.Update

No comments:

Post a Comment