Sandboxed WebPart on .aspx page using replaceable parameters in Sharepoint 2010

Although it is not possible to create real (in _layouts folder) application pages in sanboxed solutions, there is a way to create site pages with codebehind – basically just .aspx page that renders webpart. Page is deployed as a part of a module, and all the logic is located in the wepart.

There are some examples how to accomplish that.
One thing – there is no simple .aspx page template in VS 2010, that can be deployed in module. I am using CKS: Development Tools Edition. It adds site page template and some more, which is nice.

Another thing – to render WebPart, you have to specify AssemblyFullName, SolutionId and TypeFullName. Finding these values every time is somewhat annoying.

Here come Visual Studio Replaceable parameters. See the whole list.

For our webpart we need AssemblyFullNameand SolutionId. And we get them:

    <WebPartPages:SPUserCodeWebPart ID="MyAppWebPart"
      runat="server" 
      AssemblyFullName="$SharePoint.Project.AssemblyFullName$"
      SolutionId="$SharePoint.Package.Id$"
      TypeFullName="AppPagesTest.AppPagesWebPart.AppPagesWebPart">
    </WebPartPages:SPUserCodeWebPart>

We can also use replaceable parameter for TypeFullName. For that we need to add Guid parameter to the WebPart class:

using System.Runtime.InteropServices;

Parameter is located in System.Runtime.InteropServices namespace, so we add it.

    [Guid("72e3609b-0350-4821-839a-02658b5fc008")]
    public class AppPagesWebPart : WebPart
    {
        ...
    }

And the definition becomes

    <WebPartPages:SPUserCodeWebPart ID="MyAppWebPart"
      runat="server" 
      AssemblyFullName="$SharePoint.Project.AssemblyFullName$"
      SolutionId="$SharePoint.Package.Id$"
      TypeFullName="$SharePoint.Type.72e3609b-0350-4821-839a-02658b5fc008.FullName$">
    </WebPartPages:SPUserCodeWebPart>

Replacement of the Type token is useful if it is possible that the namespace or WepPart class name will change.

This entry was posted in Sharepoint and tagged , , . Bookmark the permalink.

One Response to Sandboxed WebPart on .aspx page using replaceable parameters in Sharepoint 2010

  1. Pingback: jesse

Leave a Reply

Your email address will not be published. Required fields are marked *