SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

This is not working....

SPLimitedWebPartManager webpartManager = webPartPage.GetLimitedWebPartManager(PersonalizationScope.Shared);
PageViewerWebPart pvwp = new PageViewerWebPart();
pvwp.Title = "My Page View WP";
webpartManager.AddWebPart(pvwp, "TOP", 0);
share|improve this question
WebPartPage class does not have GetLimitedWebPartManager method. From where do you get your webPartPage object? – Andrey Markeev May 13 '11 at 18:02
SPFile also has a GetLimitedWebPartManager method, so I'm assuming webPartPage is the aspx page. – Laurie May 13 '11 at 18:16
@Laurie: you are correct . SPFile webPartPage = "mypage.aspx"; – Roxanne May 13 '11 at 18:19
Did you mean SPFile webPartPage = web.GetFile("mypage.aspx")? :) – Andrey Markeev May 13 '11 at 18:25
up vote 1 down vote accepted

Looks like you may be missing a call to SPLimitedWebPartManager.SaveChanges. As @omlin points out, this isn't actually necessary.

Here is the code I'm using which is working fine:

using (SPSite site = new SPSite("http://servername/sites/test/"))
    SPWeb web = site.OpenWeb();
    SPFile webPartPage = web.GetFile("Pages/Default.aspx");
    SPLimitedWebPartManager webPartManager = webPartPage.GetLimitedWebPartManager(System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared);
    PageViewerWebPart pvwp = new PageViewerWebPart();
    pvwp.Title = "My Page Viewer Web Part";
    webPartManager.AddWebPart(pvwp, "TOP", 0);

    // not actually needed

Things to check:

  • Does your page need to be checked out? (Check webPartPage.CheckOutStatus)
  • Do you have permission?
  • Are you receiving an error?


share|improve this answer
Have just tested: you don't need to call SaveChanges when adding a new webpart... – Andrey Markeev May 13 '11 at 18:06
@omlin: so what am I missing. Still not working for me. Can you possibly post your code? Thanks – Roxanne May 13 '11 at 18:23
@Roxanne, I have code similar to yours, and it works fine :( I tested with both SPFile.GetLimitedWebPartManager and SPWeb.GetLimitedWebPartManager - and even if you're providing non-existant webpart zone, it works (adds the webpart to the end of last webpart zone on the page). – Andrey Markeev May 13 '11 at 18:29
Hmm.. you are right as always @omlin. :) The only time I can get it to fail is if the page isn't checked out. – Kit Menke May 13 '11 at 18:33
That could be the reason:) I only have SharePoint Foundation at home, and cannot test publishing pages. – Andrey Markeev May 13 '11 at 18:36

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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