0

In the code below I am adding a document library to a webpart which exist in the root site. When I try to add a library that exist in the subsite, the code throws an error saying that it cannot find the library.

Is it possible to add library that exist in a subsite to a web part that exist in the root site?

public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
    SPSite site = properties.Feature.Parent as SPSite;
    using (SPWeb web = site.OpenWeb())
    {

        SPLimitedWebPartManager webparts = web.GetLimitedWebPartManager(web.Url + "/Pages/Engineering.aspx", System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared);

        //create new webpart object            
        ListViewWebPart wpNew = new ListViewWebPart();
        SPList myList = web.Lists.TryGetList("Testing");

            //set properties of new webpart object     
        wpNew.ListName = myList.ID.ToString().ToUpperInvariant();

        //set properties of new webpart object     
        wpNew.ZoneID = "Header";
        wpNew.Title = "Web Part Title";
        wpNew.ChromeState = System.Web.UI.WebControls.WebParts.PartChromeState.Normal;
        wpNew.ChromeType = System.Web.UI.WebControls.WebParts.PartChromeType.None;

        ////add new webpart object to webparts collection     
        webparts.AddWebPart(wpNew, "Header", 0);

        //update spWeb object     
        web.Update();
        //manager.SaveChanges(wp);
    }
}

Edit 1

        SPWebCollection subsites = web.GetSubwebsForCurrentUser();

        //create new webpart object            
        ListViewWebPart wpNew = new ListViewWebPart();
        //ContentByQueryWebPart wpNew = new ContentByQueryWebPart(); 
        for (int i = 0; i < subsites.Count; i++)
        {
            using (SPWeb subSiteWeb = subsites[i])
            {
                SPList CurrentList = subSiteWeb.Lists.TryGetList(listName);
                if (CurrentList != null)
                {
                    wpNew.ListName = CurrentList.ID.ToString("B").ToUpper();
                    //wpNew.ListUrl = CurrentList.ID.ToString("B").ToUpper();
                    //wpNew.WebUrl = web.ServerRelativeUrl;
                    wpNew.Title = "Web Part Title";
                    wpNew.ChromeState = System.Web.UI.WebControls.WebParts.PartChromeState.Normal;
                    wpNew.ChromeType = System.Web.UI.WebControls.WebParts.PartChromeType.None;

                    AddWebPartToPage(web, "Pages/" + pageName, "Header", 0, wpNew);
                    break;
                }
            }

        }
  • You have to define the subweb location, otherwise it's only looking at the same site level as your code. – Omegacron Apr 24 '14 at 15:16
  • yes, I have done that in using the code in Edit 1, but it says that list does not exist. The list is there actually and I can see it when I debug the code. – Imir Hoxha Apr 24 '14 at 15:20
  • I'll add an answer with a little more detail – Omegacron Apr 24 '14 at 15:26
0

If you're dealing with a static URL, then you could define the subsite in your original code by changing the line

using (SPWeb web = site.OpenWeb())

to something like this:

using (SPWeb web = site.OpenWeb("http://siteURL/subsite"))

Otherwise, when you call web.Lists.TryGetList("Testing"); later on, it's looking for the list in the root of the site collection.

  • if you look at my code above you will see that I use TryGetList(), but still it does not work with ListViewWebPart. – Imir Hoxha Apr 25 '14 at 9:18
  • I could be wrong, but I'm pretty sure even TryGetList is still contextual. It searches for the list dynamically by name, but only within the defined web. Hopefully someone with a little more dev experience can confirm or not - I've never used TryGetList myself. – Omegacron Apr 25 '14 at 13:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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