Friday, May 8, 2009

SharePoint Development Tutorial: Programmatically Create View on a List

While it is easy to create a view for a list in SharePoint, you may find a scenario where you need an easier way to create a view on a list. For example, I had a situation where I had to create over 30 views for one list. Creating the views using SharePoint can be time-consuming, and it could increase the possibility of user error because of the manual process. Using C#, the Microsoft.SharePoint library, and CAML, one can create a view for a list on SharePoint.

This illustration will demonstrate the simplest example of using C# code to add a view to a list (as a console application). You may want to make modifications for your needs, or use it as a foundation for a larger-scale application.

Note: This code will only work on a server where SharePoint is installed. For example, if you are going to add a view to a list that exists on http://bogus/bogussite, you need to have this code on the server that houses http://bogus.

The Scenario

In this illustration, there is a list on a SharePoint site called "Doctor Who Episodes". This list needs a new view called "JNT Era", which will display Doctor Who episodes made during the JNT era (1980-1989). This list has four fields:

  • Title

  • The Doctor

  • Synopsis

  • Year

See the example list

After starting a new project, you will need to add a reference to the Microsoft.SharePoint library in your code, and you will need to indicate that you will be using the library in your code.
See the example snippet

The next step is to define the SharePoint classes that you will need to perform the action. The four classes that you will need are:

  • SPSite - Collection of sites

  • SPWeb - The SharePoint web site

  • SPList - A List on a SharePoint web site

  • SPViewCollection - Collections of views on a list

In the code, you will need to access the website. You will define an instance of the SPSite class to get the site, then you will need to define an instance of the SPWeb class to have a reference to the actual site.

SPSite oSite = new SPSite("http://bogus/bogussite");
SPWeb oWeb = oSite.OpenWeb();

Alternative to SPList oList = oWeb.Lists["List Name"]

You can also access the list by the GUID. Use the following logic if you want to access the list by the GUID rather than the name:

Guid gui = new Guid("The List GUID");
SPList oList = oWeb.Lists[gui];

Then, you will need to access the list that will get the new view, and you will need to access the list's collection of views.

SPList oList = oWeb.Lists["Doctor Who Episodes"];
SPViewCollection oViewCollection = oList.Views;

Finally, you will need to define the new view name.

string strViewName = "JNT Era";

See the example snippet

We are ready to define the fields that will be displayed in this new view. In this example, the view will display all the fields. You will need to define an instance of the System.Collections.Specialized.StringCollection class to hold the names of the fields that will be displayed in the view.

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

To add the fields, use the Add() function of the class. You do not have to use the special hexadecimal characters to represent spaces and special characters for the field names. You can use the literal field name.
See the example snippet

Helpful Links:

CAML Syntax

Stramit Caml Viewer

We have to define the criteria for the view using CAML. In our illustration, we want to display the Doctor Who episodes from 1980 until 1989, and we want to display them in ascending order. Your scenario may be different, so your CAML will be different. If you are unsure of CAML syntax, a link to a site that has documentation on the CAML syntax has been provided for you on this post. If you want to make sure that your query will work before you begin coding, you can use the Stramit Caml Viewer to test your CAML query. A link to the tool has also been provided for you on this post. In this illustration, I am using the StringBuilder class to hold the CAML query. Then, I am converting it to a String.
See the example snippet

Finally, we are ready to add the view. The Add() function of the SPViewCollection class adds a view to the list. The Add function
takes six parameters:

  • View Name - a string

  • Collection of View Fields - string collection

  • CAML query - a string

  • Row Count - an integer

  • Is this paged? - a boolean

  • Is this going to be the default view? - a boolean

You also have to call the Update() function of the SPWeb class to make sure the changes "take".

oViewCollection.Add(strViewName, viewFields, query, 5000, true, false);

See the example snippet

After completing, compiling and running the program, the list now has a new view!
See the results

If you would like a copy of the skeleton code, you can download the RTF file of the skeleton code here.

Please post your questions or comments, and I will answer your questions to the best of my ability.


Jeffrey Langdon said...

Nice work. I am working on something similar, but with a join. I want to join two lists and filter on list "A" based on a field from list "B".

Web Design Quote said...

Thanks for the post on the sharepoint development. I think this would be helpful for the user for those who are not aware of this. Keep it up...!