Showing posts with label CQWP. Show all posts
Showing posts with label CQWP. Show all posts

Monday, October 8, 2012

CQWP PageQueryString and PageFieldValue

CQWP PageQueryString and PageFieldValue

PageQueryString allows to configure the Content Query Webpart dynamically using the query string in the url. Let's configure the CQWP to retrieve different categories of announcements based on the query string value. 
Add a CQWP to the page and configure Query section as follows :
Source: Show items from all sites in this site collection
List Type: Announcements
Content Type:Show items of this content type group : My Content Types


Content Type:
Show items of this content type: Company
 Announcements

In additional filters Select "Announcement Category" filed is equal to [PageQueryString : Category]
Save the webpart. I have placed the CQWP on the homepage of my site "http://intranet.contoso.com", so if I change the url to "http://intranet/SitePages/Home.aspx?Category=Technology", this is the result I get.

PageFieldValue is used to show the related content. Following examples illustrates it. Go to your list which contains our custom content type "Company Announcements". In the ribbon select List->Customize Form->Modify Form Webparts->Default Display Form 
Add CQWP and configure the webpart to use our custom content type. In Additional filters section select Announcement Category is equal to [PageFieldValue:Announcement Category] and  Title is not equal to [PageFieldValue:Title]

This will ensure that when we click on an item we will also get to see related items based on the Announcement Category of the current item and at the same current item will not be shown in the related announcements section.

CQWP XSL customizations - Read More Option

CQWP XSL customizations  - Read More Option

Posted by Nadeem Yousuf | 05:13 | , | 0 comments » Content Query webpart series:
Part 1: Configuring SharePoint 2010 CQWP

Part 2: CQWP PageQueryString and PageFieldValue
Part 3: CQWP XSL customizations
Part 4 : CQWP QueryOverride
CQWP style sheets are present inside a folder "XSL Style Sheets" in Style Library at the root site collection. ItemStyle.xsl file contains the style templates which can applied to the items returned by CQWP. The best practice to create custom styles is to make the copy of  ItemStyle.xsl and make changes in it and then upload the new customized xsl file to the style library.
Now let's create a custom style which will show the Title of our Company Announcements in bold font and show  first 200 characters of the Body field followed by ...Read More link. Create a copy ItemStyle.xsl file and name it  CustomItemStyle.xsl file. Open the file in any editor and paste the following at the end of the file before </xsl:stylesheet>. Upload the CustomItemStyle.xsl to Style Library inside XSL Style Sheets folder.


<xsl:template name="CustomStyle" match="Row[@Style='CustomStyle']" mode="itemstyle">
              <xsl:variable name="SafeLinkUrl">
                     <xsl:call-template name="OuterTemplate.GetSafeLink">
                           <xsl:with-param name="UrlColumnName" select="'LinkUrl'"/>
                           <xsl:with-param name="UseFileName" select="1"/>
                     </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>
              <p>
                     <strong>
                           <xsl:value-of select="$DisplayTitle"/>
                     </strong><br />
                     <xsl:value-of select="substring(@Body,0,200)" disable-output-escaping="yes"/>
                     <a href="{$SafeLinkUrl}">...Read more</a>
              </p>

       </xsl:template>

Now add a fresh CQWP to your page and then export it. Save the generated file and open it in notepad. Locate property ItemXslLink and replace it with following:
<property name="ItemXslLink" type="string">/Style Library/XSL Style Sheets/CustomItemStyle.xsl</property>
Locate property ItemStyle and replace it with following:
<property name="ItemStyle" type="string">DetailedOverview</property>
Save the webpart file and upload it to the SharePoint page.
After you upload the webpart file, you can find the webpart in Imported Web Parts category. Now add the web part to the page and edit it. Make the webpart to read data from whole site collection, select Announcements List, select My Content Types Group and Company Announcements content type. 
In the presentation section select following:
As can be seen a field named Body has been added. This is called a Slot. If we want to show more fields we can add them in xsl as we added Body field. Click OK to save the settings. The final result will be something like this.