Provision publishing page with webpart in sandboxed solution

While working with Sandboxed solutions in SharePoint 2010 I faced a problem with attaching a webpart to a publishing page.

The code I used for deploying a page:

1: «/span>Elements xmlns="http://schemas.microsoft.com/sharepoint/“>

2: «/span>Module Name="PageInstance"

3: Url="Pages"

4: Path="PageInstance">

5: 

6: «/span>File Url="test.aspx" Path="default.aspx" Type="GhostableInLibrary" IgnoreIfAlreadyExists="TRUE">

7: «/span>Property Name="Title" Value="Test Title" />

8: «/span>Property Name="PublishingPageLayout" Value="~SiteCollection/_catalogs/masterpage/customLayout.aspx, My Custom Layout" />

9: «/span>Property Name="ContentType" Value="$Resources:cmscore,contenttype_pagelayout_name;“ />

10: «/span>Property Name="PublishingPreviewImage" Value="~SiteCollection/_catalogs/masterpage/$Resources:core,Culture;/Preview Images/ArticleLinks.png, ~SiteCollection/_catalogs/masterpage/$Resources:core,Culture;/Preview Images/ArticleLinks.png" />

11: «/span>Property Name="PublishingAssociatedContentType" Value=”;#CustomLayout - CustomPages;#0x010100C568DB52D9D0A14D9B2FDCC96666E9F2007948130EC3DB064584E219954237AF3900242457EFB8B24247815D688C526CD44D00AC43246F7BB54A1389CAF0BEC1C1AEE4;#“/>

12: «/span>AllUsersWebPart WebPartOrder="1" WebPartZoneID="Main">

13: <![CDATA[

14: «/span>webParts>

15: «/span>webPart xmlns="http://schemas.microsoft.com/WebPart/v3">

16: «/span>metaData>

17: «/span>type name="CustomLayout.SandboxedVisualWebPart1.SandboxedVisualWebPart1, CustomLayout, Version=1.0.0.0, Culture=neutral, PublicKeyToken=9242a569ebe2d353" />

18: «/span>importErrorMessage>$Resources:core,ImportErrorMessage;</importErrorMessage>

19: «/span>Solution SolutionId="245ba111-9620-424c-9a8f-9eceaa2d33c6" xmlns="http://schemas.microsoft.com/sharepoint/“ />

20: </metaData>

21: «/span>data>

22: «/span>properties>

23: «/span>property name="Title" type="string">SandboxedVisualWebPart1</property>

24: «/span>property name="Description" type="string">My custom sandbox web part</property>

25: </properties>

26: </data>

27: </webPart>

28: </webParts>

29: ]]>

30: </AllUsersWebPart>

31: </File>

32: </Module>

33: </Elements>

The problems with this in a sandboxed solution are:

1. Approval state is set to draft

2. Webpart is not deployed

I realized that there is a good reason that there are problems using this approach because SharePoint will try to load “default.aspx” which is stored in the “SharePoint Root\TEMPLATE\FEATURES\FEATURENAME folder, and Sandbox Solutions doesn’t have access to the SharePoint root.

 

 

On MSDN I found that a module element accepts the “SetupPath” property (http://msdn.microsoft.com/en-us/library/ms460356.aspx) now I rewrote my feature with the “SetupPath” property.

1: «/span>Elements xmlns="http://schemas.microsoft.com/sharepoint/“>

2: «/span>Module Name="PageInstance"

3: Url="Pages"

4: SetupPath="SiteTemplates\SPS">

5: 

6: «/span>File Url="test.aspx" Path="default.aspx" Type="GhostableInLibrary" IgnoreIfAlreadyExists="TRUE">

7: «/span>Property Name="Title" Value="Test Title" />

8: «/span>Property Name="PublishingPageLayout" Value="~SiteCollection/_catalogs/masterpage/customLayout.aspx, My Custom Layout" />

9: «/span>Property Name="ContentType" Value="$Resources:cmscore,contenttype_pagelayout_name;“ />

10: «/span>Property Name="PublishingPreviewImage" Value="~SiteCollection/_catalogs/masterpage/$Resources:core,Culture;/Preview Images/ArticleLinks.png, ~SiteCollection/_catalogs/masterpage/$Resources:core,Culture;/Preview Images/ArticleLinks.png" />

11: «/span>Property Name="PublishingAssociatedContentType" Value=”;#CustomLayout - CustomPages;#0x010100C568DB52D9D0A14D9B2FDCC96666E9F2007948130EC3DB064584E219954237AF3900242457EFB8B24247815D688C526CD44D00AC43246F7BB54A1389CAF0BEC1C1AEE4;#“/>

12: «/span>AllUsersWebPart WebPartOrder="1" WebPartZoneID="Main">

13: <![CDATA[

14: «/span>webParts>

15: «/span>webPart xmlns="http://schemas.microsoft.com/WebPart/v3">

16: «/span>metaData>

17: «/span>type name="CustomLayout.SandboxedVisualWebPart1.SandboxedVisualWebPart1, CustomLayout, Version=1.0.0.0, Culture=neutral, PublicKeyToken=9242a569ebe2d353" />

18: «/span>importErrorMessage>$Resources:core,ImportErrorMessage;</importErrorMessage>

19: «/span>Solution SolutionId="245ba111-9620-424c-9a8f-9eceaa2d33c6" xmlns="http://schemas.microsoft.com/sharepoint/“ />

20: </metaData>

21: «/span>data>

22: «/span>properties>

23: «/span>property name="Title" type="string">SandboxedVisualWebPart1</property>

24: «/span>property name="Description" type="string">My custom sandbox web part</property>

25: </properties>

26: </data>

27: </webPart>

28: </webParts>

29: ]]>

30: </AllUsersWebPart>

31: </File>

32: </Module>

At this point Path=”default.aspx” is pointing to the physic location: “SharePoint Root\TEMPLATE\SiteTemplates\SPS\default.apx”, so the “default.aspx” file in the module can be removed.

The file “default.aspx” as a template file for a publishing page.

After deploying the feature with this module the state of the page is Approved and the Webpart is available on the page.