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.

Comments

Popular posts from this blog

ContextSwitchDeadlock was detected

Visual Studio setup and deployment projects

Using SvcUtil.exe to generate the proxy class and config file