Welcome to MSDN Blogs Sign in | Join | Help

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

 

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

            }

Published Saturday, March 15, 2008 3:00 AM by sowmyancs

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# MSDN Blog Postings &raquo; Programmatically create a view (custom view) of a list and change the view

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

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?

Monday, September 29, 2008 9:24 AM by SPDotnet

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

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

Monday, September 29, 2008 7:02 PM by sowmyancs

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

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.

Tuesday, September 30, 2008 2:24 AM by SPDotnet

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

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

Wednesday, October 01, 2008 4:43 AM by SPDotnet

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

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

Thursday, October 02, 2008 12:07 AM by sowmyancs

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

We can achieve same thing by setting following property:

webPart.SuppressWebPartChrome = true;

Thursday, November 13, 2008 1:21 AM by Avinash

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

How can we programmatically create a styledd ttsheet

Thursday, March 05, 2009 7:21 AM by Fagi

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

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 ?

Monday, March 16, 2009 8:40 AM by Vishwajit Walke

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

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?

Thursday, December 17, 2009 8:34 PM by DJ

Leave a Comment

(required) 
required 
(required) 

  
Enter Code Here: Required
 
Page view tracker