Login

Connecting the CQWP without changing ItemStyle.xsl


Reading over Liam's post on a good tip for using the Content Query Web Part (CQWP) to connect your XSLT styles by putting a reference in the ItemStyle.xsl file is a good idea, but it still modifies the Out of the Box (OOB) ItemStyle.xsl.  In many cases, in large companies you can't change the OOB files and this file is no exception, especially since your changes would be overwritten by any product that changed the file also.

The approach to use if you are using a different style than the out of the box style is to deploy your own .webpart of the Content Query Web Part that references a different style sheet than the ItemStyle.xsl file.

First let's look at what the .webpart would look like, the change to notice is that we have added an import statement to the supporting XSL element not to the ItemStyle.xsl

           <webParts>
              <webPart xmlns="http://schemas.microsoft.com/WebPart/v3">
                <metaData>
                  <type name="Microsoft.SharePoint.Publishing.WebControls.ContentByQueryWebPart, Microsoft.SharePoint.Publishing, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" />
                  <importErrorMessage>Cannot import this Web Part.</importErrorMessage>
                </metaData>
                <data>
                  <properties>
                    <property name="Title" type="string">Custom XSLT Style Content Query WebPart</property>
                    <property name="Description" type="string">Adds other templates to the ItemStyle.xsl</property>
                    <property name="ItemXslLink" type="string">/Style Library/XSL Style Sheets/DevCowItemStyle.xsl</property>
                  </properties>
                </data>
              </webPart>
            </webParts>

The ItemXslLink contains a link to the XSLT file that has the item templates.  Make sure that the XSLT file is available or the webpart will display that it cannot import the XSLT file.  Also notice that the path is relative, so you will need to put your files in the based site collection or you will need a way to update the location based on sub site collections.  I couldn't find a way around this, but maybe the product team will know how.

Now just create your separate file for your XSLT and make sure to add it to you Solution and Feature.

<xsl:stylesheet
  version="1.0"
  exclude-result-prefixes="x d xsl msxsl cmswrt"
  xmlns:x="http://www.w3.org/2001/XMLSchema"
  xmlns:d="http://schemas.microsoft.com/sharepoint/dsp"
  xmlns:cmswrt="http://schemas.microsoft.com/WebParts/v3/Publishing/runtime"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt">
    <xsl:template name="ShowXML" match="Row[@Style='ShowXML']" mode="itemstyle">
        <xsl:for-each select="@*">
            <br />
            Name: <xsl:value-of select="name()" />
            <br />Value:<xsl:value-of select="." />
        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>

Here is what the drop down would now look like.  Keep in mind if you want all of the ItemStyle.xsl items to show up, you can simply copy them from the ItemStyle.xsl.  This will allow you to manage your customizations and not be impacted during changes to the ItemStyle.xsl.

xslt-display

If you have questions about how to package these files correctly or how they should be deployed, let me know and I can do a quick blog post on that also.

Digg It!  StumbleUpon  Reddit  Del.icio.us  NewsVine  Furl  BlinkList  Ma.gnolia  Technorati

» Trackback URL to this Post

http://blogs.sharepointguys.com/brendon/trackback.ashx?id=19

Comments

#1 Liam Cleary on 5.11.2008 at 4:42 AM

Hey Brendon,

Like the post. You beat me too it!! Was gnna post this one today!! :-)

Liam

#2 SR on 5.13.2008 at 8:19 AM

Good post Brendon. Was in similar situation where in my app was co-hosted with other apps and there wasnt choice of updating default Itemstyle.xsl.

In my case client insists on using CQWP itself, instead of customized CQWP. Is there a way to make CQWP to point to customitemstyle.xsl without creating new one?

How does the default CQWP knows to point to ItemStyle.xsl? ( webpart xml doesnt have any hardcoded reference)

» Trackbacks & Pingbacks

  1. Entwicklung Getting Started with SharePoint Development von Paul Andrew dazu auch SharePoint Community

    #1 SharePoint Kaffeetasse #66 — May 14, 2008 3:17 AM