Freigeben über


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

Comments

  • Anonymous
    April 03, 2011
    Hi, thank you for the info but how do I avoid the following issue: andreasglaser.net/.../MOSS-2007-WCMS-development-ProvisionedFilesxml-and-strange-behavior-with-multiple-WebParts.aspx If I use this technique (SharePoint 2007) the WebPart will be added several times if you deactivate and activate the feature. Regards Andreas

  • Anonymous
    April 05, 2011
    Hi Andreas, I have not come across this issue myself. Have you experienced this on 2010, or just 2007? Does this only occur when you provision "page layouts" with pre-baked webparts, or does it happen also for plain web part pages that are not deployed to the page layouts directory? It may be worth trying this with the ID attribute set on the AllUsersWebPart nodes, failing this you could try setting the IgnoreIfAlreadyExists flag. Thanks Rob

  • Anonymous
    April 08, 2011
    The comment has been removed

  • Anonymous
    April 12, 2011
    Hi ABawa, Instead of using the AllUsersWebPart tag in the elements.xml file, use the View tag. I'll post a blogpost over the next few days on how to do this. Thanks Rob

  • Anonymous
    March 03, 2013
    The comment has been removed

  • Anonymous
    July 30, 2013
    In using the <AllUsersWebPart> approach, does this approach require code behind? Am new to this...if there is an example of this you can point me to or share, I would greatly appreciate it. Thanks :)

  • Anonymous
    March 12, 2014
    Andreas is right.. This approach would add same web part multiple times if you activating and reactivating features. Here is waldek's article but answer is coded version, not declarative version.. :)

  • Anonymous
    March 22, 2015
    If you are working with SP 2013 there is attribute "ReplaceContent ="true"" for <File>, this declarative provisioning of web part willl work then and wouldn't add multi webparts.

  • Anonymous
    August 26, 2015
    Hi, I was about to use your solution here but i found the styling of your page width made the code window truncated meaning i have to scroll right to see the end of the code lines. This is not acceptable, please update the design of your site so that i don't have to scroll ASAP. I've now had to take several moments out of my very busy schedule to give you this constructive feedback (which i will not charge you for) . Please ensure maximum efficiency from web design templates when considering the type of content which will be hosted. Regards, Frank (The (Panzer) Tank) O'Donnell