Monthly Archives: November 2013

Provisioning List Views in the onet.xml with custom Web Part properties

I’ve seen this come up numerous times (with a few people telling me “its not possible without writing code”) so I thought I’d chuck up a simple code sample showing you how this is done in SharePoint 2013.

Lets say you want to add some List View Web Parts to a custom Team Site for a Tasks list:

  • My Tasks
  • Tasks In Progress
  • Tasks Overdue

You would initially do this kind of thing in your onet.xml:

<View List="Lists/Tasks" BaseViewID="1" WebPartZoneID="Header" />
<View List="Lists/Tasks" BaseViewID="2" WebPartZoneID="Header" />
<View List="Lists/Tasks" BaseViewID="3" WebPartZoneID="Header" />

Of course the main problem here is that you have zero control over any of the Web Part properties (such as the Title, border, or anything else). The default title will use the same title as the list, which of course (when you have the same list more than once) offers the stunningly un-useful:

  • Tasks (1)
  • Tasks (2)
  • Tasks (3)

The solution is to include some additional Web Part properties in embedded CDATA tags:

<View List="Lists/Tasks" BaseViewID="1" WebPartZoneID="Header"> <![CDATA[

<webParts>
      <webPart xmlns="https://schemas.microsoft.com/WebPart/v3">
          <metaData>
              <type name="Microsoft.SharePoint.WebPartPages.XsltListViewWebPart, Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" />
              <importErrorMessage>Cannot import this Web Part.</importErrorMessage>
          </metaData>
          <data>
              <properties>
                  <property name="Title">My Tasks</property>

              </properties>
          </data>
      </webPart>
</webParts>
]]> </View>

This gives you effectively complete control over any of the Web Part Properties (see XsltListViewWebPart Properties).

Happy coding!