Propono Content Quaero Web Part Results in a risus / Mensam

Overview et obiectiva

Ex arca archa, MUSCUS’ Quaero Web content Parte (CQWP) ostenditque fructus ejus in elencho format, similar to search results. It is also possible to display the results in a grid format (i.e. HTML mensam format). Grid formats are better in some circumstances. I describe how to achieve that effect in this article.

Negotium Missionem

I have worked with a client on an enterprise-wide MOSS rollout. We have designed their taxonomy such that projects are first class citizens in the hierarchy and have their own top level site. Project managers maintain a singleton list of project summary information, ut tituli, budget, consummatio expectatur date, remaining budget and other summary type fields. By "singleton" I mean a custom SharePoint list guaranteed to contain only one item. Simplistically, is vultus amo is:

imaginem

The technical approach is much the same as described hic (http://paulgalvin.spaces.live.com/blog/cns!1CC1EDB3DAA9B8AA!447.entry). The CQWP uses an XSL transform to emit HTML for the browser to render.

I always envision the result before diving into the XSL because XSL is a nightmare. Here’s my desired result:

imaginem

HTML like this generates that result:

<html>
 <corpus>
 <centrum>
 <mensamque border=1>

<!-- Labels -->
 <tr bgcolor=blue>
 <td><font color=white><b>Project Name</b></font></td>
 <td align=right><font color=white><b>Complete Date</b></font></td>
 <td align=right><font color=white><b>Budget</b></font></td>
 <td align=right><font color=white><b>Actual Expense</b></font></td>
 <td><font color=white><b>Overall Status</b></font></td>
 </tr>

<tr>
 <td>Re-wire computer room.</td>
 <td align=right>02/01/08</td>
 <td align=right>22,500.00</td>
 <td align=right>19,000.00</td>
 <td>In Progress</td>
 </tr>

<tr>
 <td>Provision servers for SQL Upgrade</td>
 <td align=right>04/01/08</td>
 <td align=right>7,500.00</td>
 <td align=right>0.00</td>
 <td>Planned</td>
 </tr>

</mensamque>
 </centrum>
 </corpus>
</html>

Approach

Follow these steps to create the grid:

  1. Identify the components of the grid (rows/columns).
  2. Define and create necessary site columns.
  3. Create sub sites for the projects and singleton lists.
  4. Add the CQWP to a web page and configure it to search for your lists.
  5. Modify the CQWP’s XML to gather up the additional columns.
  6. Modify the XSL to generate a table.

I’m going to concentrate on number six. Numbers one through four are straight-forward and something that any CQWP user has already done. Number five has been well-documented by others including this exhaustive screen-shot laden article from MSDN hic (http://msdn2.microsoft.com/en-us/library/bb897399.aspx) and Heather Solomon’s blog hic (http://www.heathersolomon.com/blog/articles/CustomItemStyle.aspx).

Nuts And Bolts

Begin and implement steps one through five as per the MSDN documentation and Heather Solomon’s article.

Ad hoc, you’ve added your CQWP to the page and you have your <CommonViewFields> configured as necessary.

Following the usual steps, I get these intermediate results:

1. Partum a content type, a templatized custom list for that content type and two sites. Here is the content type:

imaginem

Here is the site structure:

imaginem

2. Add the CQWP after creating my project subsites and singleton project summary lists:

imaginem

3. Add all the additional information I want via the <CommonViewFields>:

        <proprietate nomen="CommonViewFields" typus="filum">Project_x0020_Name;Project_x0020_Expenses;Project_x0020_Status;Project_x0020_Start_x0020_Date;Project_x0020_End_x0020_Date;Project_x0020_Budget</proprietate>

Note that I had to keep all the property fields on one line or it would not work (CQWP would tell me that the query returned no items).

4. Ad hoc, we’re ready to move beyond the MSDN article and flip on over to Heather Solomon’s article. Follow her steps starting near step #5 to create a customized / unghosted version of ItemStyle.xsl. I follow Heather’s advice, up through step 11 and get these intermediate results:

4.1: Name my XSL template as follows:

<p:template name="Grid" match="Row[@Style=’Grid’]" mode="itemstyle">

I also slightly modify her suggested <p:nam quisque- …> by adding a <br /> tag to provide a cleaner listing:

    <p:nam quisque- elige="@*">
      P:<p:valor ex- elige="nomen()" /><br/>
    </p:nam quisque->

4.2: I modify the web part, go to appearance and select my "Grid" style:

imaginem

Apply the change and here is the result:

imaginem

We can see from the above that the fields we want (Project name, expense, status, etc) are available for us to use when we emit the HTML. Not only that, but we see the names by which we must reference those columns in the XSL. Verbigratia, we reference Project Status as "Project_x005F_x0020_Name".

Ad hoc, we depart from Heather’s blog and from the shoulders of these giants, I add my own little bit.

ContentQueryMain.xsl

MONUMENTUM: Cum utrisque mutationibus tam ContentQueryMain.xsl ItemStyle.xsl, tu opus ad reprehendo retro in conspectu illorum lima videtis effectum mutationes tuas.

Nam eget consilium-factionis, MOSS uses two different XSL files to produce the results we see from a CQWP. To generate the previous bit of output, we modified ItemStyle.xsl. MOSS actually uses another XSL file, ContentQueryMain.xsl to in conjunction with ItemStyle.xsl to generate its HTML. As its name implies, ContentQueryMain.xsl is the "main" XSL that controls the overall flow of translation. It iterates through all the found items and passes them one by one to templates in ItemStyle.xsl. We’ll modify ItemStyle.xsl to generate the open <mensamque> tag emiserit ante in primo versu de notitia et occlusio <mensamque> tag after emitting the last row. To accomplish this, ContentQueryMain.xsl is modified to pass two parameters to our "grid" luctus in ItemStyle.xsl, "last row" and "current row". ItemStyle.xsl uses these to conditionally emit the necessary tags.

Heather scriptor Salomon usura ars, we locate ContentQueryMain.xsl. It is located in the same place as ItemStyle.xsl. This screen shot should help:

imaginem

His motus ad opus:

  • Vel demutare p template, "CallItemTemplate" that actually invokes our Grid template in ItemStyle.xsl. We will pass two parameters to the Grid template so that it will have the data it needs to conditionally generate opening and closing <mensamque> tags.
  • Modify another bit of ContentQueryMain.xsl that calls the "CallItemTemplate" to pass it a "LastRow" parameter so that LastRow may be passed on to our Grid template.

Locate the template named "OuterTemplate.CallItemTemplate" identified by the string:

  <p:Template nomen="OuterTemplate.CallItemTemplate">

Replace the whole template as follows:

  <p:Template nomen="OuterTemplate.CallItemTemplate">
    <p:param nomen="CurPosition" />

    <!--
      Add the "LastRow" parameter.
      We only use it when the item style pass in is "Grid".
    -->
    <p:param nomen="LastRow" />

    <p:elegerit>
      <p:cum test="@Style='NewsRollUpItem'">
        <p:apply-templates elige="." mode="itemstyle">
          <p:cum param- nomen="EditMode" elige="$cbq_iseditmode" />
        </p:apply-templates>
      </p:cum>
      <p:cum test="@Style='NewsBigItem'">
        <p:apply-templates elige="." mode="itemstyle">
          <p:cum param- nomen="CurPos" elige="$CurPosition" />
        </p:apply-templates>
      </p:cum>
      <p:cum test="@Style='NewsCategoryItem'">
        <p:apply-templates elige="." mode="itemstyle">
          <p:cum param- nomen="CurPos" elige="$CurPosition" />
        </p:apply-templates>
      </p:cum>

      <!--
              Pass current position and lastrow to the Grid itemstyle.xsl template.
              ItemStyle.xsl will use that to emit the open and closing <mensamque> tags.
      -->
      <p:cum test="@Style='Grid'">
        <p:apply-templates elige="." mode="itemstyle">
          <p:cum param- nomen="CurPos" elige="$CurPosition" />
          <p:cum param- nomen="Last" elige="$LastRow" />
        </p:apply-templates>
      </p:cum>

      <p:alioqui>
        <p:apply-templates elige="." mode="itemstyle">
        </p:apply-templates>
      </p:alioqui>
    </p:elegerit>
  </p:Template>

The comments describe the purpose of the changes.

Utique, the "OuterTemplate.CallItemTemplate" is itself called from another template. Locate that template by searching for this text string:

<p:Template nomen="OuterTemplate.Body">

Scroll through the instructions in OuterTemplate.Body and insert the LastRow parameter as follows (shown as a comment in italics):

<p:Template voca- nomen="OuterTemplate.CallItemTemplate">
  <p:cum param- nomen="CurPosition" elige="$CurPosition" />
  <!-- Insert the LastRow parameter. -->
  <p:cum param- nomen="LastRow" elige="$LastRow"/>
</p:Template voca->

After all of this, we finally have things set up properly so that our ItemStyle.xsl can emit <mensamque> tags at the right place.

ItemStyle.Xsl

MONUMENTUM: Iterum, check in ItemStyle.xsl after making any changes so that you see the effect of those changes.

We have two tasks here:

  • Replace the entire Grid template. You can copy/paste from below.
  • Add some mumbo jumbo outside the template definition that enables "formatcurrency" template to work. (You can tell that I have a tenuous handle on XSL).

Primum, near the top of ItemStyle.xsl, add this line:

  <!-- Some mumbo jumbo that enables us to display U.S. currency. -->
  <p:decimales format- nomen="staff" digit="D" />

  <p:Template nomen="Default" match="*" mode="itemstyle">

Note that I added it directly before the <p:template name="Default" …> definition.

Postero, go back to our Grid template. Replace the entire Grid template with the code below. It is thoroughly commented, but don’t hesitate to email me or leave comments on my blog if you have questions.

  <p:Template nomen="Grid" match="Row[@Style='Grid']" mode="itemstyle">

    <!--
      ContentMain.xsl passes CurPos and Last.
      We use these to conditionally emit the open and closing <mensamque> tags.
    -->
    <p:param nomen="CurPos" />
    <p:param nomen="Last" />

    <!-- The following variables are unmodified from the standard ItemStyle.xsl -->
    <p:variabilis nomen="SafeImageUrl">
      <p:Template voca- nomen="OuterTemplate.GetSafeStaticUrl">
        <p:cum param- nomen="UrlColumnName" elige="'ImageUrl'"/>
      </p:Template voca->
    </p:variabilis>
    <p:variabilis nomen="SafeLinkUrl">
      <p:Template voca- nomen="OuterTemplate.GetSafeLink">
        <p:cum param- nomen="UrlColumnName" elige="'LinkUrl'"/>
      </p:Template voca->
    </p:variabilis>
    <p:variabilis nomen="DisplayTitle">
      <p:Template voca- nomen="OuterTemplate.GetTitle">
        <p:cum param- nomen="Title" elige="@Title"/>
        <p:cum param- nomen="UrlColumnName" elige="'LinkUrl'"/>
      </p:Template voca->
    </p:variabilis>
    <p:variabilis nomen="LinkTarget">
      <p:si test="@OpenInNewWindow = 'True'" >_blank</p:si>
    </p:variabilis>

    <!--
      Here we define a variable, "tableStart".  This contains the HTML
      that we use to define the opening of the table as well as the column
      labels.  Note that if CurPos = 1, it includes the HTML in a CDATA tag.
      Otherwise, it will be empty.

      The value of tableStart is emited every time ItemStyle is called via
      ContentQueryMain.xsl.
    -->
    <p:variabilis nomen="tableStart">
      <p:si test="$CurPos = 1">
        <![CDATA[
        <table border=1>
          <tr bgcolor="blue">
            <td><font color="white"><b>Project Name</b></font></td>
            <td align="right"><font color="white"><b>Complete Date</b></font></td>
            <td align="right"><font color="white"><b>Budget</b></font></td>
            <td align="right"><font color="white"><b>Actual Expense</b></font></td>
            <td><font color="white"><b>Overall Status</b></font></td>
          </tr>
        ]]>
      </p:si>
    </p:variabilis>

    <!--
      Another variable, tableEnd simply defines the closing table tag.

      As with tableStart, it's always emited.  This is why its value is
      assigned conditionally based upon whether we've been passed the last
      row by ContentQueryMain.xsl.
    -->
    <p:variabilis nomen="tableEnd">
      <p:si test="$CurPos = $Last">
        <![CDATA[ </mensamque> ]]>
      </p:si>
    </p:variabilis>

    <!--
      Always emit the contents of tableStart.  If this is not the first
      row passed to us by ContentQueryMain.xsl, then we know its value
      will be blank.

      Disable output escaping because when tableStart it not blank, it
      includes actual HTML that we want to be rendered by the browser.  If
      we don't tell the XSL parser to disable output escaping, it will generate
      stuff like "&LT;mensamque&gt;" instead of "<mensamque>".
    -->
    <p:valor ex- elige="$tableStart" disable-output-erepta="Imo"/>


    <tr>
      <!--
      P:Project_x005F_x0020_Name
      P:Project_x005F_x0020_End_x005F_x0020_Date
      P:Project_x005F_x0020_Budget
      P:Project_x005F_x0020_Expenses
      P:Project_x005F_x0020_Status
      -->
      <td>
        <p:valor ex- elige="@Project_x005F_x0020_Name"/>
      </td>

      <td align="ius">
        <p:valor ex- elige="@Project_x005F_x0020_End_x005F_x0020_Date"/>
      </td>

      <td align="ius">
        <p:Template voca- nomen="formatcurrency">
          <p:cum param- nomen="valor" 
elige="@Project_x005F_x0020_Budget"></p:cum param-> </p:Template voca-> </td> <td align="ius"> <p:Template voca- nomen="formatcurrency"> <p:cum param- nomen="valor" elige="@Project_x005F_x0020_Expenses">
</p:cum param-> </p:Template voca-> </td> <td> <p:valor ex- elige="@Project_x005F_x0020_Status"/> </td> <!-- All of the following is commented out to clarify things. Autem, bring it back and stuff it into a <td> to see its effect. --> <!-- <div id="linkitem" class="item"> <p:if test="string-length($SafeImageUrl) != 0"> <div class="image-area-left"> <a href="{$SafeLinkUrl}" target="{$LinkTarget}"> <img class="image-fixed-width" src="{$SafeImageUrl}"
alt="{@ImageUrlAltText}"/> </a> </Div> </p:si> <div class="link-item"> <p:Template voca-
name="OuterTemplate.CallPresenceStatusIconTemplate"/> <a href="{$SafeLinkUrl}"
target="{$LinkTarget}" title="{@LinkToolTip}"> <p:value-of select="$DisplayTitle"/> </a> <div class="description"> <p:value-of select="@Description" /> </Div> </Div> </Div>
--> </tr> <!-- Emit the closing table tag. If we are not on the last row, this will be blank. --> <p:valor ex- elige="$tableEnd" disable-output-erepta="Imo"/> </p:Template> <p:Template nomen="formatcurrency"> <p:param nomen="valor" elige="0" /> <p:valor ex- elige='numerum format-($valor, "$DDD,DDD,DDD.DD", "staff")' /> </p:Template>

22 cogitationes on "Propono Content Quaero Web Part Results in a risus / Mensam

  1. Andreas

    Aah… it was because I named my List Style differently and didn’t realise that in the ContentQueryMain.xsl it checks for the "style":

    <p:when test="@Style=’NameOfMyStyle’">

    With that renamed it worked. 🙂

  2. Andreas

    It almost worked for me: I manage to output the </mensamque> end tag correctly, but the starting tag is missing. It seems the test="$CurPos = 1" does not return true. But why?

  3. Anand singh Rawat

    Great Post. Paul i have done all the above things. I have a query, how can I achive the sum of "TOTAL ACTUAL Expences" or sum of the value of two columns.

  4. Lisa

    Have you done any work with creating custom ContentQueryMain and ItemStyle xsl files? The reason I ask is that I don’t want to worry about my custom styles being overwritten with any patches or upgrades. Do you have examples? I’ve tried making an identical copy of the ContentQueryMain.xsl and uploading that new custom copy to the Style Library. When putting a reference to this xsl file in the .webpart’s MainXslLink property, I get 401 not authorized. The custom xsl file is a published version and everyone has read access to it.

  5. Greg Sheridan

    HI, this was a really helpful posting. I have it all working, and also implemented Mike Brown’s change so it groups
    the results inside the table by whatever I choose as the Group By parameter in the CQWB’s properties.
    I made the title a link also, so it works nicely. I still have one problem, licet. I’m grouping the results by site name. Inside the table, it groups them nicely, and it looks great, but it’s still printing out all the group
    names(in my case the site names) above the table. Does anyone know how to get rid of these?
    Gratias,
    Greg

  6. Non nomen

    Nice article. I managed to give my table the look of default Sharepoint Lists by analyzing the source code of the list. Here are the main HTML blocks needed to fit in ItemStyle:

    <TABLE width="100%" class="ms-listviewtable" border=0 cellspacing=0 cellpadding=1 dir="None">

    <!–HEADER–>
    <TR class="ms-viewheadertr" VALIGN=TOP>
    <TH nowrap scope="col" class="ms-vh2"><div style="width:100%;position:relative;left:0;top:0;">
    <TABLE style="width:100%;" CtxNum="1" height="100%" cellspacing=1 cellpadding=0 class="ms-unselectedtitle">
    <TR>
    <TD width="100%" Class="ms-vb" nowrap>
    Texte du titre ici

    </TD>
    <TD style="position:absolute;">
    </TD>
    </TR>
    </TABLE></Div>
    </TH>

    <!–Repeat for all header cells–>
    </TR>

    <!– Table data –>
    <TR class="">
    <!–Data column, use Paul’s code for the first one with the hyperlink –>
    <TD Class="ms-vb2">Data here</TD>

    <!–Empty column–>
    <TD Class="ms-vb2">
    <span dir=None></span><
    /TD>

    <!–Date column–>
    <TD Class="ms-vb2">
    <NOBR>11/12/2008</NOBR>
    </TD>
    </TR>

    <!–***Alternate row – Use a new XSL variable that will insert the alternate class based on the current row modulo 2–>
    <TR class="ms-alternating">

    </TR>
    </TABLE>

  7. Mike Brown
    Zoltan,
    I was working on the same issue as you. I wanted all my items displayed in a grouped manner rather than just a lits. Here is how I achieved it.
    ContentQueryMain.xsl
    <p:when test="@Style=’Grid’">
    <p:apply-templates select="." mode="itemstyle">
    <p:with-param name="CurPos" select="$CurPosition" />
    <p:with-param name="Last" select="$LastRow" />
    <!–add this new section to pass in the parameter indicating a new group has begun–>
    <p:with-param name="StartNewGroup" select="@__begingroup" />
    </p:apply-templates>
    </p:cum>
    ItemStyle.xsl
    Add your parameter
    <p:param name="StartNewGroup"/>
    Add this text as the first row of data
    <p:elegerit>
    <p:when test="$StartNewGroup = ‘True’">
    <tr>
    <td >
    <p:call-template name="OuterTemplate.GetGroupName">
    <p:with-param name="GroupName" select="@*[nomen()=$Group]"/>
    <p:with-param name="GroupType" select="$GroupType"/>
    </p:Template voca->
    </td>
    </tr>
    </p:cum>
    <p:alioqui>
    </p:alioqui>
    </p:elegerit>
    This should take care of your grouping issue in your grid. Each group will now begin with a new row indicating the group name. In my solution I actually added a blank <td/> in the data to indent it as well so the grouping stood out much better.
    Site1
    datacolumn1 datacolumn2 datacolumn3 etc

    datacolumn1 datacolumn2 datacolumn3 etc
    Site2
    datacolumn1 datacolumn2 datacolumn3 etc
    Site3

    datacolumn1 datacolumn2 datacolumn3 etc

    datacolumn1 datacolumn2 datacolumn3 etc
    etc.
  8. Zoltan
    Salve Paulus,
    Gratiae pro stipes, i managed to organise my query results in a grid, which is very helpful. Autem, I should go further and group my results based on Sites. At the moment, the Site names are listed first, and then the there is only one big grid gathering all the rows from different sites. Something like this:
    Site1 Name
    Site2 Name
    Data in grid for all sites
    Any ideas on what I should modify in order to get my data as follows:
    Site1 Name
    Data in grid for Site1
    Site2 Name
    Data in grid for Site2
    Gratias in progressus,
    Zoltan
  9. Pierre Joubert
    Fantastic Post, solved my problem I had.
    My only issue is displaying the AssignedTo field, I configure it with the "User" field type and the imported webpart then displays the following error:
    "There was an error retrieving data to display in this Web Part."
    I have tried using all possible field types, with anything other that "User" it does not display the error, but it does not display anything in the field. So I am assuming the User field type is correct, so I must be missing a setting somewhere.
    Any ideas on this?
    Adtinet
    Pierre
  10. Charftong
    Great Post!!! Very Very Helpful.
    Quick Question, fecisti opus in ostentans AssignedTo aut agros propositionis ModifiedBy? Propter aliquam causam,, Quotiens experiri dipslay est assignata in agro CQWP, it always displays with as an example "203;#Charf Tong". I cant find a way around this.
  11. Jennifer Davis
    Paulus
    Magna articulum, gratias agimus tibi. Sed ego volo ostendere consimili ipsum dolor sit amet, non item in album est. Verbigratia, habemus 25 summo gradu et singula continent project sites album genera contentus. Hi sunt Project Name, Divisionis, Descriptio. Volo uti contentus hac notitia query telam pars vellerent et potuero nisi quod per defaltam URL me accipit ad ingressum item in album quod ego vere opus ad me project site. potes me consultore?
    Gratias agimus tibi
    Jen
  12. Paulus Galvin
    Kieren,
    I always use visual studio to edit the XSL. It provides color coding and it also has some good intellisense support.
    –Paulus
  13. Scripsit Kieran:

    HI Paulus,

    Cum operantes in XML / p et telam contentus parte query, An invenisti, aliquid bonum format vias vestras xslt? Conatur ad consilium mutationes et probate probandum est ius dolor in justo a ** usura a text emendator…

    Optima

  14. Michael Dipay
    Regarding the issue I was having last time where it doesn’t display the images from the grid view but instead displays the text (https://myweb.com/Marketing/images/icn-order.gif,). EGO curo Solvere ope SharePoint Designer creando more sententia et consuetudo seligendi columnarum quod volo ut propono. Ego invenit quod typus imagines ostenduntur columna ut URL(Comma) (e.g. https://myweb.com/Marketing/images/icn-order.gif,). In current format est Text, Pellentesque ut ex communi ad p mutare:value-munia -> Format ut DESCRIPTIO. Species mutationis ostendit consuetudinem ostendere volui. Descripsi Codicem ad ostendendum imaginis ( <img terminus ="0" src="{substring ante-(@ IcnOrder, ', ')}" /> ) And in the ItemStyle.xsl file I have to change the @PICTURECOLUMNNAME to the code <img terminus ="0" src="{substring ante-(@ IcnOrder, ', ')}" /> . Tunc nisi illud. Nunc eget mensam meam consequitur ostendere textus tam consuetudinem imaginem typus columnarum.
  15. Helenam
    HI Paulus,
    Gratia magna articulus!
    De qua vos conditionalis formatting – "You can also put in conditional formatting, such as displaying projects who have exceeded their budget in red." Do you happen to have a code example for that?
    Gratias iterum!
    Helenam
  16. Michael Dipay
    HI Paulus,
    It was a great article and was able to generate the same results as expected. I have a requirement to show an image in the grid as one of its columns. It’s not displaying the image on the grid but instead displays the url to the image with a (,) comma at the end.
    (e.g. https://myweb.com/Marketing/images/icn-order.gif, )
    Should I use Image or Picture in the CommonViewFields Field Type? (e.g. InternalColumnName, Image)
    Gratias,
    Mike
  17. Saji Ijiyemi

    Your post is exactly what I was looking for. Autem, I can’t get it to work.

    I have documents that are stored in various doc. libraries but classified using a custom class column (choice: 1 aut 2. I want to use CQWP to show these documents based on thier class.
    I was able to add the CQWP, export it to SPD, configure the itemStyle.xls to show custom fileds in list view. autem, I could not successfully implement what you describe here.

    These are the columns I want to show in my CQWP:

    File Type (to show icon)
    File Title
    Date Last Modified
    Modified By Who
    Checked Out To
    Checked In Comments

    I currently have the result in a list format. How do I change it to a talble format.

    Gratias.

    Saji

Aliquam

Tua inscriptio electronica non editis. Velit sunt insignis *