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.

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

2 Answers 2

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

2
  • I tried this solution but it does not work for me. Commented Sep 28, 2015 at 8:13
  • what did you used in place of "Right"? Commented Sep 28, 2015 at 8:45

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.