Online Training On SharePoint
                      

Thursday, 24 July 2008

Creating a view to show all items in a list in SharePoint

If we want to show all the items in a list even when they are stored in multiple folders inside a list we need to use SPViewScope.Recursive. To use this we can create a new view with SharePoint object Model and then use this Recursive property. This view will show all the items in a flat list irrespective of folder hierarchy in the list.

Here is the code snippet:

SPSite spsite = new SPSite("http://mysite/");
SPWeb spweb = spsite.OpenWeb();
SPList splist = spweb.Lists["mylist"];
SPViewCollection spviewcollection = splist.Views;
string strMyView= "MyView";
System.Collections.Specialized.StringCollection viewColumnFields =
new System.Collections.Specialized.StringCollection();
viewColumnFields.Add("Title");
viewColumnFields.Add("Age");
string filter =

//show all the items which has age less than 100
spviewcollection.Add(strMyView, viewColumnFields, filter, 100, true, true);
//This will add the view in the list and make it the default view
spview.Scope = SPViewScope.Recursive;
//With this all the items inside the folders will be displayed
spweb.Update();

2 comments:

Unknown said...

It seems to be very interesting for me, but I'm a newbie in SP.
Could you please explain where should I put this code?

Thank you.

Unknown said...

You can create a feature and use this code there. Once you deploy the feature the view will be created and will show all the items.

Related Posts with Thumbnails