Friday, November 7, 2008

How to create a List/document library view programmatically

We @ sharepointkings not only answer questions and challenges posted by visitors and the fan club but literally hunt the same across the web.

This week Challenge hunters came across one such challenging question across many blogs.

“How to create a List/document library view programmatically?”
Reference: http://suguk.org/forums/thread/13624.aspx

“Create Document Library Views programmatically”
Reference: http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/Server/MS-SharePoint/Q_23345716.html

And the solution is here:

To create a view in a list you have a SPViewCollection.Add Method (Microsoft.SharePoint).
There are 2 overloads available with the following parameters to be set:

Parameters
strViewName
A string that contains the name of the view.
strCollViewFields

A System.Collections.Specialized.StringCollection object that contains the internal names of the view fields.

strQuery
A Collaborative Application Markup Language (CAML) string that contains the Where clause for the query.

iRowLimit
A 32-bit unsigned integer that specifies the maximum number of items to return in the view. Specifying a value greater than Int32.MaxValue (2,147,483,647 or hexadecimal 0x7FFFFFFF) throws an exception because the value is out of range.

bPaged
true to specify that the view supports displaying more items page by page; otherwise, false.

bMakeViewDefault
true to make the view the default view; otherwise, false.

type
Microsoft.SharePoint.SPViewCollection.SPViewType.Html
Microsoft.SharePoint.SPViewCollection.SPViewType.Grid

bPersonalView
true to create a personal view; false to create a public view.

Return Value
An SPView object that represents the view.



The below code is written for Creating a Grid view and displays all items in a list.

SPSite oSiteCollection = SPContext.Current.Site;
using (SPWeb oWebsite = oSiteCollection.AllWebs["Website_Name"])
{
SPList oList = oWebsite.Lists["List_Name"];
SPViewCollection collViews = oList.Views;

string strViewName = "View_Name";

System.Collections.Specialized.StringCollection collViewFields = new System.Collections.Specialized.StringCollection();

collViewFields.Add("Field1_Name");
collViewFields.Add("Field2_Name");
collViewFields.Add("Field3_Name");

string strQuery = "<Where><Gt><FieldRef Name='ID' /><Value Type='Counter'>0</Value></Gt></Where>";

collViews.Add(strViewName, collViewFields, strQuery, 100, true, false);
}


You can also create a View based on a previous view. Example you have a View “All Items”.

To achieve this you have to just write the below piece of code:

SPSite oSiteCollection = SPContext.Current.Site;
using (SPWeb oWebsite = oSiteCollection.AllWebs["Website_Name"])
{
SPList oList = oWebsite.Lists["List_Name"];
SPViewCollection collViews = oList.Views;

string strViewName = "View_Name";


string strQuery = "<Where><Gt><FieldRef Name='ID' /><Value Type='Counter'>0</Value></Gt></Where>";

collViews.Add(strViewName, oList.Views["All Items"].ViewFields.ToStringCollection(), strQuery, 100, true, false);

}

To Create a View a HTML View instead of a Grid View then…try this

collViews.Add(strViewName, collViewFields, strQuery, 100, true, false , Microsoft.SharePoint.SPViewCollection.SPViewType.Html, False)

Thank you!

6 comments:

Rags said...

Hi,
I want to create a view with Name (linked to document with edit menu) which is defaulted in sharepoint. Help me to fix this problem

Anonymous said...

What does the parameter strQuery specify here?

string strQuery = "&ltWhere>&ltGt>&ltFieldRef Name='ID' />&ltValue Type='Counter'&gt0/Value>/Gt>/Where>";

cant we use the query property of the "All Items" view for creating a view similar to All Items View?

:S

Parth Patel said...

@rags: you can create a view first and then modify the view coluns by SPView.ViewFields objects.

add columns you want.


@Anonymouse: All items view uses "<OrderBy><FieldRef Name=\"ID\" /></OrderBy>" as query you can use the same.

Jason Graham said...

To Rags's question: Use "LinkTitle" instead of "Title" for your field name. That will add the ECB menu as well.

If you do not want the ECB menu (edit menu) on the item as well, then use "LinkTitleNoMenu" instead of "LinkTitle" or "Title." Hope that helps.

Thakhi Shaik said...
This comment has been removed by the author.
SharePoint Kings said...

@Thakhi Shaik,

if its public view you can set only one view as default but you cannot restrict user to see available view if its public.

and personal view of user cannot be shared with users but it cannot be set as default.




Share your SharePoint Experiences with us...
As good as the SharePointKings is, we want to make it even better. One of our most valuable sources of input for our Blog Posts comes from ever enthusiastic Visitors/Readers. We welcome every Visitor/Reader to contribute their experiences with SharePoint. It may be in the form of a code stub, snippet, any tips and trick or any crazy thing you have tried with SharePoint.
Send your Articles to sharepointkings@gmail.com with your Profile Summary. We will Post them. The idea is to act as a bridge between you Readers!!!

If anyone would like to have their advertisement posted on this blog, please send us the requirement details to sharepointkings@gmail.com