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.


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.

  • 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 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.