I hope this post will help many of you feel comfortable with using the Content Query Web Part.
In this post I will walk you through the process of creating a content query web part and configuring it to show custom field types. I will also give details on how to use XSLT to stylize and format the data being pulled.
I will be using the following scenario as an example. Say that you had a site collection with a top level publishing site. This publishing site would display a the most recent blog posts from all blog sites within its own site collection.
To solve this problem we will use a Content Query Web Part and a customized ItemStyle.xsl using XSLT.
Please note that the “SharePoint Server Publishing Infrastructure” needs to be enabled at the site collection to display the content query web part.
Step 1: Add a Content Query Web Part to Page
- Navigate to the site that you want the blog posts to show up and click on edit page.
- Under Editing Tools in the Ribbon, Click on insert > Web Part
- Under the Content Rollup Category Click on Content Query
- Then Click on the Add Button
- Now that you have a CQWP on the page edit the web part
- Expand open the “Query” properties
- Since we will be just showing blog post in this query choose the following
- List Type: Posts
- Content Type Group: List Content Types
- Content Type: Post
- You should get a result like the following.
- Next we will configure the web part to pull in additional values from the blog posts.
- Title – linked to post
- Date – with standard formatting
- Body – Rich text
- Author
- Number of Comments
- Categories
Step 2: Configure Web Part
- Click on web part arrow and choose “Export”
- Save the .webpart file to your desktop
- Edit the “Content_Query.webpart” file in Notepad
- Search for "CommonViewFields”
- Replace the default “<property name="CommonViewFields" type="string" />” with the following:
<property name="CommonViewFields" type="string">Title, Text;PublishedDate, DateTime;Body, RichHTML;Author, Text;NumComments, Lookup;PostCategory, Lookup;</property>
- Here are the available Field Types that can be used
- Text
- Note
- Number
- Currency
- Integer
- Boolean
- DateTime
- Threading
- Lookup
- Choice
- URL
- Counter
- RichHTML
- Image
- When creating your own there are a few rules.
- First off you need to separate each custom field type by a semi colon (Ex: Title, Text;Editor, Text)
- There cannot be a space after the semi colon
- Save the .webpart file
- Edit the page again and choose Inster Web Part
- It’s a little hard to find but click on the Upload a Web Part and browse for the custom .webpart file.
- Click on Upload
- Now when you click on Insert webpart you will see a category of imported webparts.
- Click on Add button.
- Now you will not see a difference but now the webpart has been configured to display these custom columns.
Step 3: Configure ItemStyle.xsl
- Click on View All Site Content > Style Library > XSL Style Sheets
- Download the itemstyle.xsl file
- Add the following to the top of the file
xmlns:ddwrt="http://schemas.microsoft.com/WebParts/v2/DataView/runtime"
- At the very bottom of the xsl file add the following template
<xsl:template name="BlogPost" match="Row[@Style='BlogPost']" mode="itemstyle">
<xsl:variable name="SafeLinkUrl">
<xsl:call-template name="OuterTemplate.GetSafeLink">
<xsl:with-param name="UrlColumnName" select="'LinkUrl'"/>
</xsl:call-template>
</xsl:variable>
<xsl:variable name="DisplayTitle">
<xsl:call-template name="OuterTemplate.GetTitle">
<xsl:with-param name="Title" select="@Title"/>
<xsl:with-param name="UrlColumnName" select="'LinkUrl'"/>
</xsl:call-template>
</xsl:variable>
<div class="custom_posttitle">
<xsl:call-template name="OuterTemplate.CallPresenceStatusIconTemplate"/>
<a href="{$SafeLinkUrl}" title="{@LinkToolTip}">
<xsl:if test="$ItemsHaveStreams = 'True'">
<xsl:attribute name="onclick">
<xsl:value-of select="@OnClickForWebRendering"/>
</xsl:attribute>
</xsl:if>
<xsl:if test="$ItemsHaveStreams != 'True' and @OpenInNewWindow = 'True'">
<xsl:attribute name="onclick">
<xsl:value-of disable-output-escaping="yes" select="$OnClickTargetAttribute"/>
</xsl:attribute>
</xsl:if>
<xsl:value-of select="$DisplayTitle"/>
</a>
</div>
<xsl:variable name="StartDate">
<xsl:value-of select="ddwrt:FormatDateTime(string(@PublishedDate), 1033, 'g')" />
</xsl:variable>
<div class="custom_date">
<xsl:value-of select="$StartDate" />
</div>
<div class="custom_description">
<xsl:value-of select="@Body" disable-output-escaping="yes" />
</div>
<div>
<table class="custom_postdetails" border="0" cellpadding="0" cellspacing="0">
<tr>
<td class="custom_author">
By: <xsl:value-of select="@Author" /> |
</td>
<td class="custom_comments">
Comments: <xsl:value-of select="@NumComments" /> |
</td>
<td class="custom_category">
Category: <xsl:value-of select="@PostCategory" />
</td>
</tr>
</table>
</div>
</xsl:template>
- Save the file and upload it to back up to the style library
Step 4: Apply Custom XSL style to content query
- Modify the Content Query Web Part again
- Expand open The Presentation Category
- Choose the custom “BlogPost” style
- Save the page and you should now see the blog post with the following data and format.
Here are the files so that you don’t have to go through the whole process. Happy SharePointing!
Comments
just a thing: with new CQWP of 2010 you should not need anymore to export your web part to enumerate the Commonviewfields; you can use slots (and you do not need anymore to know the field type and the internal name).
Isn't it? Thanks for you post,
Barbara
I try to find how display rating control in the CQWP?
Can I display more than 4 columns?
Thanx in advance.
When implementing the web part, and also when looking at your screen shot, there is nothing showing up ater 'comments' and/or 'categories' at the end of each post. Aren't the number of comments and the categories for each post going to show up there?
Why dont you put it on sale and earn some bucks. http://www.thesharepointmarket.com/submit-product/
and start earning!
Plz help me out.
Correlation ID: 5d29889e-041b-40cf-9dc0-bc46fedbf983
That is about all that I can find that is helpful.
Item has already been added. Key in dictionary: 'CommonViewFields' Key being added: 'CommonViewFields'
Tryed to reload the original file and still get the same error.
Thanks for at great post.
Regarding the comments and category.
I can get it to work when I point the CQWP to one specifik list with posts.
The category however needs styling, but I guess that is fixable.
Claus
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 folled all the steps but not able to see "BlogPost" Style.Am I missing something.
Plseae, help
Thank You so much for this post. It's work like charm.
Could you please advice me that how can I make some cosmatic changes. Like I want Blog Title Should be in Bold, "Created By " should be in Different Colour.
Any idea??
Followed all the steps and selected Item style: BlogPost and hit apply.
I recieved this message in my Content Query Blog Section:
--------------------------------
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.
Correlation ID:cc5ae083-f6c7-4248-b8b7-f91187751bea
-----------------------------------
Any ideas why this is showing up and how to solve this problem?
But I have a question, I want the post link to open the post in a page not in default SharePoint Item Display Form, how can I do that?
Thank you so much.
When I wire the list directly in the post list I can get back comments, but that is the only way. Any idea what might be causing this?
With reference to NonsoloSharePoint's comment.. Do I just need to append the ItemStyles.xsl with my additional fields. I am thinking I want my CQWPs to be easily maintained by anyone other than me.
Is it possible to retrieve contact information from a Query, only as i am trying to retrieve an About Me field for an Employee Spotlight webpart and i am unable to do so. This works fine when working with Announcements and Blog Posts. Any ideas?
Thanks, Rich
I have followed the steps up to step 3.
I can not find Library style after clicking 'view all contnet site'? Do you know why?
Thanks.
I've downloaded your web part and the style?
I had the same problem with the Comments and Categories not appearing.
I found that if you do the following then you can get the items to appear.
1. Edit the Webpart
2. Go to the Presentation section
3. In the fields to display enter the column name for your category and comments in the PostCategory and NumComments fields
Hope this helps someone
But I have some problems :
- impossible to get categories
- do you have a tip to limit the body size ? (xx first caracters for example) because if we have big posts, it breaks the layout
- the URL seems to be wrong, if I click on the post title I have a 404 error, page not found
John,
Hopefully your comment about adding those fieldnames to the webpart will help doum with his issue with the comments.
Doum,
To limit the content area use the follwing: (Note the part about the substring and the 200 is the charictor limit
xsl:value-of select="substring(@Body, 0, 200)" disable-output-escaping="yes"
Had to add xmlns:ddwrt="http://schemas.microsoft.com/WebParts/v2/DataView/runtime"
in the top element
xmlns:ddwrt="http://schemas.microsoft.com/WebParts/v2/DataView/runtime"
I also checked the attached file and that has it in there as well. Did you miss step 3 or am I missing something else?
Erik
Thank you for this post - it is exactly what I have been looking for.
I used the files you provided and the CQWP works as designed except for one small issue. The CQWP loads properly for both site owners and site members; however, site visitors (those with read-only permissions) are getting the following error message:
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.
Correlation ID:529cf9c3-6e3e-4ac8-862e-41fb5c1d7ef5
Any advice you can offer would be greatly appreciated,
Cori
Did you make sure that the itemstyle file is checked in, published as a major version and also approved?
Just a question (complete SharePoint styling novice here) - how do I get the title to display (use the same style attributes) that are in the post list we're pulling in from?
> Add any (sub)Site(s) all over the SiteCollection
> Query any Content Type(s)
> Add any Column(s) in no time
> Change style to any Layout without deploying anything
Here is a project on CodePlex: http://enhancedcqwp.codeplex.com/
Great post, but...
I followed all the steps, and it appears that I have the same issue as Satish Nagpal. I can not see the style under presentation. What am I missing? I downloaded your style and webpart.
Did you make sure that your itemstyle.xsl file in the style library was published as a major version and also approved? Also check to see if it is stuck in a approval workflow in your site has one enabled...
Joet
Thanks!
I know this is a two-year-old article; just thought I'd ask. Thanks!
New question: How can I show posts and comments in the same CQWP?
Thanks!
After I get back to the page and try to edit to set the webpart to the xsl template SP throws error, while I have this stylesheet, I can't edit any CQWPs which is a weird red flag to me. Is there something that perhaps rings a bell with this?
Thanks
Ram
very nice code but how do i get a word count. i just want to show some few lines of my blog..yhnaks
Name, Text; Division, Text; Sub Division, Text; Address, Text;
How would the File need to look for this
I have one problem left... Everything works great, I can see "Created by:" and "Date:". But the related information doesnt display. It should be saying "Created By: Charles Baker" But my name is not appearing. Any ideas?
Thanks.
i tried your code but it neither shows me number comments, Author name, Published Date.
Also character limit is also not working.
Could you please tell me how could i resolve this issue?
Thanks for this post.
I have followed each step carefully and still get the Correlation ID error.
Yes = the xsl is checked in and published as major version.
Question: Our devs already have some customisations in the ItemStyle.xsl. Is it possible they were would be any conflicts that could cause this error? What might they be?
Is it possible for any code conflicts that could cause this error?
I am an XML noob. Though fairly competent at picking things up.
First, make sure the xmlns:ddwrt code is within the tag at the top of the file.
Second, the long code snippet needs to be added to the end of the file BUT BEFORE the closing tag.
I was getting the "Unable to display this Web Part" error until I figured that out. It is a no-brainer type of user error now that I figured it out.
Hope that helps!
1. Edit the Content Query Web Part.
2. Under "Content Query Tool Part" on the right, expand "Presentation"
3. Scroll down to the "Styles:" section.
4. Select "Group style:" as "Default", Select "Item style" as "BlogPost" (last item in the list at the bottom).
and voila, you will see the blog postings from the sub-sites if you have created blogs within them.
I have tried - comments, # comments, NumComments;
Category, Categories, postCategory
None of them work. Do we need to add new site columns for Postcategory and NumComments?
used # Comments; and Category[Custom Column];
and it works.
So will this only be able to pull Blogs from one site?
1.Edit web part in browser
2.expand query
3.select show all items from list - pick the posts list from your Blog.
4.scroll down and and in NumComments type-
# Comments;
5.In post Category type-
Category [Custom Columns];
6. Click OK
7.done!
Hope this helps someone :)
sharepoint training institute in hyderabad
msbi training institute in hyderabad
Khalil
Kiran
I don't know whether its fair me or if other people experiencing issues with your site.
It shows up like a percentage of the composed content inside your substance are running off the screen. Would somebody be able to else please remark and let me know whether this occurrence to them also? This could bumblebee an issue with my program on the grounds that I've had this happen awhile ago. Much thanks to you..
web development company uae
Thank u for helpful tutorial, can I display more than 3 column ?
It's really helpful for me if you want advice me about this :)
link asphotportal
Thanked By
SharePoint Tutorial