Creating SharePoint/WSS View Programmatically

23 04 2009

Using the object model it is really easy to create a SharePoint view programmatically in code and then add it to a list. This code could be executed from a workflow for example to create a dynamic view as required. The query for the view uses an SPQuery which uses CAML. Personally I like to use the U2U CAML Creator application to create the CAML query for me which was created by Patrick Tisseghem (


//Define an array to hold the columns required in the view

String[] colArray = new String[] { "Column1", "Column2", "Column3” };

//Add the array to a string collection

StringCollection colCollection = new StringCollection();


//Define the query for the view

String viewQry = “Your CAML Query Here’;

//Define the new view & Add to the List (100 is the item limit, true is allow paging and false is to set as the default view)

SPView newView = YourList.Views.Add(“View Title”, colCollection, viewQry, 100, true, false);

**End Code**

Success, your view should now be visible in the list!

Hope this helps someone! 🙂




One response

28 12 2009

hello alex
what if i need to add the existing fileds like checked out to or version field to my new created view

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: