Saturday, March 17, 2012

SharePoint Sandbox: How to specify the solution ID

When creating solutions for the SharePoint sandbox, whether it is for SharePoint 2010 or Office365, the solution ID must be given whenever some kind of code-behind is used. This is because SharePoint needs to know which assembly to use. Without the solution ID SharePoint would just look in the GAC for the assembly, which would produce a 'File could not be found' exception.

I will show how to supply the solution ID for a number of SharePoint components, and also describe a work-around in a case where we cannot specify a solution ID.

Features with feature receivers

The solution ID is inserted the same place as the definition of receiver-assembly and -class in the Feature-tag in the featrure.xml file:
<Feature ReceiverAssembly="..." ReceiverClass="..." SolutionId="MySolutionID">
  ...
</Feature>

Content types with event receivers

The solution ID is inserted as an extra SolutionID-tag in the receiver definition in the content type element manifest:
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <ContentType ...>
    <XmlDocuments>
      <XmlDocument NamespaceURI="http://schemas.microsoft.com/sharepoint/events">
        <spe:Receivers xmlns:spe="http://schemas.microsoft.com/sharepoint/events">
          <spe:Receiver>
            ...
            <spe:Assembly>...</spe:Assembly>
            <spe:Class>...</spe:Class>
            <spe:SolutionId>MySolutionID</spe:SolutionId>
          </spe:Receiver>
        </spe:Receivers>
      </XmlDocument>
    </XmlDocuments>
  </ContentType>
</Elements>

Web parts

The web part definition does not need any solution ID defined. But when adding the web part to a page through a module, the solution ID must be given as part of the inserted web part script in the module element manifest:
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <Module ...>
    <File ...>
      <AllUsersWebPart ...>
        <![CDATA[
          <webParts>
            <webPart xmlns="http://schemas.microsoft.com/WebPart/v3">
              <metaData>
                ...
                <Solution SolutionId="MySolutionID" xmlns="http://schemas.microsoft.com/sharepoint/" />
              </metaData>
            </webPart>
          </webParts>
        ]]>
      </AllUsersWebPart>
    </File>
  </Module>
</Elements>

Custom page

Unfortunately you are not able to supply a solution ID reference in the Page directive on a custom page. Doing so will result in a syntax error. If you need to deploy your own pages to the sandbox, you need to use a different approach.

If you need to add a solution reference to your page it is because you need some code-behind to run. If you don’t need code-behind then you don’t ned to reference the solution ID.

So since we cannot add a solution ID to the page reference without ruining the page, we cannot make the page inherit from a custom page class. Instead we use a built-in page, and stick a custom web part on it (which, as you can see above does allow for a solution ID.

If you don’t like the web part zones on the built-in pages there is nothing preventing you from defining your own. Just copy for example the STS default.aspx, and then change the zones. You could for example have a single large zone in which you place your web part. The point is that you ned to inherit from a page that is defined in the GAC, so with a sandboxed solution the page should inherit from for example Microsoft.SharePoint.WebPartPages.WebPartPage.

Since we don’t have the option to create a layouts page in the sandbox the above would be the way to create a sandboxed settings page.

Summary

In the above you can find examples for the SharePoint sandbox on how to specify the solution ID for:

  • Feature receivers
  • Event receivers
  • Web parts

Also, I explin how to create something that looks like a custom page with code-behind.

No comments:

Post a Comment