locked
Changing a Web Part Lists default view RRS feed

  • Question

  • I have created a new view for my Shared Documents, document library, called TestView. Currently the default view of this document library is "All Documents". I want to change the defualt viewto TestView. So far I have the following code:

     

    list.Views["TestView"].DefaultView = true;

    list.Update();

     

    This doesn't work. When I navigate to my document library the current (and default) view is still "All Documents". How can I change this?

     

    Lucy

    Tuesday, March 25, 2008 12:25 PM

Answers

  • I think you need to get the SPView object, make the SPView object a default view, and run Update() method against the view. Please try something similiar to this:

    SPView view = list.Views["TestView"];
    view.DefaultView = true;
    view.Update();
    Tuesday, March 25, 2008 7:10 PM
  • Thank you for your help!

    My code now works with the following:

     

    SPView view = list.Views["TestView"];

    view.DefaultView = true;

    view.Update();

    list.Update();

     

    Now I have another problem! What I really wanted to do was to change the view of the document library as it is seen on the default.aspx page (as a web part). So Far I have the following code but this just changes the view of the document library when it is selected:

     

    Code Snippet

    SPLimitedWebPartManager webPartManager = mySite.GetLimitedWebPartManager("http://lholgate/default.aspx", PersonalizationScope.Shared);

    for (int i = 0; i < webPartManager.WebParts.Count; i++)

    {

    Microsoft.SharePoint.WebPartPages.WebPart wp = (Microsoft.SharePoint.WebPartPages.WebPart)webPartManager.WebParts[i];

    if (wp.ToString().Contains("ListViewWebPart") == true)

    {

    ListViewWebPart lvwp = (ListViewWebPart)wp;

    if (lvwp.Title == "Shared Documents")

    {

    Guid guid = new Guid(lvwp.ListName);

    SPList list = mySite.Lists[guid];

    SPView view = list.Views["TestView"];

    view.DefaultView = true;

    view.Update();

    list.Update();

    }

    }

    else

    {

    }

    }

    mySite.Update();

     

     

    Does anyone know how to change the view of the web part?

     

    Lucy

    Wednesday, March 26, 2008 9:21 AM
  • I should've been more specific. SPView has a ViewFields property, and you can customize the property to show specific fields. Here is an example how to use view fields..


                // Refresh View by deleting all fields and creating them again
                string[] fields;
                fields = new string[view.ViewFields.Count];
                for (int i = 0; i < view.ViewFields.Count; i++)
                {
                    fieldsIdea = view.ViewFieldsIdea;
                }

                view.ViewFields.DeleteAll();
                foreach (string field in fields)
                {
                    view.ViewFields.Add(field);
                }
                view.Scope = SPViewScope.Recursive;
                view.Update();
    Friday, March 28, 2008 7:20 PM

All replies

  • Is it required to do it through object model code?

     

    if it is just one time thing, you can setup the Default view from Document Library settings. go inside your view and you will see the check box in the begining of the Page. so mark it to make it a default view.

     

    Tuesday, March 25, 2008 1:28 PM
  • Yes it is. That is exactly what I want to do - but through code!

     

    The reason for this is that I have developed a feature that creates a new column and a new view that includes this column now I want this view to be the default when the feature is installed.

     

    Lucy

    Tuesday, March 25, 2008 2:14 PM
  • your method of setting the DefaultView property is fine. however perform  the View Update 1st and then perform the List Update afterward. you are missing your list.Views["TestView"].Update(); method call.

     

    Tuesday, March 25, 2008 2:41 PM
  • My code is now:

     

    list.Views["TestView"].DefaultView = true;

    list.Views["TestView"].Update();

    list.Update();

     

    This still does not seem to be working Sad.

    Tuesday, March 25, 2008 2:48 PM
  • just for debugging purpose, iterate on all views of your list and set the DefaultView to false and call each veiw Update method as well. after that set this one as True to see if it helps.

     

    Tuesday, March 25, 2008 3:18 PM
  • I have now tried the following code:

     

    Code Snippet

    for (int i = 0; i < list.Views.Count; i++)

    {

    if (list.Views[i].Title == "TestView")

    {

    list.Views[i].DefaultView = true;

    list.Views[i].Update();

    list.Update();

    mySite.Update();

    }

    else

    {

    list.Views[i].DefaultView = false;

    list.Views[i].Update();

    list.Update();

    mySite.Update();

    }

    }

     

     

     

    I have also tried different variations in this for loop and still no luck. The default view is still "All Documents".

    Tuesday, March 25, 2008 3:32 PM
  • after list update try list.SystemUpdate method and see if it updates the view. use it in above latest code. if this does not resolve the issue, i am sure some MSFT guy will respond on it soon.
    Tuesday, March 25, 2008 5:13 PM
  • There is no SystemUpdate method for SPList. This method only exists for SPListItem.

     

    Thanks for you help.

    Tuesday, March 25, 2008 5:29 PM
  • sorry about the confusion. i thought it was an option on SPlist.

     

    see in this posted article where some one has suggested to setup the DefaultView on SPList object. as per MSDN article it is a read only property but this post has been marked as answered & using it as a writeable property as well. so not sure if it can work. in that case it will be like

     

    list.DefaultView = list.Views["Testview"]

    http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=1403469&SiteID=1

     

     

     

    Tuesday, March 25, 2008 5:48 PM
  • I think you need to get the SPView object, make the SPView object a default view, and run Update() method against the view. Please try something similiar to this:

    SPView view = list.Views["TestView"];
    view.DefaultView = true;
    view.Update();
    Tuesday, March 25, 2008 7:10 PM
  • Jason is right.  Accessing the view through its list will not work for a few operations.

    Tuesday, March 25, 2008 8:07 PM
  • Thank you for your help!

    My code now works with the following:

     

    SPView view = list.Views["TestView"];

    view.DefaultView = true;

    view.Update();

    list.Update();

     

    Now I have another problem! What I really wanted to do was to change the view of the document library as it is seen on the default.aspx page (as a web part). So Far I have the following code but this just changes the view of the document library when it is selected:

     

    Code Snippet

    SPLimitedWebPartManager webPartManager = mySite.GetLimitedWebPartManager("http://lholgate/default.aspx", PersonalizationScope.Shared);

    for (int i = 0; i < webPartManager.WebParts.Count; i++)

    {

    Microsoft.SharePoint.WebPartPages.WebPart wp = (Microsoft.SharePoint.WebPartPages.WebPart)webPartManager.WebParts[i];

    if (wp.ToString().Contains("ListViewWebPart") == true)

    {

    ListViewWebPart lvwp = (ListViewWebPart)wp;

    if (lvwp.Title == "Shared Documents")

    {

    Guid guid = new Guid(lvwp.ListName);

    SPList list = mySite.Lists[guid];

    SPView view = list.Views["TestView"];

    view.DefaultView = true;

    view.Update();

    list.Update();

    }

    }

    else

    {

    }

    }

    mySite.Update();

     

     

    Does anyone know how to change the view of the web part?

     

    Lucy

    Wednesday, March 26, 2008 9:21 AM
  • Hi,

    The problem is that you are updating the default view of the List and not the web part itself.  Try and update the view of the web part. The problem is that I am not sure how to do this.  I found the following but it does not seem to work.


    lvwp.ViewGuid = "guidofview";

    let me know if you figure out how to update the view.

    Thanks
    Arjuna.
    Wednesday, March 26, 2008 10:16 AM
  • Thanks for the tip! I now have this code:

     

    if (lvwp.Title == "Shared Documents")

    {

    Guid guid = new Guid(lvwp.ListName);

    SPList list = mySite.Lists[guid];

    SPView view = list.Views["TestView"];

    view.DefaultView = true;

    view.Update();

    list.Update();

    lvwp.ViewGuid = view.ID.ToString("B").ToUpper();

    webPartManager.SaveChanges(lvwp);

    }

     

     

    If I debug this code and step through each line, the lvwp.ViewGuid changes to view.ID.ToString("B").ToUpper(); even up until the last curly bracket, as though it is saved as it should be( webPartManager.SaveChanges(lvwp); ) but when I try this again the original ViewGuid is shown as though it has not been saved previously. Am I missing an update or something? I cannot see another update to use.

     

    Lucy

    Wednesday, March 26, 2008 10:59 AM
  • I have just noticed smething else! If I manually change the view of the document library web part on the default.aspx page (via the UI) to "All documents" and then in my code simply get the ViewGuid of the selected ListViewWebPart, it returns this guid:

     

    {3F43FD3A-7D31-48ED-997F-D81D10246603}

     

    If I again maually change the view again to "TestView" and get the ViewGuid I get this:

     

    {3F43FD3A-7D31-48ED-997F-D81D10246603}

     

    And I again get the above guid if I change the view to "Explorer View".

    When you change the view maually the drop down list has shown as selected <Current view>. I am guessing that when you get the ViewGuid, the guid returns the guid for the selected view <Current view>. So, How do you change the view?

     

    Lucy

    Wednesday, March 26, 2008 12:32 PM
  • I can change the view of the web part when adding a new web part like so:

     

    SPList list = mySite.Lists["Shared Documents"];

    SPView view = list.Views["TestView"];

    view.Toolbar = "";

    view.Update();

    ListViewWebPart wp = new ListViewWebPart();

    wp.ZoneID = "Left";

    wp.ListName = list.ID.ToString("B").ToUpper();

    wp.ViewGuid = view.ID.ToString("B").ToUpper();

    SPLimitedWebPartManager webPartManager = mySite.GetLimitedWebPartManager("http://lholgate/default.aspx", PersonalizationScope.Shared);

    webPartManager.AddWebPart(wp, "Left", 0);

     

     

    This however shows the full toolbar of the document library. I still cannot simply change the view of the existing web part.

    Lucy

    Thursday, March 27, 2008 9:31 AM
  • I have changed the view of my web part manually (through the UI) to "TestView". I then setup a simple application that gets the ListViewXml of the web part:

     

    richTextBox1.Text = lvwp.ListViewXml;

     

    I then saved the text in the rich textbox. Going back to my SharePoint site, I manually changed the view back to "All Documents". Then going back to my application I changed the code so that I would set the ListViewXml:

     

    lvwp.ListViewXml = richTextBox1.Text;

     

    After saving the changes webPartManager.SaveChanges(lvwp); I went back to my SharePoint site and refreshed the home page (I have also tried this by doing an iisreset) and the view of my web part did NOT change. It's as though it is not being saved. But if I simple change the title of the web part then the changes are saved just not the view. Also, when I get the name of the view I get nothing. Anyone any ideas please?

     

    Lucy

    Thursday, March 27, 2008 5:10 PM
  • Lucy,

    When you add a ListView webpart to a page such as Default.aspx from SharePoint UI, the ListView webpart creates a copy of the referrenced view as a hidden View inside the referenced SharePoint List. The newly created hidden view is used for the specific ListView webpart only. The ListView webpart uses its own hidden View, and does not use the referrened view anymore. The reason is that even if you edit the current view from the ListView web part, the change would not affect the referrenced view.

    So, the way to update the View in the ListView webpart is slightly different from updating a view on SharePoint List. And actually, SPList.DefaultView = true will not do anything on the ListView webpart because it is a default view from the beginning.

    There are couple of options here. Both options will work. Typically, I would use the Option 1 for creating a new custom ListViewWebpart, and use Option 2 for updating the existing ListView webpart.

    Option 1: Delete an existing "Shared Documents" webpart and create a new one.

    The code for creating a new webpart using the TestView would look like this:

    ListViewWebPart wp = new ListViewWebPart();
    wp.ListName = list.ID.ToString("B").ToUpper();
    wp.ViewGuid = list.Views["Test View"].ID.ToString("B").ToUppser();
    webPartManager.AddWebPart(wp, [zoneName], [zoneIndex]);


    Option 2: Modifying a hidden view which would resemble the Test View.

    if (lvwp.Title == "Shared Documents")

    {

    Guid guid = new Guid(lvwp.ListName);

    SPList list = mySite.Lists[guid];

    SPView view = list.Views[new Guid(lvwp.ViewGuid)];

    SPView testView = list.Views["Test View"];

    // Copy settings from Test View to the hidden view

    view.Fields = testView.Fields;

    view.Query = testView.Query;

    view.Update();

    }


    Friday, March 28, 2008 12:27 PM
  • Jason,

     

    Thank you for the reply and the help. I have already tried option 1 but I don't really want to delete it and add it again (as I found this created further problems such as the toolbar showing!)

     

    I am not trying option 2. However, SPView does not have a fields property. I tried view.ParentList.Fields but this property is read only. I have tried to assign all the set properties of view to the equivalent property of testView. None have been successfull (this is with updating the webpart, list and site). I'm still trying to find the correct property to assign.

     

    Lucy

    Friday, March 28, 2008 2:51 PM
  • I should've been more specific. SPView has a ViewFields property, and you can customize the property to show specific fields. Here is an example how to use view fields..


                // Refresh View by deleting all fields and creating them again
                string[] fields;
                fields = new string[view.ViewFields.Count];
                for (int i = 0; i < view.ViewFields.Count; i++)
                {
                    fieldsIdea = view.ViewFieldsIdea;
                }

                view.ViewFields.DeleteAll();
                foreach (string field in fields)
                {
                    view.ViewFields.Add(field);
                }
                view.Scope = SPViewScope.Recursive;
                view.Update();
    Friday, March 28, 2008 7:20 PM
  • Thank you for your help! That works perfectly now. And it makes sense to me as well. Good learning for me. Thanks again!

     

    Lucy

    Monday, March 31, 2008 10:00 AM
  • Hi
    Please tell me how to Programmatically change  the Selected-View for a WebPart List
    from a dropdown in another webpart.like connecting webpart.

    Any help appricicted
    Thanks
    Moss

    Monday, November 15, 2010 8:04 PM
  • Would you mind sharing the whole code? I've gotten lost and it's not making sense to me. Any help would be appreciated.

    David


    David Narramore

    Monday, November 27, 2017 7:44 PM