Programmatically Working with ListViewWebPart using Non-Default View (GetUncustomizedViewByBaseViewId)

June 25, 2008 at 2:02 pm 12 comments

If you add an Announcement web part to page from the web part gallery you get a nice view of the list which shows 5 items, shows (More Announcements…) link and “Add a new Announcement” link

ListViewWebPart - Announcements

This is exactly what I needed for one of my web part but my requirement was to add the web part to page from a feature programmatically and not using the UI.

After looking around for a while in MSDN and searching most of the examples showed how to set the properties of the ListViewWebPart programmatically but all of the examples used the Default View of the list as shown below (The default view is the view which you get by default on AllItems.aspx).

<br /> SPList list = web.Lists["Announcement"];<br /> ListViewWebPart webPart = new ListViewWebPart();<br /> webPart.Title = "Announcements";<br /> webPart.ViewGuid = list.DefaultView.ID.ToString("B").ToUpper();<br /> 

If you look at the Schema file for the list, the View with BaseViewID of 0 is the one which has the HTML to render the view in the ListViewWebPart

<View BaseViewID=”0″ FreeForm=”TRUE” Type=”HTML”>

So First thing that came to my mind was to iterate though the Views collection of SPList object and look for a view with BaseViewID of zero, but to my surprise it was not there in the collection OOPSSS! But If I add the Announcement web part using the UI and than search the Views collection of SPList object for BaseViewID of Zero than the View is found in the collection. So the moral of the story is SharePoint is doing some thing to customize the view and add it to the Views collection.

Now how do you do the same programmatically? Luckily I did not try very hard and found a method called GetUncustomizedViewByBaseViewId on the SPList object and that was it, here is the code snippet:

<br /> SPList list = web.Lists["Announcement"];<br /> ListViewWebPart webPart = new ListViewWebPart();<br /> webPart.Title = "Announcements";<br /> webPart.ListName = list.ID.ToString("B").ToUpper();<br /> SPView view = list.GetUncustomizedViewByBaseViewId(0);<br /> webPart.ListViewXml = view.HtmlSchemaXml;<br /> 

Few things though:

  • You cannot use ViewGuid property of ListViewWebPart as ID of the View returned by GetUncustomizedViewByBaseViewId will be empty instead use ListViewXml property of ListViewWebPart and assign View’s HtmlSchemaXml property to it
  • If your List is in a different SPWeb than the page on which you are adding ListViewWebPart than use WebId property of ListViewWebPart to point it to the ID of SPWeb which contains the List

Happy SharePointing!

About these ads

Entry filed under: MOSS. Tags: , .

Removing Attachment button from MOSS Form Toolbar Programmatically setting up Approval workflow in MOSS

12 Comments Add your own

  • 1. huk Mok  |  July 15, 2008 at 11:40 am

    A user wants the View select button to appear right beside the buttons on the left. How can I do that ? Do I convert the ListView Webpart into DataView webpart and change the html code inside.



  • 2. madcour  |  September 23, 2008 at 5:46 pm

    Is an update() method required for this webpart to show up in the page? What could cause a custom web part to not render?

  • 3. pateketu  |  September 23, 2008 at 7:26 pm

    How are you adding the web part to your Page? using WebPartManager?

  • 4. madcour  |  September 24, 2008 at 2:16 pm

    Yes, with these chained methods:
    myPage.GetLimitedWebPartManager(System.Web.UI.WebControls.WebParts.PersonalizationScope.User).AddWebPart(myGrpsTasksWP, “Right”, 4);

  • 5. madcour  |  September 24, 2008 at 2:18 pm


    AddWebPart(myGrpsTasksWP, “Right”, 4);

  • 6. pateketu  |  September 24, 2008 at 3:20 pm

    Yes you will need to call Updat() method on the page, also if it is a publication site than CheckIn() and Approve()

  • 7. madcour  |  September 24, 2008 at 4:10 pm

    Hmm…still not working for me. Here’s my code below. Why wouldn’t this render on the page? My ListName property may be set incorrectly, but my list has no GUID assigned to it so I have to use its name. Thanks.

    //Add Web Part to Setup.aspx
    SPList tasksList = Web.Lists["Tasks"];
    SPView myTasksView = tasksList.Views["My Tasks"];

    ListViewWebPart myTasksWP = new ListViewWebPart();
    myTasksWP.ListName = “Tasks”;
    myTasksWP.ListViewXml = myTasksView.SchemaXml;
    myTasksWP.Title = “My Tasks”;
    if (Web.Files.Count != null)

    SPFile SetupPage= Web.Files["Lists/Site Pages/Setup.aspx"];
    User).AddWebPart(myTasksWP, “Right”, 4);


  • 8. pateketu  |  October 12, 2008 at 11:37 am


    myTasksWP.ListName = “Tasks”;

    Should point to the Guid of the List

    myTasksWP.ListName = tasksList .ID.ToString(“B”);

    It’s not possible that list does not have a GUID

  • 9. Avi  |  February 12, 2009 at 3:15 pm

    Refer following post to hide toolbar from ListViewWebPart programmatically:

  • 10. Alex Angas  |  August 5, 2009 at 11:00 am

    Nice one, this was a big help to me today. I never knew about GetUncustomizedViewByBaseViewId() before!

  • 11. Alex Angas » Showing the bulleted links list view  |  August 5, 2009 at 5:49 pm

    [...] much fighting with the SPViewCollection on the list, I discovered this post on Ketul Patel’s blog. He showed that to get a reference to this ‘hidden’ view, [...]

  • 12. moss  |  November 16, 2010 at 7:20 pm

    I want change selected view of a list view webpart from another dropdown list in another webpart or from same webpart. Any help appriciated.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Trackback this post  |  Subscribe to the comments via RSS Feed

Recent Posts



Add to Technorati Favorites



Get every new post delivered to your Inbox.

%d bloggers like this: