Ħarsa ġenerali u Objettiv
Barra mill-kaxxa, MOSS’ Parti kontenut Web Mistoqsija (CQWP) juri r-riżultati tagħha f'format lista, similar to search results. It is also possible to display the results in a grid format (I.E. F'format ta 'tabella HTML). Grid formats are better in some circumstances. I describe how to achieve that effect in this article.
Xenarju Business
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, such as title, budget, expected completion date, remaining budget and other summary type fields. By "singleton" I mean a custom SharePoint list guaranteed to contain only one item. Simplistically, jidher qisu dan:
The technical approach is much the same as described hawn (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:
HTML like this generates that result:
<html> <korp> <ċentru> <tabella fruntiera=1> <!-- Labels --> <tr bgcolor=blue> <td><font color=white><b>Project Name</b></font></td> <td jallinjaw=right><font color=white><b>Complete Date</b></font></td> <td jallinjaw=right><font color=white><b>Budget</b></font></td> <td jallinjaw=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 jallinjaw=right>02/01/08</td> <td jallinjaw=right>22,500.00</td> <td jallinjaw=right>19,000.00</td> <td>In Progress</td> </tr> <tr> <td>Provision servers for SQL Upgrade</td> <td jallinjaw=right>04/01/08</td> <td jallinjaw=right>7,500.00</td> <td jallinjaw=right>0.00</td> <td>Planned</td> </tr> </tabella> </ċentru> </korp> </html> |
Approach
Follow these steps to create the grid:
- Identify the components of the grid (rows/columns).
- Define and create necessary site columns.
- Create sub sites for the projects and singleton lists.
- Add the CQWP to a web page and configure it to search for your lists.
- Modify the CQWP’s XML to gather up the additional columns.
- 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 hawn (http://msdn2.microsoft.com/en-us/library/bb897399.aspx) and Heather Solomon’s blog hawn (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.
F'dan il-punt, 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. Oħloq tip kontenut, a templatized custom list for that content type and two sites. Here is the content type:
Here is the site structure:
2. Add the CQWP after creating my project subsites and singleton project summary lists:
3. Add all the additional information I want via the <CommonViewFields>:
<proprjetà isem="CommonViewFields" tip="string">Project_x0020_Name;Project_x0020_Expenses;Project_x0020_Status;Project_x0020_Start_x0020_Date;Project_x0020_End_x0020_Date;Project_x0020_Budget</proprjetà>
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. F'dan il-punt, 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:
<XSL:template name="Grid" match="Row[@Style=’Grid’]" mode="itemstyle">
I also slightly modify her suggested <XSL:għal-kull …> by adding a <br /> tag to provide a cleaner listing:
<XSL:għal-kull tagħżel="@*"> P:<XSL:il-valur tal- tagħżel="isem()" /><br/> </XSL:għal-kull>
4.2: I modify the web part, go to appearance and select my "Grid" stil:
Apply the change and here is the result:
We can see from the above that the fields we want (Project name, expense, status, eċċ) 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. Per eżempju, we reference Project Status as "Project_x005F_x0020_Name".
F'dan il-punt, we depart from Heather’s blog and from the shoulders of these giants, I add my own little bit.
ContentQueryMain.xsl
NOTA: When making changes to both ContentQueryMain.xsl as well as ItemStyle.xsl, you need to check those files back in before you see the effect of your changes.
For grid-making purposes, 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 <tabella> tag before emitting the first row of data and the closing <tabella> tag after emitting the last row. To accomplish this, ContentQueryMain.xsl is modified to pass two parameters to our "grid" template in ItemStyle.xsl, "last row" and "current row". ItemStyle.xsl uses these to conditionally emit the necessary tags.
Using Heather Solomon’s technique, we locate ContentQueryMain.xsl. It is located in the same place as ItemStyle.xsl. This screen shot should help:
We need to make the following changes:
- Modify an xsl 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 <tabella> 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:
<XSL:template isem="OuterTemplate.CallItemTemplate">
Replace the whole template as follows:
<XSL:template isem="OuterTemplate.CallItemTemplate"> <XSL:param isem="CurPosition" /> <!-- Add the "LastRow" parameter. We only use it when the item style pass in is "Grid". --> <XSL:param isem="LastRow" /> <XSL:jagħżlu> <XSL:Meta test="@Style='NewsRollUpItem'"> <XSL:apply-templates tagħżel="." mode="itemstyle"> <XSL:ma 'param isem="EditMode" tagħżel="$cbq_iseditmode" /> </XSL:apply-templates> </XSL:Meta> <XSL:Meta test="@Style='NewsBigItem'"> <XSL:apply-templates tagħżel="." mode="itemstyle"> <XSL:ma 'param isem="CurPos" tagħżel="$CurPosition" /> </XSL:apply-templates> </XSL:Meta> <XSL:Meta test="@Style='NewsCategoryItem'"> <XSL:apply-templates tagħżel="." mode="itemstyle"> <XSL:ma 'param isem="CurPos" tagħżel="$CurPosition" /> </XSL:apply-templates> </XSL:Meta> <!-- Pass current position and lastrow to the Grid itemstyle.xsl template. ItemStyle.xsl will use that to emit the open and closing <tabella> tags. --> <XSL:Meta test="@Style='Grid'"> <XSL:apply-templates tagħżel="." mode="itemstyle"> <XSL:ma 'param isem="CurPos" tagħżel="$CurPosition" /> <XSL:ma 'param isem="Last" tagħżel="$LastRow" /> </XSL:apply-templates> </XSL:Meta> <XSL:inkella> <XSL:apply-templates tagħżel="." mode="itemstyle"> </XSL:apply-templates> </XSL:inkella> </XSL:jagħżlu> </XSL:template> |
The comments describe the purpose of the changes.
Of course, the "OuterTemplate.CallItemTemplate" is itself called from another template. Locate that template by searching for this text string:
<XSL:template isem="OuterTemplate.Body">
Scroll through the instructions in OuterTemplate.Body and insert the LastRow parameter as follows (shown as a comment in italics):
<XSL:call-template isem="OuterTemplate.CallItemTemplate"> <XSL:ma 'param isem="CurPosition" tagħżel="$CurPosition" /> <!-- Insert the LastRow parameter. --> <XSL:ma 'param isem="LastRow" tagħżel="$LastRow"/> </XSL:call-template>
After all of this, we finally have things set up properly so that our ItemStyle.xsl can emit <tabella> tags at the right place.
ItemStyle.Xsl
NOTA: Għal darb'oħra, 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).
Ewwel, near the top of ItemStyle.xsl, add this line:
<!-- Some mumbo jumbo that enables us to display U.S. currency. --> <XSL:dećimali-format isem="staff" digit="D" /> <XSL:template isem="Default" jaqblu="*" mode="itemstyle">
Note that I added it directly before the <XSL:template name="Default" …> definition.
Li jmiss, 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.
<XSL:template isem="Grid" jaqblu="Row[@Style='Grid']" mode="itemstyle"> <!-- ContentMain.xsl passes CurPos and Last. We use these to conditionally emit the open and closing <tabella> tags. --> <XSL:param isem="CurPos" /> <XSL:param isem="Last" /> <!-- The following variables are unmodified from the standard ItemStyle.xsl --> <XSL:varjabbli isem="SafeImageUrl"> <XSL:call-template isem="OuterTemplate.GetSafeStaticUrl"> <XSL:ma 'param isem="UrlColumnName" tagħżel="'ImageUrl'"/> </XSL:call-template> </XSL:varjabbli> <XSL:varjabbli isem="SafeLinkUrl"> <XSL:call-template isem="OuterTemplate.GetSafeLink"> <XSL:ma 'param isem="UrlColumnName" tagħżel="'LinkUrl'"/> </XSL:call-template> </XSL:varjabbli> <XSL:varjabbli isem="DisplayTitle"> <XSL:call-template isem="OuterTemplate.GetTitle"> <XSL:ma 'param isem="Titolu" tagħżel="@Title"/> <XSL:ma 'param isem="UrlColumnName" tagħżel="'LinkUrl'"/> </XSL:call-template> </XSL:varjabbli> <XSL:varjabbli isem="LinkTarget"> <XSL:jekk test="@OpenInNewWindow = 'True'" >_blank</XSL:jekk> </XSL:varjabbli> <!-- 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. --> <XSL:varjabbli isem="tableStart"> <XSL:jekk 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> ]]> </XSL:jekk> </XSL:varjabbli> <!-- 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. --> <XSL:varjabbli isem="tableEnd"> <XSL:jekk test="$CurPos = $Last"> <![CDATA[ </tabella> ]]> </XSL:jekk> </XSL:varjabbli> <!-- 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 "<tabella>" instead of "<tabella>". --> <XSL:il-valur tal- tagħżel="$tableStart" jiskonnettja-output-ħarba="iva"/> <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> <XSL:il-valur tal- tagħżel="@Project_x005F_x0020_Name"/> </td> <td jallinjaw="dritt"> <XSL:il-valur tal- tagħżel="@Project_x005F_x0020_End_x005F_x0020_Date"/> </td> <td jallinjaw="dritt"> <XSL:call-template isem="formatcurrency"> <XSL:ma 'param isem="valur" |
This has been incredibly useful. Grazzi!
Aah… kien minħabba I jismu Lista Style tiegħi b'mod differenti u ma jirrealizzaw li fil-ContentQueryMain.xsl din il-kontrolli għall- "stil":
<XSL:meta test = "@ Style =" NameOfMyStyle ' ">
With that renamed it worked. 🙂
It almost worked for me: I manage to output the </tabella> end tag correctly, but the starting tag is missing. Jidher it-test = "$ CurPos = 1" does not return true. But why?
Great Post. Paul i have done all the above things. I have a query, Ta 'kif nista għas-somma ta' "spejjeż tal Attwali TOTAL" or sum of the value of two columns.
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.
fantastic post thanks! Have been battling with this now for the last few days.
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, għalkemm. 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?
Grazzi,
Greg
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:
<Wisa TABELLA = "100%" class = "ms-listviewtable" Fruntiera = 0 cellspacing = 0 cellpadding = 1 dir = "Xejn">
<!–HEADER–>
<TR klassi = "ms-viewheadertr" VALIGN=TOP>
<TH nowrap ambitu = "col" klassi = "ms-vh2"><stil div = "wisa ':100%;position:relative;left:0;top:0;">
<stil TABELLA = "wisa ':100%;" CtxNum = "1" għoli = "100%" cellspacing = 1 cellpadding = 0 klassi = "MS-unselectedtitle">
<TR>
<Wisa TD = "100%" Class = "ms-vb" nowrap>
Texte du titre ici
</TD>
<stil TD = "pożizzjoni:absolute;">
</TD>
</TR>
</TABLE></div>
</TH>
<!–Repeat for all header cells–>
</TR>
<!– Table data –>
<klassi TR = "">
<!–Data column, use Paul’s code for the first one with the hyperlink –>
<TD Klassi = "ms-VB2">Data here</TD>
<!–Empty column–>
<TD Klassi = "ms-VB2">
<span dir=None></medda><
/TD>
<!–Date column–>
<TD Klassi = "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 klassi = "ms-alternanti">
…
</TR>
</TABLE>
Thatz precisely i was looking for..You rock man..Long live..Love you for the post..
<XSL:japplikaw-mudelli agħżel = "." mode="itemstyle">
<XSL:-Bi param isem = "CurPos" agħżel = "$ CurPosition" />
<XSL:-Bi param isem = "Last" agħżel = "$ LastRow" />
<XSL:-Bi param isem = "StartNewGroup" agħżel = "@__ begingroup" />
</XSL:Meta>
<XSL:meta test = "$ StartNewGroup =" Veru "">
<tr>
<td >
<XSL:Isem telf mudell = "OuterTemplate.GetGroupName">
<XSL:-Bi param isem = "GroupName" agħżel = "@ *[isem()=$Group]"/>
<XSL:-Bi param isem = "GroupType" agħżel = "$ GroupType" />
</XSL:call-template>
</td>
</tr>
</XSL:Meta>
<XSL:inkella>
</XSL:inkella>
</XSL:jagħżlu>
Hi Paul,
When working with the xml/xsl and content query web part, have you found any good ways to format your xslt? Trying to make changes and test the design is proving a right pain in the a** just using a text editor…
All the best
(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)
Your post is exactly what I was looking for. Madankollu, 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 jew 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. madankollu, 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.
Grazzi.
Saji
Sorry for the missspelling on my name not sure where the ” came from.