سؤال

I'm developing a web part in which the Announcements list of a subsite will be displayed on its parent. Here's what I have so far:

XsltListViewWebPart XLV = new XsltListViewWebPart();
SPWeb web1 = SPContext.Current.Site.OpenWeb("clients/FooFactory");
SPList lists = web1.Lists["Announcements"];
XLV.ListId = lists.ID;
XLV.ViewGuid = lists.DefaultView.ID.ToString();
this.Controls.Add(XLV);

When I debug, I know it can find the site. I know it can find the list. But, it bombs on this.Controls.Add(XLV). Here's my RTE:

List does not exist.

The page you selected contains a list that does not exist. It may have been deleted by another user.

Presumably it's because the site that I'm on doesn't know about the List as belonging to THAT site. But, my whole aim is to display the list of another site onto THIS site. What can I do?

Just to Reiterate: The custom Web Part is being displayed on a Parent Site, and is intended to display a list that exists on a subsite which inherits permissions.

هل كانت مفيدة؟

المحلول

Figured it out...

Apparently, you also have to let the XsltListViewWebPart object know what site you're looking at. In order to do that, I added the line XLV.WebId = web1.ID; Here's my new code:

        XsltListViewWebPart XLV = new XsltListViewWebPart();
        Label errorLabel = new Label() { Text = "No Client Selected" };
        string clientSite = Page.Request.QueryString["client"];
        if (clientSite != null)
        {
            SPWeb web1 = SPContext.Current.Site.OpenWeb("clients/" + clientSite);
            SPList lists = web1.Lists["Announcements"];
            XLV.WebId = web1.ID;
            XLV.ListId = lists.ID;
            XLV.ViewGuid = lists.DefaultView.ID.ToString();
            this.Controls.Add(XLV);
        }
        else
        {
            this.Controls.Add(errorLabel);
        }
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى sharepoint.stackexchange
scroll top