0

I have the following code below and I would like to add a web part to a page. Now my question is how can I associate the web part with the document library that I would like to show on the page? Any idea? thank you

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();



//Editting 1
 //-----------------------------------
            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 2:

Now I am getting the following error:

Error occurred in deployment step 'Activate Features': The file is not checked out.  You must first check out this document before making changes.
3
  • You could set the ListId or ListName of the ListViewWebPart (both need the GUID of the list though)
    – tweytjens
    Commented Apr 24, 2014 at 8:17
  • Please look up at my code where it says Editting 1. The code that I added gives me this error: >> Error occurred in deployment step 'Activate Features': The file is not checked out. You must first check out this document before making changes. 0 0 SharePointProject1
    – Imir Hoxha
    Commented Apr 24, 2014 at 8:24
  • I think you should check out the Engineering.aspx page before being able to add the web part to the page, alternatively, you could remove the requirement to check out items before editing from the Pages library
    – tweytjens
    Commented Apr 24, 2014 at 8:41

2 Answers 2

1

You need to set the ListName property of ListViewWebPart.

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();

        //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;

        SPList list = web.List["ListName"];
        wpNew.ListName = list.ID.ToString("B").ToUpper();       
        wpNew.Title = "List Title";
        ////add new webpart object to webparts collection     
        webparts.AddWebPart(wpNew, "Header", 0);

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

See this for more info: http://sharepointnadeem.blogspot.com/2012/08/programatically-add-xsltlist-view.html

1
  • hi Nadeem, I used your example and I am getting the following error: Error 1 Error occurred in deployment step 'Activate Features': The file is not checked out. You must first check out this document before making changes.
    – Imir Hoxha
    Commented Apr 24, 2014 at 8:59
0

somthing on the lines of:

    public override void FeatureActivated(SPFeatureReceiverProperties properties)
    {
        SPSite site = properties.Feature.Parent as SPSite;
        using (SPWeb web = site.OpenWeb())
        {
            //get the aspx file
            SPFile spfile =web.GetFile(web.Url + "/Pages/Engineering.aspx");        
            //we need to checkout the file before we make changes
            spfile.CheckOut();

            //create new webpart object            
            ListViewWebPart wpNew = new ListViewWebPart();

            //set properties of new webpart object     
            wpNew.Title = "Web Part Title";
            wpNew.ChromeState = System.Web.UI.WebControls.WebParts.PartChromeState.Normal;
            wpNew.ChromeType = System.Web.UI.WebControls.WebParts.PartChromeType.None;
                                 //call method to add webpart to page!
            AddWebPartToPage(web,web.Url + "/Pages/Engineering.aspx", "Main", 0,wpNew);
            //checkin file once finished
            spfile.CheckIn("Check back in Comment goes here");

        }
    }

    public void AddWebPartToPage(SPWeb web, string pageUrl, string zoneID, int zoneIndex, ListViewWebPart wpNew)
    {
        using (SPLimitedWebPartManager manager = web.GetLimitedWebPartManager(pageUrl, PersonalizationScope.Shared))
        {
            manager.AddWebPart(wpNew, zoneID, zoneIndex);
        }
    }

I use this method but had to ammend it to suit your needs! I made a method to add any webpart on any site... but iv changed it so its a bit more simple.

3
  • Thanks for the code but now I am getting this error: Error 1 Error occurred in deployment step 'Activate Features': The file is not checked out. You must first check out this document before making changes.
    – Imir Hoxha
    Commented Apr 24, 2014 at 8:58
  • just update the answer!
    – Ali Jafer
    Commented Apr 24, 2014 at 10:06
  • Displaying the library on a web part page in the same site is working fine, but now I need to display a document library found in a the subsite in the root site. It is not working with ListViewWebPart. Any idea?
    – Imir Hoxha
    Commented Apr 24, 2014 at 14:30

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.