Removing unwanted properties from imported Document Library contents with XSLT


In my previous post I showed you how you can import contents of a Document Library into your SharePoint Project for repeatable deployment. In this article I will show you how to remove all unwanted properties from the generated Element Manifest and keep it tidy.

While showing you the newest extension for the Visual Studio 2010 SharePoint Developer Tools that I made, I mentioned that during the import it includes all properties in the generated Element Manifest.

Element Manifest including all properties for all imported files

This is by design to give you the greatest flexibility. Depending on your scenario you might want to keep some of the properties while leave other out. And although you could clean the generated Element Manifest manually, there is a much more efficient way to do it.

Last week, when discussing another extension – Mavention Import List Instance, I showed you how to remove unwanted properties using XSLT. And although provisioning List Instance data and documents to Document Libraries is different, the XSLT used for removing unwanted properties is very similar.

You can remove unwanted properties from the generated Element Manifest using the following XSLT file:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:msxsl="urn:schemas-microsoft-com:xslt"
                xmlns:sp="http://schemas.microsoft.com/sharepoint/"
                exclude-result-prefixes="msxsl sp">
  <xsl:output method="xml" indent="yes"/>

  <xsl:variable name="IncludeProperties" select="'ContentType ContentTypeId PublishingAssociatedContentType PublishingPreviewImage MasterPageDescription'"/>

  <xsl:variable name="IncludePropertiesSet">
    <xsl:call-template name="GetIncludePropertiesSet">
      <xsl:with-param name="propertiesList" select="$IncludeProperties"/>
    </xsl:call-template>
  </xsl:variable>
  
  <xsl:template match="@* | node()">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="text()">
    <xsl:choose>
      <xsl:when test="string-length(normalize-space(self::node())) &gt; 0">
        <xsl:copy>
          <xsl:apply-templates select="@* | node()"/>
        </xsl:copy>
      </xsl:when>
      <xsl:otherwise />
    </xsl:choose>
  </xsl:template>

  <xsl:template match="sp:Property">
    <xsl:variable name="name" select="@Name"/>
    <xsl:choose>
      <xsl:when test="count(msxsl:node-set($IncludePropertiesSet)/property[@name = $name]) &gt; 0">
        <xsl:copy>
          <xsl:apply-templates select="@* | node()"/>
        </xsl:copy>
      </xsl:when>
      <xsl:otherwise />
    </xsl:choose>
  </xsl:template>

  <xsl:template name="GetIncludePropertiesSet">
    <xsl:param name="propertiesList"/>
    <xsl:variable name="trimmedPropertiesList" select="concat(normalize-space($propertiesList), ' ')"/>
    <xsl:variable name="property" select="substring-before($trimmedPropertiesList, ' ')"/>
    <xsl:variable name="remaining" select="substring-after($trimmedPropertiesList, ' ')"/>
    <property>
      <xsl:attribute name="name">
        <xsl:value-of select="$property"/>
      </xsl:attribute>
    </property>
    <xsl:if test="$remaining">
      <xsl:call-template name="GetIncludePropertiesSet">
        <xsl:with-param name="propertiesList" select="$remaining"/>
      </xsl:call-template>
    </xsl:if>
  </xsl:template>

</xsl:stylesheet>

The IncludeProperties variable in line 8 contains a space-separated list of all properties that should be included in the Element Manifest. All properties not listed in this variable will be removed during the XSL transformation.

If you compare contents of this XSLT file with the one I showed you previously, which you could use for removing unwanted fields from List Instance data, you should notice that there are very few differences and that most of them are cosmetic (ie. referring to properties instead of fields).

The way it work is exactly the same as when removing unwanted fields from List Instance data. All you have to do, is to open the XSLT file and from the XML menu choose the Start XSLT Without Debugging option. After the transformation is completed you should keep a clean Element Manifest with your properties only.

Element Manifest with specific properties only

Others found also helpful: