Programmatically create a view (custom view) of a list and change the view

If you want to programmatically create a view of a list and if you want to change the existing view of a ListViewWebPart to the custom view you can develop it by referring the following code sample.  

code snippet for creating a custom view



SPSite oSite = new SPSite([Site URL]);// [Site URL] change it to your sharepoint site URL

SPWeb oWeb = oSite.OpenWeb();

SPList oList = oWeb.Lists["shared documents"];  

SPViewCollection oViewCollection = oList.Views;


            string strViewName = "MyCustomView";


            System.Collections.Specialized.StringCollection viewFields =

            new System.Collections.Specialized.StringCollection();





            string query = "<Where><Eq><FieldRef Name=\"Name\"/>" +

                "<Value Type=\"Text\">mysample</Value></Eq></Where>";// here you can filter your items using the selected

                                                                         item in the dropdownlist

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




Now refresh the list in UI and then you can see the “MyCustomView” in the selection list of views.


The below code will change the current view of list to our custom view. You can use the below code to change the view programmatically.


code snippet for changing the view




    SPSite oSite = new SPSite([Site URL]);// change it to your sharepoint site URL

                SPWeb oWeb = oSite.OpenWeb();

                SPList oList = oWeb.Lists["shared documents"];              


                SPLimitedWebPartManager oWebPartManager = oWeb.GetLimitedWebPartManager("Shared%20Documents/MyCustomPage.aspx", PersonalizationScope.Shared);// change the wepart page absolute URL to your webpart page URL

                foreach (System.Web.UI.WebControls.WebParts.WebPart oWebpart in oWebPartManager.WebParts)


                    if (oWebpart.GetType().Name == "ListViewWebPart")


                        ListViewWebPart myWebPart = (ListViewWebPart)oWebpart;


                        SPView doclibview = oList.Views[“MyCustomView”];


                        Guid oGuid = new Guid(myWebPart.ViewGuid);

                        SPView oWebPartView = oList.Views[oGuid];


                        oWebPartView.ViewFields.DeleteAll();// deleting the existing view fields for adding new one


                        foreach (string strField in doclibview.ViewFields)




                        oWebPartView.Query = doclibview.Query;

                        oWebPartView.RowLimit = doclibview.RowLimit;

                        oWebPartView.ViewEmpty = doclibview.ViewEmpty;

                        oWebPartView.ViewFooter = doclibview.ViewFooter;

                        oWebPartView.ViewHeader = doclibview.ViewHeader;

                        oWebPartView.Scope = doclibview.Scope;

                        oWebPartView.GroupByFooter = doclibview.GroupByFooter;

                        oWebPartView.GroupByHeader = doclibview.GroupByHeader;



                        myWebPart.ViewGuid = oWebPartView.ID.ToString("B").ToUpper();

                        myWebPart.Visible = true;

                        myWebPart.Title = "My title";










            catch (Exception ex)




Comments (15)

  1. SPDotnet says:

    The 1st half of the code works perfectly fine but would you mind helping me with where should I write "code snippet for changing the view".

    Also, Does "Shared Documents/MyCustomPage.aspx" has a physical location? If yes, what it is?

  2. sowmyancs says:

    "code snippet for changing the view"

    I have tested this code in a .Net windows based application. If you want to plug it in an menu item then you can do it. Let me know what is your exact requirement then I can suggest the best way to put it.

    All the contents of the document library are storing in sharepoint content DB. You can also open those files in a windows explorer using WebDav.

    Actions –> open with windows explorer.

  3. SPDotnet says:

    Thanks for your reply.

    My exact requirement is to create a web based sharepoint customized view to display the list data that is displayed in the document library into a datagrid.

  4. SPDotnet says:

    Can you please share the dummy windows application that you created with me.If yes can you please email me at

  5. sowmyancs says:

    I am so sorry. Unfortuanately I don’t have that sample right now. You can develop one by simply copying this code.

  6. Avinash says:

    We can achieve same thing by setting following property:

    webPart.SuppressWebPartChrome = true;

  7. Fagi says:

    How can we programmatically create a styledd ttsheet

  8. While testing "code snippet for creating a custom view", I am getting same view twice which we have created programmatically. But I dont want that to be happen, any suggestions ?

  9. DJ says:


    Im new to Sharepoint.Please help!! Is it possible to create a view, that displays all documents based from a document libraries? Requirement is like, I have 4 document libraries where i upload documents. While uploading the document there is a custom dropdown column named Group.And user has to select one from this dropdown.

    In the new View , all the document should be displayed based on the data in the group. Is it possible to do so?

  10. Advert says:

    how to update default view of calender and assign filter programatically???

    i am doing this…

     SPView view = oList.DefaultView;

    String query = "<Where><Eq><FieldRef Name='Room_x0020_Name' /><Value Type='Text'>IT Conference Room</Value></Eq></Where>";

                       view.Query = query;


    but it is not updating the default view of calender…please help….thanx….

  11. elke says:

    is there a way to add a column to a view with a link to the edit menu?

    i've tried "Name (linked to document with edit menu)" but that doesn't work…

  12. Nagarjuna says:

    Instead of   view.Query = query;


    spquery Query = "Something";

    view.Query = Query.query;

  13. I'm always impressed just what you Premier Engineers choose to blog.  This was very useful.

  14. Swapnil Phatale says:


    Its giving me error at  oWebPartManager.SaveChanges(myWebPart);

    exception has been thrown by the target of an invocation

    Please Help

Skip to main content