Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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
add comment

1 Answer

up vote 2 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. –  DeeMac May 10 '13 at 10:59
add comment

Your Answer

 
discard

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.