Welcome on my SharePoint 2010 blog

Hello,

My name's Roy. I'm a dutch SharePoint & BI consultant/architect at Advantive B.V. At this moment I'm busy with some big SharePoint 2010 projects in The Netherlands. In all the projects I've got different roles, like: Business consultancy, Lead Consultant, Architect (logical and technical), Development and Teaching/courses.

Products where you can ask me about are: SharePoint, Visual Studio, SQL Server, PowerPivot, Analysis and Reporting Services, Visio Services, InfoPath, PerformancePoint Services, Team Foundation Server, Office line.

I love to work and to write about Microsoft SharePoint 2010 so, feel free and read/comment my Blogs!

Greetz.

Saturday, June 12, 2010

The new, friendlier SP 2010 Content By Query WebPart

This is a nice blog about a very often used and famous webpart: Content by Query WebPart.

The Content By Query webpart (CQWP) is one of the most powerful and most commonly used tools in SharePoint. On top of that it is also highly customizable. A big downside is that customizing the CQWP is tedious work. Especially if you do not want to alter SharePoint native files.

The CQWP simplifies working with Item Styles by introducing slots. A slot is a marker in the Item Style Template which is being filled with the content on runtime. The Content Query Web Part allows you to set the mappings between the slots and the fields using the property pane what allows you to create more semantic and reusable Item Style Templates!

Another great feature of the new Content Query Web Part is the Content to Content concept. Content to Content introduces two tokens that can be used in the Content Query Web Part filters. The PageQueryString token allows you to include in the query a value from a query string parameter and the PageFieldValue token gives you the possibility to filter the query results using a value of another field on the same page. This makes it extremely easy to provide functionality such as related content.

When I was working with the CQWP in SharePoint 2010 I found that most of the known nuisances are still there. For instance the ItemXslLink (and others) are still not configurable in the webpart configuration panel. But it seems one big improvement has been made: You do not longer have to set the CommonViewFields property (well, not if you do it right).
The SharePoint CQWP actively looks in the XSL style sheets for XML attribute references (@) it can’t resolve. If it finds them it will presents the user the possibility in the CQWP toolpart to map these attributes to SharePoint fields. For instance by default SharePoint would map the @Description attribute to the Comments field.

Even nicer is that you can even map multiple fields to one attribute. If the first field is empty the value of the second field is used.  

First of all you start off by creating a new Item Style template just as you would with Office SharePoint Server 2007 – nothing new here. But then the magic happens. As soon as you pick your custom Item Style template in the Web Part properties, CQWP parses the selected template and generates slots for it: every @SomeProperty becomes a slot:

<xsl:template name="Advantive" match="Row[@Style='Advantive']" 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="''"/>
        <xsl:with-param name="UrlColumnName" select="'LinkUrl'"/>
        <xsl:with-param name="UseFileName" select="1"/>
      </xsl:call-template>
    </xsl:variable>
    <p>
      <strong>
        <xsl:value-of select="$DisplayTitle"/>
      </strong><br />       <xsl:value-of select="@AdvantiveField1"/>, <xsl:value-of select="@AdvantiveField2"/>
    </p>
    <p>
      <xsl:value-of select="@AdvantiveField3"/>
      <xsl:value-of select="@AdvantiveField4"/>
      <xsl:value-of select="@AdvantiveField5"/>
    </p>
    <p>
      <a href="{$SafeLinkUrl}">Read more</a>
    </p>
  </xsl:template>


Example snippet of itemstyle.xsl the @AdvantiveField1, @AdvantiveField2, @AdvantiveField3 and @AdvantiveField4  attributes are highlighted.

Reload the ItemStyle.xsl sheet in de style library and the result will be presented in the props of the CQWP. You can use the fields (called the props) as the default fields in the CQWP.

No comments:

Post a Comment