I have a list of lists and an empty XsltListViewWebPart. When the user clicks on a list in the list of lists, I'd like to show that list inside the XsltListViewWebPart. How can I change the underlying list of an XsltListViewWebPart?

This is the code I've tried so far:

using (SPSite spSiteTest = new SPSite("http://MySite"))
    using (SPWeb web = spSiteTest.OpenWeb())   
        SPList boardrooms = web.Lists["Boardrooms"];    // Boardrooms is just an arbitrary list I've chosen for testing purposes
        SPView view = boardrooms.Views["Test"];
        listViewWebPart.ListId = boardrooms.ID;
        listViewWebPart.ViewGuid = view.ID.ToString();
        listViewWebPart.XmlDefinition = view.GetViewXml();

        //update spWeb object     

The list view shows up initially, but when I click the button that this method is attached to, I get an error saying that the specified view is invalid. I've specifically designed the view (it's just all the columns and items in the list), and in the Visual Studio debugger have verified that it's been instantiated.

This is the code for my XsltListViewWebPart:

<WebPartPages:XsltListViewWebPart id="listViewWebPart" runat="server"></WebPartPages:XsltListViewWebPart>
  • Couple of questions - How did you get the object listViewWebPart, and are all of your list definitions SharePoint 2010 format? I.e., they use XSL for rendering, rather than CAML (as was the case in 2007).
    – Andy Burns
    Commented Jul 13, 2011 at 7:46
  • @Andy Burns I defined the object in XML as shown at the end of my question, and just referenced the ID there. I believe the lists are Sharepoint 2010 format; I created them using the web interface, and our site was always ver 2010, i.e. there was no upgrade from 2007.
    – nfw
    Commented Jul 13, 2011 at 16:49
  • Cool re: 2007 version lists - that one has caught me before. I understand that you /defined/ the list view web part in XML, but in your C# code somewhere you had to get an object that represented that web part - something like "XslListViewWebPart listViewWebPart = .... " I was wondering what that line was.
    – Andy Burns
    Commented Jul 13, 2011 at 17:17
  • The line doesn't exist. I am just using the reference provided by the id="listViewWebPart" attribute in the <WebPartPages:XsltListViewWebPart> element, so the object is implicitly available in my C#. I'll double check this to make sure it isn't causing any issues.
    – nfw
    Commented Jul 13, 2011 at 17:26
  • @Andy Burns Double checked. Setting the id="" attribute in XML is enough to have the object automatically created and instantiated in C# code.
    – nfw
    Commented Jul 13, 2011 at 21:06

4 Answers 4


You might want to try to set the ListUrl instead. This works for me. So, do not set the ListId and ListName. ListUrl can be something relative like 'Lists/My List'.

Regarding the view you might have an issue there as well. Try to not set the view at all - SharePoint will create a view automatically and see if the webpart works. Than you can try to get your view to work. I have been working all day on a similiar issue. The only way I could get a custom View on a XSLT List View WebPart was by defining it in the page like so:

<asp:content ContentPlaceHolderId="PlaceHolderMain" runat="server">
  <WebPartPages:WebPartZone runat="server" FrameType="None" ID="Main" Title="loc:Main">
        <WebPartPages:XsltListViewWebPart runat="server" ListUrl="Lists/My List" [abbreviated...]>
                <View Name="Some View" Type="HTML" Hidden="TRUE" TabularView="FALSE" DisplayName="" Url="View.aspx" Level="255" BaseViewID="1" ContentTypeID="0x" ImageUrl="/_layouts/images/generic.png">
                            <FieldRef Ascending="FALSE" Name="ID"/>
                        <FieldRef Name="ID"/>
                    <RowLimit Paged="TRUE">1</RowLimit>
                    <Aggregations Value="Off"/>
                    <Toolbar Type="Standard"/>

Even creating a list view WebPart via UI, choosing another view, exporting the webpart and than importing the very same file again does not work. It just shows a generic error message and creates than a default view.

Good luck!


I know your error indicates the view, but error messages can often be misleading. Try setting the ListName, ListUrl, or ListDisplayName property instead of the ListID property.

I haven't set any of these properties personally, but the MSDN Article XsltListViewWebPart and Custom List Views states the following:

One of the List* properties (ListName, ListUrl, or ListDisplayName) must be present and must specify a reference to a valid list. If more than one property is present, the order of evaluation is ListName, then ListUrl, and then ListDisplayName.


I fixed this same problem by moving the View creation outside of the context of the SPLimitedWebPartManager:

SPView view = CreateHiddenView(list);
using (SPLimitedWebPartManager manager = file.GetLimitedWebPartManager(PersonalizationScope.Shared))
        XsltListViewWebPart webpart = new XsltListViewWebPart();
        webpart.ListName = list.ID.ToString("B").ToUpperInvariant();
        webpart.TitleUrl = list.DefaultViewUrl;
        webpart.WebId = list.ParentWeb.ID;
        webpart.Title = list.Title;
        webpart.ViewGuid = view.ID.ToString("B").ToUpperInvariant();
        manager.AddWebPart(webpart, "Right", 1);

For more details, see “The specified view is invalid” in call to LimitedWebPartManager.AddWebPart in SharePoint 2010.


Just set WebId of XsltListViewWebPart like this:

xsltWebpart.WebId = web.ID;
xsltWebpart.ListId = list.ID;
xsltWebpart.XmlDefinition = view.GetViewXml();
xsltWebpart.ViewGuid = ViewId;  

It will fixing your problem but still you will get lots of other problems like, if you go to a Folder, you have to set rootfolder query string to AdditionalQueryString property of xslt webpart in order to paging works correctly and all of this possible if your folder url is in current web and if not you will get following error:

No item exists at blahblah/sites/lab2_4/newssamplefa/Pages/testsub.aspx?Paged=TRUE&p_SortBehavior=0&p_FileLeafRef=news.cmp&p_Created=20140514 09:34:59&p_ID=2&PageFirstRow=2&listname={d04e2a4e-6e11-4da1-b5f1-8a89c833c06e}&RootFolder=/sites/lab2_4/Shared Documents/NewFolder&&View={F3ED7087-E6B7-43E8-8018-D485C1B3BFFF}. It may have been deleted or renamed by another user.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.