Skip to content

Display News pages with CQWP – Part III

July 18, 2011

This blog shows how to create a news ticker with Content By Query Web Part (CQWP), how you can make items slide from right to left with help of SharePoint Designer 2010. In this example you will modify ItemStyle.xsl and ContentQueryMain.xsl and use a portion CSS.
In Part 1 in this series I wrote about CQWP and how to create a custom template in ItemStyle.xsl and how to use CSS for the styles. In Part 2 I wrote about how to display the latest 5 news by category and how to separate them by tabs.

Click at the image below to activate the video:

This blog do not cover how to configure the CQWP or to modify ItemStyle.xsl or the other XSL files that describes the CQWP, we´re going straight to the core for the fun stuff which means ItemStyle.xsl, ContentQueryMail.xsl, CSS and jQuery.
Please read the previous posts in this series for more related information.

Some things to consider when the slider template is to be built:

  • Modify the structure of ContentQueryMain can affect all other templates in ItemStyle, when you do this you need therefore to customize a copy of ContentQueryMain. In this example were just adding a class name so you don’t need to do that now.
  • Alternative to customize the xsl files you can create modul with Visual Studio, a feature that copies your customized files to style library including a modified version of the CQWP to the web part gallery.

Let´s go!

Download the jQuery plugin called easySlider by CSS globe. Make a reference in your custom master page to this easySlider. Do also reference to latest jQuery in your custom master page.

<script type="text/javascript" src="/Style Library/Scripts/jquery-1.6.min.js"></script>
<script type="text/javascript" src="/Style Library/Scripts/easySlider1.7.js"></script>

Paste following script in the head section of your master page.

<script type="text/javascript">
$(document).ready(function(){	
	
	$(".slider").easySlider({
		auto: true,
		continuous: true,
		nextId: "slider1next",
		prevId: "slider1prev",
		prevText: 		'',
		nextText: 		'',
		speed: 	1000, // 800
		pause:	3000, // 2000
	});
	
});	
</script>

Check out ItemStyle.xsl. Paste following templates into ItemStyle. This example contains two templates, the first one is for limit number of words for the header and the next is the actual sliding template. Publish the file when you’re done.

<!-- Word counter Template -->
<xsl:template name="FirstNWords">
  <xsl:param name="TextData"/>
  <xsl:param name="WordCount"/>
  <xsl:param name="MoreText"/>
  <xsl:choose>
    <xsl:when test="$WordCount &gt; 1 and (string-length(substring-before($TextData, ' ')) &gt; 0 or string-length(substring-before($TextData, '  ')) &gt; 0)">
      <xsl:value-of select="concat(substring-before($TextData, ' '), ' ')" disable-output-escaping="yes"/>
      <xsl:call-template name="FirstNWords">
        <xsl:with-param name="TextData" select="substring-after($TextData, ' ')"/>
        <xsl:with-param name="WordCount" select="$WordCount - 1"/>
        <xsl:with-param name="MoreText" select="$MoreText"/>
      </xsl:call-template>
    </xsl:when>
    <xsl:when test="(string-length(substring-before($TextData, ' ')) &gt; 0 or string-length(substring-before($TextData, '  ')) &gt; 0)">
      <xsl:value-of select="concat(substring-before($TextData, ' '), $MoreText)" disable-output-escaping="yes"/>
    </xsl:when>
    <xsl:otherwise>
      <xsl:value-of select="$TextData" disable-output-escaping="yes"/>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template> 

<!-- Sliding News Template -->
<xsl:template name="SlidingNews" match="Row[@Style='SlidingNews']" 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>
	<xsl:variable name="Created">
            <xsl:value-of select="@Created" />
	</xsl:variable>
	<xsl:variable name="LinkTarget">
		<xsl:if test="@OpenInNewWindow = 'True'" >_blank</xsl:if>
	</xsl:variable>
	
	<div class="SlidingNewsWrap CQWP-Content">		
		<xsl:value-of select="$Created"/>
		<a href="{$SafeLinkUrl}" target="{$LinkTarget}" title="{@LinkToolTip}" >
			<xsl:call-template name="FirstNWords">
        		<xsl:with-param name="TextData" select="$DisplayTitle"/>
       			<xsl:with-param name="WordCount" select="7"/>
        		<xsl:with-param name="MoreText" select="'...'"/>
      		</xsl:call-template>
		</a>
	</div> 
	
</xsl:template>

If you want the date to be formatted you can use for example this xsl:value-of select=”ddwrt:FormatDateTime(string(@Created) ,1033 ,’dd MMMM’)” instead of xsl:value-of select=”@Created”, if you use the ddwrt function you have to include xmlns:ddwrt=”http://schemas.microsoft.com/WebParts/v2/DataView/runtime&#8221; in the XSL:Stylesheet defintion in the top of the Itemstyle file.

Check out ContentQueryMain.xsl. Open the file and locate the div with the class name cbq-layout-main. Add the class slider after the cbq-layout-main class.
Don’t forget a blank space between. Publish the file when you’re done.

<div id="{concat('cbqwp', $ClientId)}" class="cbq-layout-main slider">

Here goes the CSS

/* --- QCWP Slider --- */
.CQWP-Content {
width:390px; height:22px;
padding:7px 0px 0px 10px;
font-family:Cambria, Georgia;
font-size:11px;
border-style:none none solid none;
border-width:1px;
border-color:#e5e5e5;  
}
.CQWP-Content a:link, .CQWP-Content a:visited {
color:green;
}
.slider ul, .slider li, .slider li{ 
width:900px;
overflow:hidden;
}

Stay in tune for the next post in this series, I will wrote about how to cycle images from a image library this time. Please drop a comment if you got any questions, Christian

About these ads
11 Comments
  1. Umar permalink

    Fabulous. Awesome . I have read all of your blogs pblished untill now, You are Sharepoint King

  2. Padma permalink

    Hi,
    I am getting error after adding the code into Itemstyle.xsl:

    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:e23fa1d0-f828-47bc-93d4-127cbdb851b7

    Please suggest me the same

  3. HI Padma, check out the ItemStyle.xsl and change the ddwrt:FormatDateTime, use created instead do verify that everything works ok:

    If you want to use the ddwrt:FormatDateTime you have add the following stuff into the XSL:StyleSheet in the head of the page xmlns:ddwrt=”http://schemas.microsoft.com/WebParts/v2/DataView/runtime”

    I have now updated this blog with this instructions, thanks Padma and let me know if you still have trouble with this.

    / C

  4. Umar permalink

    Dude . you are great !!

  5. moses permalink

    Hi Christian,
    Great articles i followed all three of them and after adding all the code from this third article
    the two other CQWP i’d configrued before also slide, how can I prevent them from sliding?
    Thanks
    Moses

  6. Hi Moses, that’s because the jQuery that creates the slide use the .slider class thats been added in the wrapper file ContentQueryMain.xsl, to avoid that other templates in ItemStyle slides I recommend you to create a copy of ContentQueryMain.xsl and add the slide class in this instead. I have written about how to use custom versions of this two files in another blog -
    http://chrisstahl.wordpress.com/2011/08/09/image-slideshow-with-cqwp-in-sharepoint-2010/ – hopes this can help you!

    /C

    • moses permalink

      Great! thanks
      Moses

  7. Preeti permalink

    Hello Christian, I am new to content query webparts. I have followed the steps in this article. I created custom versions of the files and worked on them. But the slider does not seem to work for me. It is just displaying as the regular CQWP. Can you please help me?

  8. philchak permalink

    Your work is amazing ! I’m new to Sharepoint 2010 but i’m coming from the java world (Liferay, etc ..) With this lesson is 3 steps, you give all the necessary tools for webmaster to realize beautiful and wonderful website ! Thanks again !

Trackbacks & Pingbacks

  1. SharePoint 2010 Branding and Customization Resources « Branding « Sharepoint « The SharePoint Muse
  2. Useful links for SharePoint 2010 Branding « Inside Microsoft Technologies – Sunil Thotapalli

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.

Join 1,207 other followers

%d bloggers like this: