扩展内容查询WebPart的XSLT函数

【注:摘自Waldek Mastykarz的博客】

Earlier this month I wrote that SharePoint developers working with the Content Query Web Part (CQWP) are provided a mechanism to extend the information available in the XSLT transformations. While exploring the CQWP even further I found out that you could create custom XSLT functions as well! Can you imagine the possibilities?

It's probably a common knowledge by now, that the CQWP uses XSLT for creating the presentation layer for the queried data. The default set of the functions provided with XSLT is limited. While the ddwrt namespace adds some very useful functionality to the CQWP there are still many situations when you might need something else that isn't there like Regular Expressions for example. Many SharePoint developers I know would decide to create a custom Web Part at this point. Knowing how great the CQWP performs I would extend the standard functionality instead.

[If you want to extend the Content Query Web Part XSLT transformations with extra functionality, there are several points where you need to hook into:]

如果你想用更多的函数扩展Content Query Web Part的 XSLT transformations,以下几点你必须要做:

  • the Content Query Web Part, to make the custom methods available in the XSLT
  • custom class to define the methods
  • the XSL file (ItemStyle.xsl, ContentQueryMain.xsl and/or Header.xsl) to make it aware of the custom functions

Getting Started

The very first thing you want to do is to extend the Content Query Web Part to leverage the custom functionality and in my case to setup the project.

The beginning: extending the Content Query Web Part

As I don’t use anything else than MSBuild I have added a custom action to the build tasks of the project to make it create a WSP for me using the DDF file from the Solution directory.

After getting the first build up and running, and confirming that the extended version of the Content Query Web Part is available in SharePoint, we can proceed with expanding the functionality of the CQWP.

Confirm that the Extended Content Query Web Part is available in the Web Part gallery

Defining the custom functions

The next step is defining and programming the various functions you want to make available in the XSLT used by the Content Query Web Part. Please note that you can define virtually unlimited number of custom functions and namespaces, so be wise with splitting them in various namespace to keep it all maintainable. For the brevity I will create just one function in this article: generating a random number.

Let’s start off with programming the .NET method containing the logic for the XSLT function.

Programming the singleton class containing custom XSL functions and the Random method

As already mentioned I have defined only one custom function for this example which returns a random number from the given range. While adding your custom functions to the Content Query Web Part you need to pass an instance of the class which contains the custom functions. To keep yourself from constantly re-instantiating the functions class, you can use the Singleton pattern to store the reference to the once instantiated class – just as I did in the example code above.

Adding the functions to the Content Query Web Part

After having defined and programmed the custom functions it is time to make the CQWP aware of them. To add your custom functions to the Content Query Web Part you need override the ModifyXsltArgumentsList method – the same which we used earlier to add some custom parameters. This time for a change you will be using the ArgumentClassWrapper.AddExtensionObject method to define the link between the CQWP and the class which contains the custom functions.

Adding the custom functions to the Extended Content Query Web Part

As the parameters you have to pass the namespace: unique value which will identify your custom class and a reference to the instance of the class.

Half way there

By completing the steps I’ve mentioned above you’re done with custom coding. The last thing you need to do is to make the XSLT files actually use your custom functions.

Test Content

Let’s say you want to display a random article on each reload of a page. What you need to do is to retrieve all the articles that you’re interested in and pick one random page to be displayed. As you might suspect: the first part can be done using the standard CQWP. In order to pick the random page you will use the custom function you have just created.

Let’s start with adding a few pages to be able to test the extended CQWP. I have used the Imtech Test Content Generator for this purpose but doing it manual is ok too.

Generating test content using the Imtech Test Content Generator

Modifying XSLT

Now it’s time to make the custom functions we have created available in the XSLT. First of all let’s create a copy of the existing ItemStyle.xsl and ContentQueryMain.xsl files. You can find them either in /Style Library/XSL Style Sheets of your Publishing Site Collection or in the 12\TEMPLATE\FEATURES\PublishingResources directory on your web server.

Please notice that in a regular scenario you would modify the ItemStyle.xsl only as it’s the file that contains item-level formatting. In our situation however, first of all you want to select one item only and the apply the formatting to it. The ContentQueryMain.xsl is the place where you can intercept applying formatting to the different items.

Let’s now define a custom prefix for our functions which will allow us to call the functions in CustomContentQueryMain.xsl:

Defining the prefix for the custom XSL functions in CustomContentQueryMain.xsl

There are two things you need to do here. First of all you need to define the prefix which you will use to call your custom functions. Then, to simplify working with your custom prefix, you will exclude the prefix from the output.

As I mentioned before: CustomContentQueryMain.xsl is responsible for iterating through all retrieved results and applying formatting to them. If you want to change the default behavior, this is the place where it can be done. Modify the standard ContentQueryMain.xsl file as shown on the picture below:

Adding the logic required to display a random page inside CustomContenQueryMain.xsl

The first change (line 34) defines a new variable which contains a random number between 1 and the number of pages retrieved by the Content Query Web Part. The second modification (lines 44 – 50) apply template to the randomly selected item.

In this scenario we will focus on using the custom functions in XSLT rather than customizing the formatting of items. All you need to do in the CustomItemStyle.xsl file is to make a copy of the Default template and give it a new name, for example: RandomItem. You will use it to help the CQWP determine whether it should display all items as returned by the query or a single – randomly chosen item only.

After you have done the above modifications it’s time to upload both XSL files to the /Style Library/XSL Style Sheets directory on your Site Collection. You can do this either by uploading the files or by using a Feature. In a real life scenario you would provision these file alongside other files from your solution.

Displaying random item in the Content Query Web Part

By default the Content Query Web Part uses the ItemStyle.xsl and the ContentQueryMain.xsl files. Unfortunately there is no interface to make the CQWP use different resources. You can do this however by adding an instance of the CustomFunctionsCQWP on the page, exporting it to XML, changing the values and importing it on the page again. This process has been described in detail in the How to: Customize the Content Query Web Part using Custom Properties article on MSDN.

After re-importing the Web Part don’t forget to set the Item Style to RandomItem (or whatever name you have chose). This will make the Content Query Web Part display a randomly chosen item.

Choose your custom Item Style to enable displaying random items

If you’ve followed the instructions above correctly you should see something similar to this on your page:

Result: Extended Content Query Web Part displaying a randomly chose page

每一次你重新载入这个页面就会显示不同的文章 。

Summary

The Content Query Web Part shipped with Microsoft Office SharePoint Server 2007 is a very powerful content aggregation solution. It not only aggregates content in a very performing way but it also provides you advanced caching mechanisms which make it perfect for many different scenarios. By extending the Content Query Web Part with custom XSL functions, you can make it more suitable to your challenges and make even more benefit of using it within your solutions.

转载于:https://www.cnblogs.com/jacobchan/archive/2012/12/25/2831996.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
资源包主要包含以下内容: ASP项目源码:每个资源包中都包含完整的ASP项目源码,这些源码采用了经典的ASP技术开发,结构清晰、注释详细,帮助用户轻松理解整个项目的逻辑和实现方式。通过这些源码,用户可以学习到ASP的基本语法、服务器端脚本编写方法、数据库操作、用户权限管理等关键技术。 数据库设计文件:为了方便用户更好地理解系统的后台逻辑,每个项目中都附带了完整的数据库设计文件。这些文件通常包括数据库结构图、数据表设计文档,以及示例数据SQL脚本。用户可以通过这些文件快速搭建项目所需的数据库环境,并了解各个数据表之间的关系和作用。 详细的开发文档:每个资源包都附有详细的开发文档,文档内容包括项目背景介绍、功能模块说明、系统流程图、用户界面设计以及关键代码解析等。这些文档为用户提供了深入的学习材料,使得即便是从零开始的开发者也能逐步掌握项目开发的全过程。 项目演示与使用指南:为帮助用户更好地理解和使用这些ASP项目,每个资源包中都包含项目的演示文件和使用指南。演示文件通常以视频或图文形式展示项目的主要功能和操作流程,使用指南则详细说明了如何配置开发环境、部署项目以及常见问题的解决方法。 毕业设计参考:对于正在准备毕业设计的学生来说,这些资源包是绝佳的参考材料。每个项目不仅功能完善、结构清晰,还符合常见的毕业设计要求和标准。通过这些项目,学生可以学习到如何从零开始构建一个完整的Web系统,并积累丰富的项目经验。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值