moss自定义内容查询webpart
轉至virusswb博客
總的步驟如下:
??????1、添加內容查詢部件
??????2、設置查詢范圍
??????3、導出查詢部件文件,.webpart文件
??????4、修改webpart文件中的<property name="CommonViewFields" type="string" >Title,Text;Modified,DateTime</property>配置節,添加你想要顯示的欄目
??????5、用SPD打開頂級網站,在右側的樹目錄中找到樣式庫,如下圖所示
??????
??????拷貝ContentQueryMain[控制內容顯示]和ItemStyle【控制條目顯示】一份,重命名為自定義的名稱
?????6、修改webpart文件中的<property name="MainXslLink" type="string" >/Style Library/XSL Style Sheets/ContentQueryMain_OUWeb_Bulletin.xsl</property>
????????????? <property name="ItemXslLink" type="string" >/Style Library/XSL Style Sheets/ItemStyle_OUWeb_Bulletin.xsl</property>
??????這兩個配置節,樣式路徑選擇為自定義的樣式文件。
??????7、保存修改之后的webpart文件,在網站導入這個webpart文件。
??????8、效果出來了。
??????9、以后就是修改xsl樣式了,格式化顯示。
??????
?
??????在SP的開發過程中,有的時候首頁需要一些webpart來顯示子網站的內容,顯示一個標題,可以鏈接的,還有tooltip的,開始的時候我用content qeury web part ,后來發現他默認只能顯示標題,什么時間啊,用戶名啊,什么的都不能顯示。
??????過一段時間發現,我錯誤了,那個控件還是很強大的,詳見微軟的官網http://msdn.microsoft.com/en-us/library/bb447557.aspx。
???? 內容需要編輯ItemStyle.xsl,標題格式化需要編輯ContentQueryMain.xsl中的,下面是我添加的一個標題
???
?
?
ContentQueryMain<xsl:template?name="OuterTemplate">
????????<xsl:variable?name="Rows"?select="/dsQueryResponse/Rows/Row"?/>
????????<xsl:variable?name="RowCount"?select="count($Rows)"?/>
????????<xsl:variable?name="IsEmpty"?select="$RowCount?=?0"?/>
????????????<table?id="cbqwp"?cellspacing="0"?cellpadding="0"?border="1"?bordercolor="#0099FF"?class="cbq-layout-main"?title="論壇熱帖">
????????????????<tr?style="font-size:14px;background-color:#0099FF"><a?href="#"?style="font-weight:bolder;">論壇熱帖</a>
???????????????? <a?href="#">More</a>
?????????????????????<xsl:if?test="$FeedEnabled?=?'True'">
????????????????????????<div?class="cqfeed">
????????????????????????????<xsl:variable?name="FeedUrl1"?select="concat($SiteUrl,$FeedPageUrl,'xsl=1&web=',$WebUrl,'&page=',$PageId,'&wp=',$WebPartId)"?/>
????????????????????????????<a?href="{cmswrt:RegisterFeedUrl(?$FeedUrl1,?'application/rss+xml')}"><img?src="\_layouts\images\rss.gif"?border="0"?alt="{cmswrt:GetPublishingResource('CbqRssAlt')}"/></a>
????????????????????????</div>
????????????????????????</xsl:if>
????????????????</tr>
????????????????<tr>
????????????????????<xsl:choose>
????????????????????????<xsl:when?test="$IsEmpty">
?????????????????????????????<xsl:call-template?name="OuterTemplate.Empty"?>
?????????????????????????????????<xsl:with-param?name="EditMode"?select="$cbq_iseditmode"?/>
?????????????????????????????</xsl:call-template>
????????????????????????</xsl:when>
????????????????????????<xsl:otherwise>
?????????????????????????????<xsl:call-template?name="OuterTemplate.Body">
?????????????????????????????????<xsl:with-param?name="Rows"?select="$Rows"?/>
?????????????????????????????????<xsl:with-param?name="FirstRow"?select="1"?/>
?????????????????????????????????<xsl:with-param?name="LastRow"?select="$RowCount"?/>
????????????????????????????</xsl:call-template>
????????????????????????</xsl:otherwise>
????????????????????</xsl:choose>
????????????????</tr>
????????????</table>
???????????
????</xsl:template>
?
?
???? 效果圖如下
???
?
????? 可是不是很理想,More應該在右面,還有啊,內容是三欄,上面如何也弄三欄和下面的配合的,還需要研究啊?那位知道的話,可以告訴我啊,還有啊,還可以弄幻燈片效果,是http://www.cnblogs.com/ghner這位老兄說的,可是他沒有給出任務信息,http://www.cnblogs.com/ghner/archive/2008/06/16/1212775.html就是這個地址的效果,知道的老兄可以告知一下。
????? 剛才調整了一下ContentQuery.xsl文件中一些代碼的位置,標題就好看多了,見下圖,大家可以參看下面的代碼,自己做一些修改,實現更強大的控制。
?????? 剛開始我是在原來的table上面添加了一行,用來顯示標題,現在我是添加一個table在原來的table上面,好像好多了。
??????還有一點要注意,如果你修改自帶的ContentQueryMain,全部的CQWP都會有影響。要想消除這種影響,就要先將原有的拷貝一份,該一個名字,然后再修改。最后在.webpart文件中引用自定義的ContentQueryMain,格式為
????? <property name="MainXslLink" type="string">/Style Library/XSL Style Sheets/MyCustomCQMain.xsl</property>
????? MainXslLink為原來的一個屬性,原來的值為空,修改為上面的模式,MyCustomCQMain.xsl是自定義的文件名,還有其他的itemstyle,如果有需要的話,也最好自定義一些吧,這樣就對原來的沒有影響了。
???
代碼如下
?
?
ContentQueryMain.xsl中的部分<xsl:template?name="OuterTemplate">
????????<xsl:variable?name="Rows"?select="/dsQueryResponse/Rows/Row"?/>
????????<xsl:variable?name="RowCount"?select="count($Rows)"?/>
????????<xsl:variable?name="IsEmpty"?select="$RowCount?=?0"?/>
?????????<table?id="cbqwp"?cellspacing="0"?cellpadding="0"?border="1"?bordercolor="#0099FF"?class="cbq-layout-main"?title="論壇熱帖">
????????????????<tr?style="font-size:14px;background-color:#0099FF">
????????????????????<td><a?href="#"?style="font-weight:bolder;?font-size:14px;font-weight:bolder;">論壇熱帖</a>
????????????????????????????
?????????????????????</td>
?????????????????????<td>
?????????????????????????<a?href="#"?style="font-weight:bolder;?font-size:14px;font-weight:bolder;">More</a>
?????????????????????</td>
?????????????????????<td>
?????????????????????????<xsl:if?test="$FeedEnabled?=?'True'">
????????????????????????????<div?class="cqfeed">
????????????????????????????????<xsl:variable?name="FeedUrl1"?select="concat($SiteUrl,$FeedPageUrl,'xsl=1&web=',$WebUrl,'&page=',$PageId,'&wp=',$WebPartId)"?/>
????????????????????????????????<a?href="{cmswrt:RegisterFeedUrl(?$FeedUrl1,?'application/rss+xml')}"><img?src="\_layouts\images\rss.gif"?border="0"?alt="{cmswrt:GetPublishingResource('CbqRssAlt')}"/>RSS</a>
????????????????????????????</div>
????????????????????????????</xsl:if>
?????????????????????</td>
????????????????</tr>
????????</table>
????????????????
????????????<table?id="cbqwp"?cellspacing="0"?cellpadding="0"?border="1"?bordercolor="#0099FF"?class="cbq-layout-main"?title="論壇熱帖">
????????????????<tr>
????????????????????<xsl:choose>
????????????????????????<xsl:when?test="$IsEmpty">
?????????????????????????????<xsl:call-template?name="OuterTemplate.Empty"?>
?????????????????????????????????<xsl:with-param?name="EditMode"?select="$cbq_iseditmode"?/>
?????????????????????????????</xsl:call-template>
????????????????????????</xsl:when>
????????????????????????<xsl:otherwise>
?????????????????????????????<xsl:call-template?name="OuterTemplate.Body">
?????????????????????????????????<xsl:with-param?name="Rows"?select="$Rows"?/>
?????????????????????????????????<xsl:with-param?name="FirstRow"?select="1"?/>
?????????????????????????????????<xsl:with-param?name="LastRow"?select="$RowCount"?/>
????????????????????????????</xsl:call-template>
????????????????????????</xsl:otherwise>
????????????????????</xsl:choose>
????????????????</tr>
????????????</table>
???????????
????</xsl:template>
?
??????可以定制的,不過是用到處webpart文件,聲明要使用的欄目,然后編寫xslt,定義顯示的格式,可以多列,可以自定義一些東西,不錯。
? ? ? 在xslt中還可以用函數來處理,格式化時間什么的。有計劃以后寫一個系列,最近開始學習java web編程,從基礎開始學習,居然第一章就是xml、dtd、schema、xsl、xslt,所以才想起了SP中的xsl定義顯示的格式。
??????園子里面已經有很多的文章了,我就不寫步驟了。留個標記。
? ? ? 這位老兄總結的就很不錯,謝謝了先。 ? http://www.cnblogs.com/heavencloud/archive/2009/03/28/1423788.html
?
<xsl:template?name="ForumStyle"?match="Row[@Style='ForumStyle']"?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="LinkTarget">
????????????<xsl:if?test="@OpenInNewWindow?=?'True'"?>_blank</xsl:if>
????????</xsl:variable>
????????<div?id="linkitem"?class="item?link-item">
????????????<table?>
???????????????????<tr>
???????????????????????<td?width="200px">
???????????????????????????<xsl:if?test="string-length($DisplayTitle)?=?0">
???????????????????????????????<a?href="{$SafeLinkUrl}"?target="{$LinkTarget}"?title="{@Title}">
????????????????????????????????????<xsl:value-of?select="$DisplayTitle"/>
????????????????????????????</a>
???????????????????????????</xsl:if>
???????????????????????????<xsl:if?test="string-length($DisplayTitle)?<=?10">
???????????????????????????????<a?href="{$SafeLinkUrl}"?target="{$LinkTarget}"?title="{@Title}">
????????????????????????????????????<xsl:value-of?select="$DisplayTitle"/>
????????????????????????????</a>
???????????????????????????</xsl:if>
???????????????????????????<xsl:if?test="string-length($DisplayTitle)?>?10">
???????????????????????????????<a?href="{$SafeLinkUrl}"?target="{$LinkTarget}"?title="{@Title}">
????????????????????????????????????<xsl:value-of?select="substring($DisplayTitle,1,9)"/>
????????????????????????????</a>
???????????????????????????</xsl:if>????????????????????????
???????????????????????</td>
???????????????????????<td?width="100px">
???????????????????????????<xsl:value-of?select='substring(@Modified,1,4)'></xsl:value-of>
???????????????????????</td>
???????????????????????<td?align="right">回復數量:
???????????????????????????<xsl:value-of?select="@ItemChildCount"/>????
???????????????????????</td>
???????????????????</tr>
???????????</table>
????????????
????????</div>
????</xsl:template>
?
?
?
??????? 下面是我從網上找到的一些資料,記錄下來吧。
??????? http://blogs.msdn.com/ecm/archive/2006/10/25/configuring-and-customizing-the-content-query-web-part.aspx
??????? http://www.heathersolomon.com/blog/articles/customitemstyle.aspx
?
?
ItemStyle1/*?Item?Styles?-?Store?in?a?CSS?file?or?the?master?page?file*/????????
.S2StyleImage?{
????border-bottom:?1px?rgb(251,230,187)?solid;
????padding:?5px?7px?10px?0px;
????}
.S2StyleTitle?{
????padding-top:?10px;
????}
.S2StyleTitle?a,?.S2StyleTitle?a:link,?.S2StyleTitle?a:visited,?.S2StyleTitle?a:hover?{
????font-weight:?bold;
????font-size:?1.2em;
????}
.S2StyleDescription?{
????padding:?5px?0px?10px?0px;
????border-bottom:?1px?rgb(251,230,187)?solid;
????}
.S2StyleBorder?{
????border-bottom:?2px?rgb(251,230,187)?solid;
????}
<xsl:template?name="S2Article_ShowImageTitleBody"?match="Row[@Style='S2Article_ShowImageTitleBody']"?mode="itemstyle">
????????<xsl:variable?name="SafeImageUrl">
????????????<xsl:call-template?name="OuterTemplate.GetSafeStaticUrl">
????????????????<xsl:with-param?name="UrlColumnName"?select="'ImageUrl'"/>
????????????</xsl:call-template>
????????</xsl:variable>
????????<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="LinkTarget">
????????????<xsl:if?test="@OpenInNewWindow?=?'True'"?>_blank</xsl:if>
????????</xsl:variable>
????????
????????<xsl:variable?name="bodyContent">
????????????<xsl:call-template?name="removeMarkup">
????????????????<xsl:with-param?name="string"?select="@PublishingPageContent"/>
????????????</xsl:call-template>
????????</xsl:variable>
????????????????????????
???????<table?cellpadding="0"?cellspacing="0"?width="100%">
????????????<tr>
???????????????????<td?class="S2StyleTitle"?colspan="2">
????????????????????<xsl:call-template?name="OuterTemplate.CallPresenceStatusIconTemplate"/>?????
????????????????????????<a?href="{$SafeLinkUrl}"?target="{$LinkTarget}"?title="{@LinkToolTip}">
????????????????????????<xsl:value-of?select="$DisplayTitle"/>
????????????????????????</a>
????????????????</td>
????????????</tr>
????????????<tr?class="S2StyleBorder">
?????????????????<xsl:if?test="string-length($SafeImageUrl)?!=?0">
????????????????????<td?class="S2StyleImage">?
????????????????????????<a?href="{$SafeLinkUrl}"?target="{$LinkTarget}">
????????????????????????????<img?class="image"?src="{$SafeImageUrl}"?alt="{@ImageUrlAltText}"?/>
????????????????????????</a>
????????????????????</td>
????????????????</xsl:if>
????????????????<td?class="S2StyleDescription"?valign="top">
???????????????????<xsl:value-of?select="substring($bodyContent,1,100)"/>?(<a?href="{$SafeLinkUrl}"?mce_href="{$SafeLinkUrl}"?target="{$LinkTarget}"?title="{@LinkToolTip}">more</a>)
????????????????</td>
????????????</tr>
????????</table>
</xsl:template>????
<!--?Remove?Markup?from?escaped?page?content?-?Call?function?in?the?style?-->
<xsl:template?name="removeMarkup">
????<xsl:param?name="string"?/>
????<xsl:choose>
????<xsl:when?test="contains($string,?'<')">
????????<xsl:variable?name="nextString">
????????????<xsl:call-template?name="removeMarkup">
????????????<xsl:with-param?name="string"?select="substring-after($string,?'>')"?/>
????????????</xsl:call-template>
????????</xsl:variable>
????<xsl:value-of?select="concat(substring-before($string,?'<'),?$nextString)"?/>
????</xsl:when>
????<xsl:otherwise>
????????<xsl:value-of?select="$string"?/>
????</xsl:otherwise>
????</xsl:choose>
</xsl:template>
?
?
????? 在顯示富文本內容的時候,也就是html內容的時候,還可以去除html標記中的<br>之類的東西
?
ItemStyle2/*?Item?Styles?-?Store?in?a?CSS?file?or?the?master?page?file*/????????
.S2StyleImage?{
????border-bottom:?1px?rgb(251,230,187)?solid;
????padding:?5px?7px?10px?0px;
????}
.S2StyleTitle?{
????padding-top:?10px;
????}
.S2StyleTitle?a,?.S2StyleTitle?a:link,?.S2StyleTitle?a:visited,?.S2StyleTitle?a:hover?{
????font-weight:?bold;
????font-size:?1.2em;
????}
.S2StyleDescription?{
????padding:?5px?0px?10px?0px;
????border-bottom:?1px?rgb(251,230,187)?solid;
????}
.S2StyleBorder?{
????border-bottom:?2px?rgb(251,230,187)?solid;
????}
<xsl:template?name="S2Article_ShowImageTitleDescrip"?match="Row[@Style='S2Article_ShowImageTitleDescrip']"?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="SafeImageUrl">
????????????<xsl:call-template?name="OuterTemplate.GetSafeStaticUrl">
????????????????<xsl:with-param?name="UrlColumnName"?select="'ImageUrl'"/>
????????????</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="LinkTarget">
????????????<xsl:if?test="@OpenInNewWindow?=?'True'"?>_blank</xsl:if>
????????</xsl:variable>
????????<table?cellpadding="0"?cellspacing="0"?width="100%">
????????????<tr>
???????????????????<td?class="S2StyleTitle"?colspan="2">
????????????????????<xsl:call-template?name="OuterTemplate.CallPresenceStatusIconTemplate"/>?????
????????????????????????<a?href="{$SafeLinkUrl}"?target="{$LinkTarget}"?title="{@LinkToolTip}">
????????????????????????<xsl:value-of?select="$DisplayTitle"/>
????????????????????????</a>
????????????????</td>
????????????</tr>
????????????<tr?class="S2StyleBorder">
?????????????????<xsl:if?test="string-length($SafeImageUrl)?!=?0">
????????????????????<td?class="S2StyleImage">?
????????????????????????<a?href="{$SafeLinkUrl}"?target="{$LinkTarget}">
????????????????????????????<img?class="image"?src="{$SafeImageUrl}"?alt="{@ImageUrlAltText}"?/>
????????????????????????</a>
????????????????????</td>
????????????????</xsl:if>
????????????????<td?class="S2StyleDescription"?valign="top">
???????????????????????<xsl:value-of?select="@Description"?/>
????????????????</td>
????????????</tr>
????????</table>
</xsl:template>
<!--?Remove?Markup?from?escaped?page?content?-?Call?function?in?the?style?-->
<xsl:template?name="removeMarkup">
????<xsl:param?name="string"?/>
????<xsl:choose>
????<xsl:when?test="contains($string,?'<')">
????????<xsl:variable?name="nextString">
????????????<xsl:call-template?name="removeMarkup">
????????????<xsl:with-param?name="string"?select="substring-after($string,?'>')"?/>
????????????</xsl:call-template>
????????</xsl:variable>
????<xsl:value-of?select="concat(substring-before($string,?'<'),?$nextString)"?/>
????</xsl:when>
????<xsl:otherwise>
????????<xsl:value-of?select="$string"?/>
????</xsl:otherwise>
????</xsl:choose>
</xsl:template>
?
???????
?
圖1?此效果用2個webpart
Title 是內容編輯webpart
內容 是內容查詢webpart
?
需要修改2個地方
修改.webpart文件(把內容查詢webpart導出后,修改它)
?
??????1.?在頁面上添加一個內容查詢webpart,并查詢到所需內容,導出該webpart到本地:內容查詢webpart.webpart
2.?用記事本或者VS打開”內容查詢webpart.webpart”,編輯CommonViewFields 這個屬性并保存:
??????<property?name="CommonViewFields"?type="string">Title,Text;Modified,DateTime</property>
??????個字段中間一行要用分號分割
??????前面字段,后面類型
??????3.DataColumnRenames屬性修改為:
??????<property name="DataColumnRenames" type="string">Title,Title;Modified,Description</property>
??????因為ItemStyle.xsl只識別4個內容,Title,Description和一些鏈接,所以在這里,我們將Modified作為Description。
??????4.修改.ItemStyle.xsl樣式文件來呈現和格式化數據。
找到 ItemStyle.xls
新建一個xsl:template模板
?<xsl:template name="AA" match="Row[@Style='AA']" 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="SafeImageUrl">
??????????? <xsl:call-template name="OuterTemplate.GetSafeStaticUrl">
??????????????? <xsl:with-param name="UrlColumnName" select="'ImageUrl'"/>
??????????? </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="LinkTarget">
??????????? <xsl:if test="@OpenInNewWindow = 'True'" >_blank</xsl:if>
??????? </xsl:variable>
??????? <div id="linkitem" class="item">
??????????? <xsl:if test="string-length($SafeImageUrl) != 0">
??????????????? <div class="image-area-right">
??????????????????? <a href="/{substring-after(substring-after($SafeLinkUrl,'//'),'/')}" target="{$LinkTarget}">
??????????????????????? <img class="image" src="{$SafeImageUrl}" alt="{@ImageUrlAltText}" />
??????????????????? </a>
??????????????? </div>
??????????? </xsl:if>
??????????? <div class="link-item">
???????? <xsl:call-template name="OuterTemplate.CallPresenceStatusIconTemplate"/>
?????????????? <table style="width: 95%">
??????????? <tr>
?????????????? <td><img src="_layouts/images/square.gif"/></td>
?????????????? <td style="width: 60%">
??????????????? <a href="{$SafeLinkUrl}" target="{$LinkTarget}" title="{@LinkToolTip}">
??????????????????? <xsl:value-of select="concat(substring($DisplayTitle,1,$TitleLength),'...')"/>
?????????????????????<xsl:value-of select="$DisplayTitle"/>
??????????????? </a>
??????????????? </td>
??????????????? <td align="left">
??????????????? <div class="description">
??????????????????? <xsl:value-of select="ddwrt:FormatDateTime(string(@Modified),1033 ,'yyyy-MM-dd')" />
?????????????????? <xsl:value-of select="string(@Modified)"/>
??????????????? </div>?
??????????????? </td>
???????????? </tr>?????
???????????? </table>??????
??????????? </div>
??????? </div>
??? </xsl:template>
??????5.將ItemStyle屬性修改為
????????????<property name="ItemStyle" type="string">AA</property>
????????????其中的AA為剛才的Template名稱
關鍵地方用紅色字段表示,其中ddwrt需要在ItemStyle中引入名稱空間,如何引入請見下面實例
保存到樣式庫發布就OK
然后把修改好的內容查詢webpart 導入到webpart 庫就OK
然后把內容查詢webpart 添加到頁面.修改 在項目樣式里面選擇 AA 就可以看到效果..
轉自AA的博客,部分修改
更多請查看SDK:
http://msdn.microsoft.com/zh-cn/library/bb608445.aspx
Function相關請看:
http://msdn.microsoft.com/en-us/library/dd583143(office.11).aspx
如何添加"新按鈕"
http://sharepointuniversity.net/joed/archive/2008/02/03/display-a-quot-new-quot-indicator-in-the-content-query-web-part.aspx
I am using the Content Query Web Part (CQWP) to display news items from news subsites.? There are a few capabilities missing from the CQWP that were present in the old SharePoint 2003 News web part.? One of them is the display of the "New!" indicator next to items recently added.
You can get the CQWP to dipslay this indicator by editing the style templates for the web part.
To do this, you will need to use SharePoint Designer (SPD).? In SPD, open your top level site and expand Style Library, then XSL Style Sheets.? Look for ItemStyle.xsl.
ItemStyle.xsl holds the XSL templates that the CQWP uses to render data.? Before making changes, it's probably a good idea to make a copy of it by right-clicking on it and using Copy, then Paste.? If you screw up your ItemStyle.xsl file, you may get errors in any CQWP throughout your sites.
Double-click on ItemStyle.xsl to edit it and choose to check it out.
ItemStyle.xsl contains a series of templates.? Each template corresponds to an Item Style in the drop down in the CQWP properties.
For example, the in the out-of-the-box ItemStyle.xsl, the first template:
<xsl:template name="Default" match="*" mode="itemstyle">
corresponds to the "Image on left" style in the Item Style drop down in the CQWP.? The next template:
<xsl:template name="NoImage" match="Row[@Style='NoImage']" mode="itemstyle">
corresponds to the "Title and description" style.
You can either change one of the standard item styles or you can create a new style.? You may want to create a new style if you only want the CQWP to display the "New!" indicator when you choose (by selecting your new style).? If you change one of the standard templates, it will affect all of the CQWP's thoughout the site collection.
To create a new template,?highlight and copy one of the existing?templates, then paste it back into the file.? Then change the template name and match attributes to give it a unique name.? When you check in the ItemStyle.xsl file, you will see your new style as an option in the CQWP's Item Style drop down list.
Let's say we want to change the "Title and description" style?so it displays the "New!" indicator anywhere it is used.? The template name for this style is "NoImage".
The first thing we need to do is figure out when to display it.? We could use some fancy XSL to compare the Created Date of the item to the current date and do some date math to see if it has been created within (say) the last 24 hours.
But this is not necessary - SharePoint will do this for us!
SharePoint comes with an XSLT extension object that is normally used in Data View Web Parts (a.k.a. Data Form Web Parts).? But we can use those extensions in our ItemStyle.xsl also.
The functions in the XSLT extension object are described here.
The function we can use is ddwrt:IfNew().? When passed a date/time, it will return true if the date/time is less than two days old.
In order to use the extension object functions, we need to add the namespace to the ItemStyle.xsl file.? At the top of the file, it declares a number?of namespaces, such as:
? xmlns:x="http://www.w3.org/2001/XMLSchema"
? xmlns:d="http://schemas.microsoft.com/sharepoint/dsp"
? xmlns:cmswrt="http://schemas.microsoft.com/WebParts/v3/Publishing/runtime"
? xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt"
Add the "ddwrt" namespace in this mix:
? xmlns:ddwrt="http://schemas.microsoft.com/WebParts/v2/DataView/runtime"
Now we need to add some code to the template so it displays the "New!" indicator, which is a GIF found in the file "_layouts/1033/images/new.gif" (this may have a different location for?languages other than English).
Since we are changing the "NoImage" style, look for the start of that template:
??? <xsl:template name="NoImage" match="Row[@Style='NoImage']" mode="itemstyle">
Find the section of the template that generates the anchor tag (<a>) for the link to the item and displays the description:
??????? <div id="linkitem" class="item link-item">
??????????? <xsl:call-template name="OuterTemplate.CallPresenceStatusIconTemplate"/>
??????????? <a href="{$SafeLinkUrl}" mce_href="{$SafeLinkUrl}" target="{$LinkTarget}" title="{@LinkToolTip}">
??????????????? <xsl:value-of select="$MyDisplayTitle"/>
??????????? </a>
??????????? <div class="description">
??????????????? <xsl:value-of select="@Description" />
??????????? </div>
??????? </div>
We want to put the "New!" indicator just after the title of the item.? So change this section so it reads (new part in bold):
??????? <div id="linkitem" class="item link-item">
??????????? <xsl:call-template name="OuterTemplate.CallPresenceStatusIconTemplate"/>
??????????? <a href="{$SafeLinkUrl}" mce_href="{$SafeLinkUrl}" target="{$LinkTarget}" title="{@LinkToolTip}">
??????????????? <xsl:value-of select="$MyDisplayTitle"/>
??????????? </a>
??????????? <xsl:if test="ddwrt:IfNew(string(@Created))">
??????????????? <img?src="/_layouts/1033/images/new.gif" alt="New" />
??????????? </xsl:if>
??????????? <div class="description">
??????????????? <xsl:value-of select="@Description" />
??????????? </div>
??????? </div>
Save the file, then check it in.? Now anywhere you use the CQWP with the "Title and description" style, it will display a "New!" indicator if the item was added within the last two days.
?
?
?
轉載于:https://www.cnblogs.com/greeny/archive/2010/09/06/1819461.html
總結
以上是生活随笔為你收集整理的moss自定义内容查询webpart的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 数据库的小细节
- 下一篇: 运动,由Silverlight助力 /