CQWP and XSLT – Part 2

So after a little prodding I finally decided to move this post up my priority list.

In Part 1 we looked at creating a custom version of the standard CQWP which uses our own XSLT files. Just as an aside, rather than export the CQWP and then open it to make changes, you can actually tell it to use your custom XSLT files through the properties settings in SPD – much easier.

So now we have our CQWP linked to the copies of the standard XSLT files and it is time to open them up to see what is inside. Start with the easy one and open up the copy of ItemStyle.xsl. I use SPD but it is just a text file so in theory could be done with any text editor. What you find is 600+ lines of stylesheet. Skip over the first few lines which is just defining namespaces and a few parameters and variables and you will get to a template.

<xsl:template name="Default" match="*" mode="itemstyle">

In total there are 14 templates in the standard ItemStyle file – if you exclude the one called HiddenSlots then they match the 13 options you get in the CQWP settings under Presentation->Styles->Item style
CQWPitemStyles

My first suggestion is to clear some space so you can focus on what is important. Delete all the existing templates and we will create our own. You can also delete the parameters and variables prior to the first template as we are not going to use those in this example. So all we are left with is the opening and closing stylesheet tags then add in the example template as below:

<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="MyStyle" match="Row[@Style='MyStyle']" mode="itemstyle">
  <div class="item">
   <xsl:value-of select="@Title" />
  </div>
 </xsl:template>
</xsl:stylesheet>

You should hopefully be able to copy and paste this as required.

The name and the @Style must be identical – including case. You also cannot have spaces in these. This name/@Style will be what you see in the CQWP dropdown as above to choose the style you want your users to see. If you have worked through my other XSL posts then the rest will look very familiar to you. Save this file and go to the page with your custom CQWP on it. Edit the web part and check out the Item style dropdown as in the image above. You should not be able to actually dropdown as there will only be the one choice – which should be the style you have just created.

Now, connect set up the Query part of the CQWP to select from a list – ideally the same one we created for one of the earlier XSL posts to keep it simple. Do not bother filtering or sorting at this stage. Scroll back down the web part settings and just underneath the Item style option you should now see Fields to display – these are also referred to as Slots. In this example there will only be the one as we only used one field in our XSLT – @Title. Here you can choose which field you use to populate @Title in the XSLT. Again in the interests of simplicity, enter Title – which may be there already.

Save your page and preview it – you should now see the Titles of the items in your list – and if you check the underlying HTML you will see that they are in DIVs where the class=”item”.

Congratulations! You now have control of your CQWP and what it presents. You can add more templates which will offer more choices of styles and you can get more adventurous with the content you present within the templates.

I will plan to add some further parts to this series – and hopefully it will not take me so long to get around to it.

Aside | This entry was posted in SharePoint and tagged , , , , . Bookmark the permalink.

12 Responses to CQWP and XSLT – Part 2

  1. David Kit says:

    I’ve also been experimenting with the CQWP and XSLT, you mentioned about Slots (this is probably something I’ll look into straight away), I noticed some ItemStyles have less slots than the default (I think it has 4, a Title, URL, Description and … Link?), if I needed to, how would I add more?

  2. SharePoint says:

    Hi Dave,

    I`m planning to use CQWP with custom XSLT as a solution to be part of a Site Template and later create multiple internal SPS2013 site based on the site template. Custom CQWP with custom link of XSLT created once and saved as a site template.
    Will it work for multiple sites created using the site template?

    Also, i`ve read all the articles on your blog and trust me, loving reading your site more then news site for the moment, as its giving me all the required knowledge to move further.

    In order to create dashboards within my published SPS 2013 site template, and use the dashboard specific to the site created in future, are there any option that doesn’t includes?

    a) performance point
    b) powerpivot

    Regards,

    Jason

    • paylord says:

      Hey Jason

      XSLT for a CQWP must be in the Style Library of that site collection. If your sites from the template will all be in the same site collection then there should be no problem with including the CQWP pointing at the custom XSLT as part of the site template. However, you may need to consider how you filter the content if you only want data from the one site.

      Dashboards rather depend where the data is coming from. You have already seen that it is possible to create simple dashboards with XSLT though this is really dependent on relatively small datasets. Similarly you can use Excel Services though to take full advantage of the API you would ideally need some JavaScript skills – though in the past I have combined XSLT with Excel Services to pass data from the list items to get a chart drawn by Excel – I think I have a blog post on that here too. PowerPivot and PerformancePoint really start to come into their own when you have larger volumes of data or want some dynamic interaction with the data. There is no reason you cannot use the same principle as the CQWP above to develop your dashboard if the data is coming from a list in the site.

      Thanks again for the positive feedback and good luck with your site templates.

      Dave

      • SharePoint says:

        Dave,

        Thanks for responding my queries till date.
        I`ve gone through the excel using REST article and will post my query on the relevant page.

        Coming to Performance Point, before I invest more time on it, just wanted a 2 cent advice.

        a) Can Dashboard created using Performance Point, be used as in a Site template model?
        b) Can Performance Point work with Published Site or does it needs BI Site Template to add Dashboard (My research suggest for Dashboard Designer is available only when you are trying to add dashboard on a BI Template in SharePoint and not sure if it works with Published Site Template?
        c) With respect to calculation, wherein let say Numeric values added on multiple fields within a column on a SPS 2013 List, In order to get summation of the value for that respective Column (I dont think Performance Point can do anything). However, Excel Web services can do the calculation by performing the calculation on a local Excel sheet stored on a document library in SPS and show the summation, which later can be used to build Dashboard?

        Once again thanks for your time and responding my queries.

        Jason

      • paylord says:

        Hey Jason

        My suggestion on PerformancePoint is to avoid it. Unless you have good BI knowledge you will likely find it hard to understand and use – in my opinion it comes near the bottom of the Microsoft products in ease of use. That said, I believe there are web parts that you can use to surface PP dashboards on non-BI sites and there is no reason it would not work in the site templates – though again you may need to do some clever stuff with filtering if you want site specific results. PerformancePoint is intended for use with data cubes which definitely do provide pre-aggregated totals. I do not believe it is as easy as you hope with Excel services though.

        Dave

  3. SharePoint says:

    Dave,

    Created CQWP using the XSLT template, what I observed Title values are picked from the desired SPS 2013 Lists, but they dont have Hyperlink enabled in order for user to click on it and reach the SPS 2013 List.

    Any suggestions?

    Jason

    • paylord says:

      Hello Jason

      Good progress🙂

      The Title value is literally just the text so if you wish to create a link then you need to do it with the XSLT. There are some examples of this on the blog.

      Keep up the good work.

      Dave

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