Tuesday, May 27, 2008

How to create and add view dynamically though code in a list?

Hi All,

May times we need to create view dynamically and then add that created view to the List.

Here is a simple way to accomplish this task.

First make a StringCollection object.

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

then add Fields from list in the StringCollection object.

For example you want to create a new view with this 4 fields from list then,

viewFields.Add("LinkTitle");
viewFields.Add("Project_x0020_Type");
viewFields.Add("Job_x0020_Number");
viewFields.Add("Legacy_x0020_Job_x0020_Number");

String strQuery = "{Your CAML Query}";

This Query is for fetching all records where Title column is not blank. you can customize this query accordig to your requirement.

objTaskList.Views.Add("ViewName", viewFields, strQuery, 10000, false, true);

//to make just crteated view as default view

objTaskList.Views["ViewName"].DefaultView = true;
objTaskList.Update();

That's it. your job is done.

No comments:




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