Monday, March 15, 2010

Display Content Query Web Part contents as Dropdown in SharePoint 2010

Objective:

To display Content Query Web Part contents as a dropdown in SharePoint 2010. The dropdown will contain list of the documents from the document library/libraries in the site.
To open up a selected document in the dropdown, create a button next to dropdown that will load the document as user clicks the button.

Solution:

A) Modify ContentQueryMain.xsl and ItemStyle.xsl

Copy XSL Files to New Location

a) Open your site in SharePoint Designer 2010.
b) Click on All Files --> Style Library --> XSL Style Sheets (if you dont find these folders then go to site collection settings and enable publishing feature on site collection level and site level)
c) Create a new folder called Dropdown under XSL Style Sheets folder so that we dont modify original xsl files and have our own customized xsl files pointing from content query web part.
d) Copy ContentQueryMain.xsl, ItemStyle.xsl and Header.xsl from XSL Style Sheets folder and paste it under Dropdown folder which you created.

Modify ContentQueryMain.xsl file

e) Open the file ContentQueryMain.xsl from Dropdown folder in edit mode.

Create Dropdown in the XSL

f) Add the code circled with red line in OuterTemplate.Body template before for loop

g) Add the code circled with red line at the end of OuterTemplate.Body template after closing tag of for loop




h) Add the code circled with red line in OuterTemplate.CallItemTemplate


i) Close ContentQueryMain.xsl file. We dont need any further modifications in it.

Modify ItemStyle.xsl file

j) Open the file ItemStyle.xsl from Dropdown folder in edit mode.

Propogate items in Dropdown

k) Copy the complete 'TitleOnly' template and paste it at the end of the file to create your own style and change/add the code circled with red line. You can see that in the image below. Make the template look as shown below:

l) Close the ItemStyle.xsl file. We dont need any further modifications in it.


B) Assign the customized XSL files to Content Query Web Part.

a) Add a CQWP on any page on your site.

b) Edit Web Part properties and assign Source, List Type, Content Type etc to display items.

c) After applying changes, you can see the list of the documents in CQWP.

d) Export the web part and save the file.

e) Open the file in any editor and change the following properties as shown in the image below.

f) You can change other properties as required. i.e. Title, Content Type etc.

g) Save the file then import the web part on any page on the site.

h) You might have to change the properties of the web part if 'Go' button redirects to another page and does not open the document.

1) Edit Web Part
2) Open 'Presentation' details.
3) On Link textbox, enter 'URL Path;'(no single quote) and on Title textbox, enter 'Title;'(no single quote)



4) Click Ok to save the values. Select the value from dropdown then click Go. It will open up the document.


I tried to explain it in a simple way.. I hope there will be no problems implementing this solution. If you have any problems just drop me an email.

Please, do provide comments for the post.

Thanks,
JK



11 comments:

Tony said...

Hi,

Thanks for posting this solution - it is just what I am looking for to replicate the "I want to..." web part that shipped with MOSS.

However, I get this error when I add the newly imported webpart into a page:

"The web part references an untrusted XSL file. Only XSL files contained in this site's Style Library may be referenced."

I have double checked all the XSL code and also the references to the new XSL files in the webpart file.

Are you able to help at all?

thanks

Tony

Jasim Khan said...

Thanks Tony for your comment. You can try a few options.
1) Add CQWP on any page and then see if it works fine. If it displays list of documents then there is nothing wrong with your ItemStyle.xsl.
2) Create a copy of your ItemStyle.xsl and Main.xsl, rename it to anything you like and then try to use it in your exported web part file.
This way you can eliminate the issues. I implemented successfully in SharePoint 2010. Its working fine.

Thanks,
JK

Willem Oosterhof said...

Hi Jasim Khan,

I got your code to work, but after adding the exported/imported webpart I don't see any of the webparts that are below this webpart. Also I cannot edit the webpart properties. The properties are not shown anymore. It looks like the webpart does some unwanted things to the page layout. Did you experience this also?

Greetings, Willem

Anonymous said...

Hi - I am total newbie by the way.. This looks almost like what I need, but..I have a requirement from the business to change a summary link webpart so that each group is actually a dropdown, I have tried to utilised this solution on a summary link but I am failing...any pointers..

Anonymous said...

Hi Tony,

its because you are not having this in root sitecollection !!!

e.g. Sharepoint is quite stupid in this matter, so you have to specify

/SiteCollName/Style Library/Style XSL Style Sheets/ItemStyle.xsl

or use something like this :


http://solutionizing.net/2008/12/11/site-collection-safe-cqwp-xsl-links/

Tomas

Anonymous said...

I am working in SharePoint 2010 and have done everything as you outlined but continue to get the following error "Unable to display this Web Part. To troubleshoot the problem, open this Web page in a Microsoft SharePoint Foundation-compatible HTML editor such as Microsoft SharePoint Designer. If the problem persists, contact your Web server administrator."

Any ideas of what i am doing wrong?

This would be very helpful if I could het it to work. Thank you!

Anonymous said...

@anonymous - when I got that error it was caused by an error (missing losing tag) in the style sheet. Maybe that's it?

Anonymous said...

MARLBORO MAN!

Anonymous said...

If you are getting the error: Unable to display this Web Part. To troubleshoot the problem, open this Web page in a Microsoft SharePoint Foundation-compatible HTML editor such as Microsoft SharePoint Designer. If the problem persists, contact your Web server administrator.

I found that in my case I tried to comment the xsl using

Anonymous said...

Thank you for posting this.

I just wanted to post to let people know this does work. But it does need a bit more configuration to get working.

I used this instead of the large chunk of code at the end of your custom ItemXsl, bewteen the if and the a tag, just put this in(Could not show properly as cant post tags):

xsl:if
option value="{$SafeLinkUrl}" xsl:value-of select="$DisplayTitle"
option
a
div

You reference your custom stylesheets with this path:
/Style%20Library/XSL%20Style%20Sheets/ItemStyle_DropDown.xsl

If you get weird things happening on your page, remove the form tags from the Main Xsl style, fixed it for me.

Also, add the first dropdown option in the main xsl file, that way the first item displayed is a 'Select...' or whatever item.

You can alos customise the button and dropdown with css in the main xsl file.

Mine works great now!

Hope this helps someone else.

Anitha Natarajan said...

Thanks for posting this solution. This is a great start for me. But I need a bit different approach and would appreciate if you could help. I use Content Query web part to display the Wikis in a HTML gird. Now would like to add fltering for each column (something similar to the column filtering in MS Excel). How can I achieve this? I used http://paulgalvinsoldblog.wordpress.com/2007/12/09/display-content-query-web-part-results-in-a-grid-table to render the Grid view, but having difficulty getting it sorted. Any help would be appreciated. Thanks!