Viewing XML data on Content by Query Web Part (CQWP)

How many times have you been tasked to customize a Content by Query Web Part in SharePoint? In my case, this task often involves customizing the XSL used by the web part. The pain is, without knowing the data we are working with, we are basically trying to aim our arrows in darkness.

If we can see the data (ie, XML) that we are dealing with, then it would become much easier to write an XSL that achieves what we want. This post aims to give you a way to dump out the XML content of the content query web part to allow you to visualize what you are working with.

To do this, first step is to download a copy of the default xsl that all CQWP uses, this usually exist in Style Library/XSL Style Sheets subfolder and is called ContentQueryMain.xsl
Download this to your local folder now, and rename it as something else, eg. ContentQueryDump.xsl

Now open the ContentQueryDump.xsl file in your favourite XSL editor or text editor, and locate the template root node

Change the inner XML of this node to dump out the xml content by surrounding it in TEXTAREA tag

<xsl:template match="/">
    <TEXTAREA style="width:200px;" rows="10">
        <xsl:copy-of select="*"/>
    </TEXTAREA>
</xsl:template>

Save this file, and upload it to the Style Library within the XSL Style Sheets folder.

Now picture this: I have a custom list in my site that contains 2 custom columns, and I want a Content by Query Web Part to display data on this list according to the XSL that I specify.

I set up a content by query web part on my home page, set the query scope to my custom list, and this is what I get:

Let us take this web part and dump its XML data on screen. Export the web part from your page, and save it locally:

Crack it open in your favourite XML Editor or text editor, then locate the property node with name attribute of MainXslLink. Put the reference to the xsl file you uploaded earlier, as follows:

<property name="MainXslLink" type="string">
    /Style Library/XSL Style Sheets/ContentQueryDump.xsl
</property>

Save this .webpart file, then import and add this web part to the page

The result is as follows:

And voila! You have your content result XML, this is what your XSL will be looking at. You can see all the attributes, which makes things much easier from here onwards.

<dsQueryResponse>
  <Rows>
    <Row Title="Item 3" FileRef="/Lists/MyList/3_.000" Author="George Lucas" Editor="George Lucas" FSObjType="0" PermMask="0x400000300c231061" ProgId="" File_x005F_x0020_Type="" File_x005F_x0020_Size="" PublishingRollupImage="" Comments="" ID="3" Modified="2011-11-02 11:33:02" Created="2011-11-02 11:33:02" _Level="1" WebId="bf499d62-67d5-46af-9746-acd74885fa45" ListId="19877173-9e7a-4543-962b-c4dc4261aceb" _x007B_94f89715_x002D_e097_x002D_4e8b_x002D_ba79_x002D_ea02aa8b7adb_x007D_="Lists/MyList/3_.000" PubDate="Wed, 02 Nov 2011 00:33:02 GMT" FileExtension="" FileSize="" DocumentIconImageUrl="/_layouts/IMAGES/icgen.gif" LinkUrl="http://localhost/Lists/MyList/3_.000" Style="Default" GroupStyle="DefaultHeader" __begincolumn="True" __begingroup="False"></Row>
    <Row Title="Item 2" FileRef="/Lists/MyList/2_.000" Author="George Lucas" Editor="George Lucas" FSObjType="0" PermMask="0x400000300c231061" ProgId="" File_x005F_x0020_Type="" File_x005F_x0020_Size="" PublishingRollupImage="" Comments="" ID="2" Modified="2011-11-02 11:32:47" Created="2011-11-02 11:32:47" _Level="1" WebId="bf499d62-67d5-46af-9746-acd74885fa45" ListId="19877173-9e7a-4543-962b-c4dc4261aceb" _x007B_94f89715_x002D_e097_x002D_4e8b_x002D_ba79_x002D_ea02aa8b7adb_x007D_="Lists/MyList/2_.000" PubDate="Wed, 02 Nov 2011 00:32:47 GMT" FileExtension="" FileSize="" DocumentIconImageUrl="/_layouts/IMAGES/icgen.gif" LinkUrl="http://localhost/Lists/MyList/2_.000" Style="Default" GroupStyle="DefaultHeader" __begincolumn="False" __begingroup="False"></Row>
    <Row Title="Item 1" FileRef="/Lists/MyList/1_.000" Author="George Lucas" Editor="George Lucas" FSObjType="0" PermMask="0x400000300c231061" ProgId="" File_x005F_x0020_Type="" File_x005F_x0020_Size="" PublishingRollupImage="" Comments="" ID="1" Modified="2011-11-02 11:32:30" Created="2011-11-02 11:32:30" _Level="1" WebId="bf499d62-67d5-46af-9746-acd74885fa45" ListId="19877173-9e7a-4543-962b-c4dc4261aceb" _x007B_94f89715_x002D_e097_x002D_4e8b_x002D_ba79_x002D_ea02aa8b7adb_x007D_="Lists/MyList/1_.000" PubDate="Wed, 02 Nov 2011 00:32:30 GMT" FileExtension="" FileSize="" DocumentIconImageUrl="/_layouts/IMAGES/icgen.gif" LinkUrl="http://localhost/Lists/MyList/1_.000" Style="Default" GroupStyle="DefaultHeader" __begincolumn="False" __begingroup="False"></Row>
  </Rows>
</dsQueryResponse>
Advertisements

Posted on November 2, 2011, in Sharepoint and tagged , , , , . Bookmark the permalink. 2 Comments.

  1. Patrick Beaupérin

    Hello Adi,
    Thanks for the tip unfortunately I can’t see the XML code since I get a “The web part references an untrusted XSL file. Only XSL files contained in this site’s Style Library may be referenced.” message when I insert my CQWP.
    Any idea how to overcome this problem ?
    Thank you.
    Patrick

    • Patrick, have you tried specifying the location of your Xsl using relative path instead of absolute path?
      As the message says, the Xsl needs to be within the same site collection. If relative path fails, then you might want to have a look further up the configs and see if there is any managed path created that points to the similar URL, as this could cause problems too.
      Feel free to send me a copy of your CQWP export and I’ll have a look.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: