1

I want to add a Custom Webpart on default.aspx. The Web Part appears on the page but not on the right place. I want the webpart on the right site and on the first place. I´m using the following Code to perform this action.

using (
            SPLimitedWebPartManager webPartManager =
                adminweb.GetLimitedWebPartManager(adminweb.Url + "/default.aspx", PersonalizationScope.Shared))
        {
byte[] kennzahlenWebPart = Resources.Kennzahlen;
MemoryStream memory = new MemoryStream(kennzahlenWebPart);
XmlReader oxmlReader = new XmlTextReader(memory);
string errorMsg;
WebPart oWebPart = webPartManager.ImportWebPart(oxmlReader, out errorMsg);
webPartManager.AddWebPart(oWebPart, "Right", 0);
webPartManager.SaveChanges(oWebPart);

}

After that Code the Webpart always appears on the right site on the last place. I don´t know how to solve this issue. Hopefully someone can help me. Thanks in Advance.

  • Try changing webPartManager.AddWebPart(oWebPart, "Right", 0); to webPartManager.AddWebPart(oWebPart, "Right", 1); and let me know if it works. – Nadeem Yousuf Sep 28 '15 at 9:55
  • Thanks for your reply. I have changed it and now it is working. – Jan Sep 30 '15 at 20:40
  • I added it as answer. – Nadeem Yousuf Oct 1 '15 at 5:28
1

Change webPartManager.AddWebPart(oWebPart, "Right", 0); to webPartManager.AddWebPart(oWebPart, "Right", 1); and it should work. I faced a similar issue some time back, I have blogged about it here Web Parts not adding in proper order

0

You can change the webpart zone by changing the following line of your code

webPartManager.AddWebPart(oWebPart, "Right", 0);

you can change it as per your requirement i.e Left, etc.

EDIT

Try something like this

public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
    SPSite site = properties.Feature.Parent as SPSite;
    using(SPWeb web = site.OpenWeb())
    {
        using(SPLimitedWebPartManager wpManager = web.GetLimitedWebPartManager(page url, System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared))
        {
            YourWebPart webpart = new YourWebPart();
            webpart.ZoneID = "Top";
            webpart.Title = "My Webpart";
            wpManager.AddWebPart(webpart,"Top" , 0);
        }
    }
}

you can refer to this link too

  • I tried this solution but it does not work for me. – Jan Sep 28 '15 at 8:13
  • what did you used in place of "Right"? – Vishwas Goswami Sep 28 '15 at 8:45

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.