Monday, May 21, 2012

create custom list view and filter the list data in sharepoint 2010

SharePoint has one of the best feature i:e we can  create a custom  view for list.Basically the view can used to display the user tasks based on the login in share point and we can filter the list data using as our filter condition.Here i will show how to create a view for subjects list and filter the data as per author
using (SPSite Site = new SPSite(url))
{
using (SPWeb sWeb = Site.OpenWeb())
{
SPList sList = sWeb.Lists["subjects"];
SPViewCollection sViewCollection = sList.Views;
string testViewName = "TestView";
System.Collections.Specialized.StringCollection testviewFields = new System.Collections.Specialized.StringCollection();
testviewFields.Add("Id");
testviewFields.Add("Name");
testviewFields.Add("Author");
testviewFields.Add("Group");
string spquery = "<query><where><eq><fieldref name="\"Name\"/">" +
                    "<value type="\"author\"/">Testview</value></fieldref></eq></where></query>";
sViewCollection.Add(testViewName, testviewFields, spquery, 100, true, false);
sWeb.Update();
}
}
Post a Comment

Bel