Saturday, October 4, 2014

Provision custom web parts from sandbox solution and add them on page declaratively in Sharepoint

We all know how to add custom web parts on the publishing page declaratively in farm solution. In order to do it first of all we need to create module and provision page itself. In the <File> element for the page we may add web parts (OTB or custom) using <AllUsersWebPart> element:

   1: <Module Name="Home" Url="$Resources:osrvcore,List_Pages_UrlName;" Path="">
   2:         <File Path="default.aspx" Url="update.aspx" Type="GhostableInLibrary" >
   3:           <Property Name="Title" Value="Update" />
   4:           <Property Name="PublishingPageLayout"
   5: Value="~SiteCollection/_catalogs/masterpage/Foo.aspx, Foo;" />
   6:           <Property Name="ContentType"
   7: Value="$Resources:cmscore,contenttype_welcomepage_name;" />
   8:           <Property Name="PublishingPageContent" Value="" />
   9:           <AllUsersWebPart WebPartZoneID="BottomLeftZone" WebPartOrder="1">
  10:             <![CDATA[
  11: <webParts>
  12:   <webPart xmlns="http://schemas.microsoft.com/WebPart/v3">
  13:     <metaData>
  14:       <type name="Foo.MyWebPart, ..." />
  15:       <importErrorMessage>Cannot import this Web Part.</importErrorMessage>
  16:     </metaData>
  17:     <data>
  18:       <properties>
  19:         <property name="Title" type="string">Update helper web part</property>
  20:       </properties>
  21:     </data>
  22:   </webPart>
  23: </webParts>
  24:             ]]>
  25:           </AllUsersWebPart>
  26:         </File>
  27: </Module>

However the same approach won’t work in sandbox solutions because in this case assembly is not installed to the GAC and instead of web part you will see error message that type of your web part is not found. In order to provision custom web parts declaratively from sandbox solution we need to add <Solution SolutionId=”…” xmlns=xmlns="http://schemas.microsoft.com/sharepoint/" > under <metaData> element. SolutionId attribute should have id of your sandbox solution which you may find in Visual Studio with opened solution, then Package > Package.package > Manifest > copy SolutionId from here. Here is how module should look like in order to work with sandbox solution:

   1:  
   2: <Module Name="Home" Url="$Resources:osrvcore,List_Pages_UrlName;" Path="">
   3:         <File Path="default.aspx" Url="update.aspx" Type="GhostableInLibrary" >
   4:           <Property Name="Title" Value="Update" />
   5:           <Property Name="PublishingPageLayout"
   6: Value="~SiteCollection/_catalogs/masterpage/Foo.aspx, Foo;" />
   7:           <Property Name="ContentType"
   8: Value="$Resources:cmscore,contenttype_welcomepage_name;" />
   9:           <Property Name="PublishingPageContent" Value="" />
  10:           <AllUsersWebPart WebPartZoneID="BottomLeftZone" WebPartOrder="1">
  11:             <![CDATA[
  12: <webParts>
  13:   <webPart xmlns="http://schemas.microsoft.com/WebPart/v3">
  14:     <metaData>
  15:       <type name="Foo.MyWebPart, ..." />
  16:       <importErrorMessage>Cannot import this Web Part.</importErrorMessage>
  17:       <Solution SolutionId="..."
  18: xmlns="http://schemas.microsoft.com/sharepoint/" />
  19:     </metaData>
  20:     <data>
  21:       <properties>
  22:         <property name="Title" type="string">Update helper web part</property>
  23:       </properties>
  24:     </data>
  25:   </webPart>
  26: </webParts>
  27:             ]]>
  28:           </AllUsersWebPart>
  29:         </File>
  30: </Module>

After that your web part will be shown on the page.

No comments:

Post a Comment