Workflow, Collaboration, Enterprise Content Management

A Tip For SharePoint Developers: XSLT Is Your Friend

by John Holliday 14. October 2007 16:58

Consider the following scenario:

You're building a SharePoint Feature in Visual Studio and you need to add a couple of custom content type definitions to the project.  The first thing you have to do is specify the site columns for the content type.

Although SharePoint makes it easy to create a new content type from the user interface, building one from scratch requires a little more effort.  Adding fields through the user interface is just a matter of browsing through the available fields and selecting the ones you want.  Doing it in XML means you have to know the unique field identifier and field name.  This information can be difficult to find, even if you know which file it's declared in.

Let's say you want to define a new content type named "Resume" that includes some of the built-in site columns like "Birthday" and "CellPhone".  Your content type definition would look something like this:

One approach is to do it in two steps.  Step 1 - create the content type via the UI in a dummy site.  Step 2 - use a command-line utility or a custom STSADM command to extract the content type definition and then add the generated XML to your project.  Granted, this works, but it's a lot to go through when you just need to add a couple of fields to your content type definition.

Here is another approach that you might find useful, at least for the built-in site columns that come with the platform.

The built-in site columns are declared in an XML file named "fieldswss.xml", which is located in the 12\TEMPLATE\FEATURES\fields folder.  If you take a look at this file, you'll see that it has over 4000 lines of XML.  Even if you know the name of the field you're looking for, you'll spend a good deal of time searching through the file to find the ID and display name you need.

fieldswss-xml

 

TIP: Transform the file using XSL so you don't have to fool around with the raw XML.

Here are the steps:

  1. Start by COPYING this file to a separate folder, like C:\TEMP.  (It's always good to make a copy of files you are working with in the 12 hive!). 
  2. Create a little XSL stylesheet like the following.  [ Instead of simply displaying the field identifier, you can generate the full declaration needed when adding the field to a custom content type. ]
    <?xml version="1.0" encoding="utf-8"?>
    <xsl:stylesheet version="1.0"
    xmlns:xsl=http://www.w3.org/1999/XSL/Transform
    xmlns:wss="http://schemas.microsoft.com/sharepoint/">
    <xsl:output method="html" version="1.0" encoding="utf-8" indent="yes"/>
    <xsl:template match="wss:Elements">
    <html>
    <body>
    <h2>SharePoint 3.0 Built-In Fields</h2>
    <table border="0" width="100%" style="font-size:9pt;">
    <tr bgcolor="#9acd32">
    <th align="left">Field</th>
    <th align="left">Group</th>
    <th align="left">Type</th>
    <th align="left">Declaration</th>
    </tr>
    <xsl:apply-templates>
    <xsl:sort select="@Name"/>
    </xsl:apply-templates>
    </table>
    </body>
    </html>
    </xsl:template>
    <xsl:template match="wss:Field">
    <tr>
    <td><xsl:value-of select="@Name"/></td>
    <td><xsl:value-of select="@Group"/></td>
    <td><xsl:value-of select="@Type"/></td>
    <td> <!-- Emit the Full Declaration Here -->
    &lt;FieldRef ID="<xsl:value-of select="@ID"/>"
    Name="<xsl:value-of select="@Name"/>"
    DisplayName="<xsl:value-of select="@DisplayName"/>" /&gt;

    </td>
    </tr>
    </xsl:template>
    </xsl:stylesheet>
  3. Save the file into the same folder as your copy of fieldswss.xml.  In this example, I've named it fieldswss.xsl.
  4. Open the fieldswss.xml in Visual Studio.
  5. Open the Properties tool window, select the Stylesheet property and browse to the fieldswss.xsl file you just created. 

    fieldswss-properties
  6. Finally, from the XML menu in Visual Studio, select "Show XSLT Output".

    fieldswss-showxslt

Voila!  You now have a nicely formatted table of built-in fields.  Now all you have to do is copy and paste the fieldref declaration into your content type definition.  What's nice is that you don't have to jump back and forth between the IDE and a separate application.

fieldswss-results

This is just one example of how you can use XSL effectively when building SharePoint solutions.  Other areas include site definitions, list definitions and control templates.  The ability to transform the templates from within Visual Studio makes it especially nice because you don't have to bounce back and forth between the UI and the IDE.  Once the transformation has been done, you can simply save the generated HTML file for future reference. 

And if you're feeling really lazy, here is a link to the generated table of built-in fields.  You can download the stylesheet from here.

Enjoy.

Technorati tags: , , ,

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

CAML | SharePoint Development

Comments

4/5/2008 1:59:05 PM

ronny De Decker

I googled for CAML end hit you
I saw your CAML just now and wondered what namespace do I have to use in XSLT to use ur CAML, if it is free.


I am since recently involved in Sharepoint development( starter).
And I have been using XSLT quite extensively the last 4 yesrs, it is flexible
for instance for a Simple upload of files of which the meta data is stored in an XML
I construct the SOAP-envelope in XSLT.

It is the start of an I hope interesting SSharepoint-adventure.

Compliments for Your work by the way.

ronny De Decker

5/13/2008 8:04:26 PM

Tomas Beijar

It's nice to find these kind of blogs and tips. This one saved my day so thank's a lot! Smile

Tomas Beijar

5/14/2008 5:16:54 AM

John Holliday

Glad to be of help, Tomas!

John Holliday

Add comment


(Will show your Gravatar icon)  

  Country flag

biuquote
  • Comment
  • Preview
Loading



Copyright © 2005-2008, John F. Holliday
This work is licensed under a Creative Commons License Powered by BlogEngine.NET 1.4.0.0

About Me

John Holliday

Independent author, consultant, trainer, and software developer specializing in enterprise content management, collaboration, workflow and business process automation. SharePoint training for developers and administrators

 

Recent comments

Comment RSS