Skip to main content

SharePoint 2010 Content Query for Blog Posts

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

image

  • 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.

image

  • 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.

image

  • 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.

image

Here are the files so that you don’t have to go through the whole process. Happy SharePointing!

Comments

Popular posts from this blog

How To: Create Hyperlink to Modal Pop-Up Form

I was asked by a client recently if there was a way to create a hyperlink to a New Item Form anywhere within a site but still get the rich experience of the Modal pop-up window that grays out the background. (Note this is for SharePoint 2010 Only…) I basically took the code directly from the “Add new item” and the “Add Document” link within the list view. What this allows you to do is simply add in the following code to any content editor web part, Master page, or Page Layout in any site collection and display the form to be filled out. The user will get the nice experience of the modal window and not have to navigate away from their current page. This could be used for example a feedback form that is included in the master page so whenever someone wants to give feedback it is always going back to a central list. The only that is required for you to know is the List ID and the site name. Full Code For a List Item: <a onclick="javascript:NewItem2(event, &quot

SharePoint 2010 Base CSS Classes

This will be the first of many SharePoint 2010 posts. I will be focusing on a few of the main CSS classes used for SharePoint 2010 Public Beta. As the product becomes more final there might be some changes to the class names but I will be sure to create a new post if that happens. This will be quite a lengthy but it should be helpful. The default CSS given below are just highlights of the full CSS attributes for that class. I will be using a basic team site as my base for the screenshots. Here is a basic structure of the main areas that I will cover. Ribbon Row Table Row Left Site Actions Navigate Up Edit Tab List Browse Page Table Row Right Give Feedback Welcome Menu Workspace Body Container Title Row Title

SharePoint 2013 Responsive Table Columns

I have been wanting to write this one for a while now. It is really amazing how UX is really finding is way into everything that we use and interact with. From Custom applications both mobile and on a desktop to document management or large data visualizations. There is always room for better usability and new concepts. SharePoint lists and library functionality really has not changed much for the past 10 years... I remember back in 2003 when I saw the same table/grid based views of documents and list items that exists in SharePoint 2013. But now we can look at them in a whole new way! In this video blog you will see how to create a responsive CSS table so that when the browser size is reduced it will hide specific columns. However hiding data is not always the right thing to do. What if a user needed those columns to filter on or to use for comparison to another document? Well that is where the custom jQuery Column chooser comes in. It allows you to see what columns are displ