PANVEGA’s Blog

DotNet Development, SharePoint Customizing, Silverlight, MS Infrastructure and other tips and tricks

SP CQWP custom ContentQueryMain.xsl

Posted by PANVEGA on March 20, 2009

I would like to get the web part title in the Contentquerymain.xsl.  I don’t want to put the title in the normal web part header, because it will modify all web parts headers and I only want to modify the CQWPs ones.

Here I described a way how to add any functionality to your CQWP. You can create your own version of the ContentQueryMain.xsl file that you can use for that specific web part.

Introduction:

While customizing the ItemStyle.xsl stylesheet is a quick and easy way to stylize your rollups and other output, it doesn’t really give you the ability to make any major changes to the structure or behavior of the CQWP. To do this, you’ll need to modify the stylesheet titled “ContentQueryMain.xsl,” which is located in the same folder as the ItemStyle.xsl file (/Style Library/XSL Style Sheets/). The problem with modifying the ContentQueryMain.xsl file is that any changes you make to this file will affect all CQWP’s site-wide. That’s not desirable in just about any situation, so you’ll have to create your own version of the ContentQueryMain.xsl file that you can use for that specific web part.

ContentQueryMain.xsl \Style Library\XSL Style Sheets\ContentQueryMain.xsl
  • Contains logic that generates the appropriate calls to the Header and Item templates for each item.
  • Contains functions that help designers modify the Item and Header XSLT transforms.
  • Receives all the content, parses it, and sends appropriate pieces to the ItemStyle and Header templates.
  • Maintains the structure of the Content Query Web Part.
  • Stores data retrieved when querying content in the path /dsQueryResponse/Rows/Row.

Solution:

The first step is to make a copy of the ContentQueryMain.xsl file. You can do this from within Microsoft SharePoint Designer 2007.

  1. Open the site you wish to modify
  2. Browse to “/Style Library/XSL Style Sheets”
  3. Make a copy of ContentQueryMain.xsl and rename it as you see fit
  4. Open the file and make any modifications that you see fit

Now that you have a working copy of the stylesheet, you’ll have to reference it in your CQWP. Browse to the page that contains the web part you wish to customize, and then go into edit mode in order to edit that particular web part.

Once in edit mode:

  1. Click on the edit menu for the CQWP that you wish to customize
  2. Click on Export…
  3. You will be prompted to save a “.webpart” file somewhere on your local machine
  4. Save the file and then open it up with SharePoint Designer

There are a number of web part properties that you can modify here within SharePoint Designer. Scroll down the file and look for the line that begins with <property name=”MainXslLink”… You will have to replace this line with the following code, replacing “MyCustomCQMain.xsl” with the name of your own file.

<property name=”MainXslLink” type=”string”>/Style Library/XSL Style Sheets/(your own file name).xsl</property>


Now save your changes, then browse back to your website and (once again in edit mode):

  1. Click on Add a Web Part
  2. Click on Advanced Web Part gallery and options (near the bottom-right of the pop-up window)
  3. At the top of the Add Web Parts task pane, click on Browse and select Import
  4. Browse to your custom .webpart file
  5. Click on Upload
  6. Select a web part zone from the “Add to:” menu and then click on Import

You should now see any changes that you have made in your custom stylesheet. Note that any subsequent changes that you make to this XSL file may not be viewable unless you go into and then out of edit mode. On publishing sites, you may have to check in your files and stylesheets to view the changes.

More Information:

http://msdn.microsoft.com/en-us/library/bb447557.aspx

http://blog.mastykarz.nl/extending-content-query-web-part-with-custom-data

About these ads

7 Responses to “SP CQWP custom ContentQueryMain.xsl”

  1. Lisa said

    I’ve followed these steps and have created a customcontentquerymain.xsl and uploaded it to my style libraty. I’ve also updated a working CBQ web part’s property to be /Style Library/XSL Style Sheets/customcontentquerymain.xsl. I’ve added this back to the web part page and now when I load the page, I get an error Error while executing web part:

    System.Xml.Xsl.XslLoadException: XSLT compile error. An error occurred at (1,469). —> System.Net.WebException: The remote server returned an error: (401) Unauthorized.

    Now, if I remove the MainXslLink and put my changes in the out of the box ContentQueryMain.xsl, the web part works fine. Any ideas on what’s causing this?

  2. love_moss_not said

    Thanks, but have you tried this on a subsite? it always complaints with the message “Unable to display this Web part…”

    Even without modifying “MainXslLink” new copy. In addition, for some idiotic reason, it decides to add “../../” to the MainXslLink property..

    sigh.. sharepoint.. gotta love it

  3. Bhargavi said

    Hey, I am facing the problem of accessing the webpart title in the ItemStyle.xsl. Is there a way i can achieve it?

    hope to get an answer
    Bhargavi

    • PANVEGA said

      read my post
      http://panvega.wordpress.com/2009/09/08/acessing-webparts-in-a-page-using-the-sp-object-model

      use the “using( SPLimitedWebPartManager webPartManager webPartManager = file.GetLimitedWebPartManager(PersonalizationScope.Shared))…..
      here you have the ability to change the webpart title for example

      Good luck

    • PANVEGA said

      Another easy way is:

      Browse to the page that contains the web part you wish to customize, and then go into edit mode in order to edit that particular web part.

      Once in edit mode:

      1. Click on the edit menu for the CQWP that you wish to customize
      2. Click on Export…
      3. You will be prompted to save a “.webpart” file somewhere on your local machine
      4. Save the file and then open it up with SharePoint Designer
      5. Change the Title in the property tag
      6. Import the webpart

      thats it

  4. Jay said

    i’ve seen this process written up a number of times now… we’ve followed the instructions… but have had no luck.

    Here’s where the problems start.
    All the walkthroughs discuss changing the line below as shown.
    /Style Library/XSL Style Sheets/contentquerymain2.xsl

    Just following that direction, the web part will import, but the web part is broken (unable to proceed or something like that)

    Looking around the web part file, we also find a long line that calls the contentquerymain.xsl
    <xsl:stylesheet xmlns:x=”http://www.w3.org/2001/XMLSchema” version=”1.0″ xmlns:xsl=”http://www.w3.org/1999/XSL/Transform” xmlns:cmswrt=”http://schemas.microsoft.com/WebPart/v3/Publishing/runtime” exclude-result-prefixes=”xsl cmswrt x” > <xsl:import href=”/Style Library/XSL Style Sheets/Header.xsl” /> <xsl:import href=”/Style Library/XSL Style Sheets/ItemStyle.xsl” /> <xsl:import href=”/Style Library/XSL Style Sheets/ContentQueryMain.xsl ” /> </xsl:stylesheet>

    If we don’t change the MainXslLink field, but change that long line to point at the copied file and import the web part, we can tell the web part is not calling the copied file….it’s still calling the original xsl file. If we change both the long line and the MainXslLink, the web part will be broken.

    this web page (http://blogs.msdn.com/ecm/archive/2006/12/09/customizing-content-query-rss-feeds.aspx) describes what appears to be our exact problem, but I don’t fully understand the work around.

    Ideas on how I implement their workaround?

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

 
Follow

Get every new post delivered to your Inbox.

%d bloggers like this: