Provision publishing page with webpart in sandboxed solution

17. August 2010 19:59 by arnoldbo in SharePoint  //  Tags: ,   //   Comments
 

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: <Elements xmlns="http://schemas.microsoft.com/sharepoint/">
   2:   <Module Name="PageInstance"
   3:         Url="Pages"
   4:         Path="PageInstance">
   5:  
   6:     <File Url="test.aspx" Path="default.aspx" Type="GhostableInLibrary" IgnoreIfAlreadyExists="TRUE">
   7:       <Property Name="Title" Value="Test Title" />
   8:       <Property Name="PublishingPageLayout" Value="~SiteCollection/_catalogs/masterpage/customLayout.aspx, My Custom Layout" />
   9:       <Property Name="ContentType" Value="$Resources:cmscore,contenttype_pagelayout_name;" />
  10:       <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:       <Property Name="PublishingAssociatedContentType" Value=";#CustomLayout - CustomPages;#0x010100C568DB52D9D0A14D9B2FDCC96666E9F2007948130EC3DB064584E219954237AF3900242457EFB8B24247815D688C526CD44D00AC43246F7BB54A1389CAF0BEC1C1AEE4;#"/>
  12:       <AllUsersWebPart WebPartOrder="1" WebPartZoneID="Main">
  13:       <![CDATA[
  14:       <webParts>
  15:         <webPart xmlns="http://schemas.microsoft.com/WebPart/v3">
  16:           <metaData>
  17:             <type name="CustomLayout.SandboxedVisualWebPart1.SandboxedVisualWebPart1, CustomLayout, Version=1.0.0.0, Culture=neutral, PublicKeyToken=9242a569ebe2d353" />
  18:             <importErrorMessage>$Resources:core,ImportErrorMessage;</importErrorMessage>
  19:             <Solution SolutionId="245ba111-9620-424c-9a8f-9eceaa2d33c6" xmlns="http://schemas.microsoft.com/sharepoint/" />
  20:           </metaData>
  21:           <data>
  22:             <properties>
  23:               <property name="Title" type="string">SandboxedVisualWebPart1</property>
  24:               <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: <Elements xmlns="http://schemas.microsoft.com/sharepoint/">
   2:   <Module Name="PageInstance"
   3:         Url="Pages"
   4:         SetupPath="SiteTemplates\SPS"> <!-- Important for sandboxed solution!!! path in file element is using this path for finding location -->
   5:  
   6:     <File Url="test.aspx" Path="default.aspx" Type="GhostableInLibrary" IgnoreIfAlreadyExists="TRUE">
   7:       <Property Name="Title" Value="Test Title" />
   8:       <Property Name="PublishingPageLayout" Value="~SiteCollection/_catalogs/masterpage/customLayout.aspx, My Custom Layout" />
   9:       <Property Name="ContentType" Value="$Resources:cmscore,contenttype_pagelayout_name;" />
  10:       <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:       <Property Name="PublishingAssociatedContentType" Value=";#CustomLayout - CustomPages;#0x010100C568DB52D9D0A14D9B2FDCC96666E9F2007948130EC3DB064584E219954237AF3900242457EFB8B24247815D688C526CD44D00AC43246F7BB54A1389CAF0BEC1C1AEE4;#"/>
  12:       <AllUsersWebPart WebPartOrder="1" WebPartZoneID="Main">
  13:       <![CDATA[
  14:       <webParts>
  15:         <webPart xmlns="http://schemas.microsoft.com/WebPart/v3">
  16:           <metaData>
  17:             <type name="CustomLayout.SandboxedVisualWebPart1.SandboxedVisualWebPart1, CustomLayout, Version=1.0.0.0, Culture=neutral, PublicKeyToken=9242a569ebe2d353" />
  18:             <importErrorMessage>$Resources:core,ImportErrorMessage;</importErrorMessage>
  19:             <Solution SolutionId="245ba111-9620-424c-9a8f-9eceaa2d33c6" xmlns="http://schemas.microsoft.com/sharepoint/" />
  20:           </metaData>
  21:           <data>
  22:             <properties>
  23:               <property name="Title" type="string">SandboxedVisualWebPart1</property>
  24:               <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.

blog comments powered by Disqus