Quick Tip: Provisioning Web Parts to a Page

When deploying a page to SharePoint through a Modules feature, there are several options adding web parts to this. These are as follows:

  1. Add the web parts to the page programmatically via a feature receiver. This approach can be done quickly and easily if you are familiar with coding, however if the feature receiver fails, the web part page may continue to be checked out.
  2. Embed references to the web parts directly in the mark-up of the aspx page. The main problem with this approach is that once the page is deployed, the page is still “ghosted”. This means that the normal drop down to “Edit Web Part” is not shown, blocking future modifications to the page.
  3. The third approach (the preferred one), is to embed the webpart markup in “AllUsersWebPart” nodes in the modules elements.xml file (as shown in the example below).
 <?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="https://schemas.microsoft.com/sharepoint/">
  <Module Name="XYZCustomPages" RootWebOnly="True">
    <File Path="XYZCustomPages\Home.aspx" Url="Home.aspx" IgnoreIfAlreadyExists="true">
      <AllUsersWebPart WebPartZoneID="Left" WebPartOrder="1">
        <![CDATA[              
        <?xml version="1.0" encoding="utf-8"?>
        <webParts>
          <webPart xmlns="https://schemas.microsoft.com/WebPart/v3">
            <metaData>
              <type name="SolutionX.ProjectY.WebParts.MyWebPart, $SharePoint.Project.AssemblyFullName$" />
              <importErrorMessage>$Resources:core,ImportErrorMessage;</importErrorMessage>
            </metaData>
            <data>
              <properties>
                <property name="Title" type="string">My WebPart</property>
                <property name="Description" type="string">My WebPart for XYZ</property>
                <property name="ListName" type="string">ExampleList</property>
                <property name="RowLimit" type="string">5</property>
              </properties>
            </data>
          </webPart>
        </webParts>
        ]]>
      </AllUsersWebPart>
    </File>
  </Module>
</Elements>

Cheers to Pete Mellish and Amardeep Bhogal, who i kicked around the pro's and con's with.

Rob Nowik

Senior Consultant

Microsoft Consulting Services UK

Click here to read my bio