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://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)

#3 mado on 12.02.2008 at 6:05 AM

Hi!

How do you add itemstyle.xsl to a feature?

#4 Jason on 1.30.2009 at 3:16 AM

Help, ideas...

I use:

<xsl:for-each select="@*">

<br />

Name: <xsl:value-of select="name()" />

<br />Value:<xsl:value-of select="." />

</xsl:for-each>

It is displaying the name but the value-of is always empty, yet on actual list there is data?

#5 Joe on 3.02.2009 at 9:10 AM

Hi, This looks like exactly what I want to do but the problem is I'm not sure what to save where. You say "Now just create your separate file for your XSLT and make sure to add it to you Solution and Feature." but how do I do this? I'm not sure what files I should be creating and where to create them. Is there any chance you could add some more step by steps in this blog to make it easier to follow? Thanks, Joe

#6 Gene on 3.09.2009 at 5:18 PM

He Brendon,

Nice write up... just what I wanted. Question for ya. If I have a custom.css file for this webpart, how do I assoicated the webpart with this .css file? Do you add it to the .xsl or the .webpart?

Thanks

#7 aobi on 3.11.2009 at 12:40 PM

Thanks Brendon,

Great Post - Very useful.

#8 Anthonyyy on 3.17.2009 at 1:03 PM

Hi,

Thanks for this post it's really great.

Now Can you answer to this:

I want to make a CQWP from a Blog site (which is a subsite of the current site) on the main page of the current site and display the body of the posts.

Your method work perfectly to display, title, comments or whatever you want but, I can not find any matchable value for the Body.

Any Idea?

#9 Ivo on 3.31.2009 at 9:25 AM

Hi,

I did all the steps and I still get this message:

Unable to display this Web Part. To troubleshoot the problem, open this Web page in a Windows SharePoint Services-compatibl...

I have created a copy of ItemStyle.xsl. Renamed it to ItemStyle1.xsl. Then I have created a webpart, export, add the link and upload.

I still get the same error even if the link refers back to the original ItemStyle.xsl .

Can you give me some help with it?

#10 Al on 4.05.2009 at 9:33 PM

I get the same error as Ivo. It is frustrating. Have tried doing this for 2 days now.

» 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