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

I've successfully implemented code to add web parts to web part zones in code using WebPartManager's in other projects.

Now that I'm working in a sandbox solution, VS can't seem to recognise necessary parts of the api such as SPWebPartCollection or GetLimitedWebPartManager.

My question really is why? Is this a limitation of a sandbox solution? If so, how else can I achieve the above (title)?

Just as a bit more context - this will ultimately be deployed to SharePoint Online.

share|improve this question
up vote 4 down vote accepted

As you have discovered SPLimitedWebPartManager is not availabe on Sandboxed solutions.

Snippet from Waldek Mastykarz blog:

Inconvenient provisioning Web Parts instances from Sandboxed Solutions

One of the challenges when working with Sandboxed Solutions is how to provision Web Parts instances to pages. If you have done this before in a Farm Solution you know, that in order to add a Web Part to a page programmatically in SharePoint you have to retrieve the instance of the SPLimitedWebPartManager class and use it to add the instance of a Web Part to a page. Unfortunately, as the SDK mentions, the SPLimitedWebPartManager class is not available in Sandboxed Solutions which means that you cannot programmatically add a Web Part to a page from within a Sandboxed Solution.

Luckily the programmatic approach isn’t the only option that SharePoint offers for adding Web Parts to pages. It turns out that there is a declarative approach as well, which is just perfect for when you need to provision a Web Part instance from within a Sandboxed Solution.

Please visit the blog post for more information about how you can provision webpart declaratively in modules with the AllUsersWebPart element.

share|improve this answer
Spot on answer. Thanks a lot mate. – JᴀʏMᴇᴇ May 10 '13 at 10:59
Is there any other way to add webpart to page on sandbox solution programmaticalltically using c# ?? – Ronak Patel Jan 22 '15 at 5:02

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.