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

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

Rate This
  • Comments 14

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();

 

            viewFields.Add("Name");

            viewFields.Add("Type");

         

            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);

 

 oWeb.Update();

 

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

======================================

try

            {

    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.ViewFields.Add(strField);

                        }

                        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;

                        oWebPartView.Update();

 

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

                        myWebPart.Visible = true;

                        myWebPart.Title = "My title";

                        oWebPartManager.SaveChanges(myWebPart);

 

                        break;

 

                    }

                }

                oWeb.Update();    

 

            }

            catch (Exception ex)

            {

                MessageBox.Show(ex.Message);

            }

Comments
  • PingBack from http://msdnrss.thecoderblogs.com/2008/03/15/programmatically-create-a-view-custom-view-of-a-list-and-change-the-view-2/

  • 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?

  • "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.

  • 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.

  • Can you please share the dummy windows application that you created with me.If yes can you please email me at gunjana@gmail.com

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

  • We can achieve same thing by setting following property:

    webPart.SuppressWebPartChrome = true;

  • How can we programmatically create a styledd ttsheet

  • 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 ?

  • Hi,

    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?

  • 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;

                       view.Update();

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

  • 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...

  • Instead of   view.Query = query;

    try

    spquery Query = "Something";

    view.Query = Query.query;

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

Page 1 of 1 (14 items)
Leave a Comment
  • Please add 8 and 5 and type the answer here:
  • Post