How to: Programmatically create a SP2007 list view

In order to programmatically create a new view in SP2007, you need to get a reference to the list, get the collection of available views, setup a filter –if needed, and create a new collection of fields to include in the new view.

Use the following example as a how-to.

 

SPList workList = web.Lists[“Work List”]; //reference to the list
SPViewCollection wlViews = workList.Views; //collection of available views
System.Collections.Specialized.StringCollection scViewFields = new System.Collections.Specialized.StringCollection(); //collection of fields to display in new view
string filter = “<Where><Geq><FieldRef Name=\”ReminderDate\”/><Value Type=\”DateTime\”><Today /></Value></Geq></Where>”; //CAML Query to filter items in/out of the view
scViewFields.Add(“Work Item Id”); //add fields to view
scViewFields.Add(“LinkTitle”); //title with Context menu to edit the item
scViewFields.Add(“Date Published”);
pqViews.Add(“Overdue Queries”, scViewFields, filter, 8, true, false); //add view to collection of views
MGR: the Intelogist

About MGR: the Intelogist

SharePoint Server developer, turned Sitefinity developer, turned Angular developer, turned SharePoint Online consultant, turned Unily consultant, turned O365 consultant... Never a dull moment!

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> 

This site uses Akismet to reduce spam. Learn how your comment data is processed.