Programmatically create a view (custom view) of a list
code snippet for creating a custom view
=====================================
SPSite oSite = new SPSite([Site URL]);// [Site URL] change it to your sharepoint site URL
SPWeb oWeb = oSite.OpenWeb();
SPList oList = oWeb.Lists["shared documents"];
SPViewCollection oViewCollection = oList.Views;
string strViewName = "MyCustomView";
System.Collections.Specialized.StringCollection viewFields =
new System.Collections.Specialized.StringCollection();
viewFields.Add("Name");
viewFields.Add("Type");
string query = "" +
"mysample ";// here you can filter your items using the selected
item in the dropdownlist
oViewCollection.Add(strViewName, viewFields, query, 100, true, false);
oWeb.Update();
Now refresh the list in UI and then you can see the “MyCustomView” in the selection list of views.
=====================================
SPSite oSite = new SPSite([Site URL]);// [Site URL] change it to your sharepoint site URL
SPWeb oWeb = oSite.OpenWeb();
SPList oList = oWeb.Lists["shared documents"];
SPViewCollection oViewCollection = oList.Views;
string strViewName = "MyCustomView";
System.Collections.Specialized.StringCollection viewFields =
new System.Collections.Specialized.StringCollection();
viewFields.Add("Name");
viewFields.Add("Type");
string query = "
"
item in the dropdownlist
oViewCollection.Add(strViewName, viewFields, query, 100, true, false);
oWeb.Update();
Now refresh the list in UI and then you can see the “MyCustomView” in the selection list of views.
Comments