Welcome to my blog, stay tunned :
Home | Blogs | Stephane Eyskens's blog

SharePoint 2010 - Implementing a custom header pattern via XSL for a custom field type

Hi,

If you wonder how to implement a custom header pattern in XSL for a custom field type in either SharePoint Foundation or SharePoint Server, here is an example that you can download showing how to do.

In this little example, we'll implement a custom field type inheriting from Text that will show the number of entries having that same value within the list. You could achieve this by creating a view that groups the elements on that field & shows the totals but this is just to show an example :).

So, here is a screenshot of the functionality

When you click on the value contained in that specific column, you get an alert message showing you how many items in the list have the same value for that column. In this example, when you click on value 1, you get 2 since two rows contain this value as shown below:

Start by creating a new empty SharePoint Project and add this class. This is just a basic C# squeletton defining the custom field type deriving from SPFieldText:

then, add a SharePoint mapped folder to your project that points to TEMPLATE\XML where the XML file describing the custom field type must be defined (no difference with 2007):

then, add another SharePoint mapped folder that points to TEMPLATE\LAYOUTS\XSL that contains the custom logic to render the field using XSL:

The first XSL template renders the field. It shows an hyperlink together with a picture. When clicking on the link, the javascript function GetEntriesCount is called and the current field value is passed in parameter.

The javascript function is declared in the other XSL template that matches the header. The first part of the template consists in displaying the default column header (Title of the column, filtering, sorting...). The secund part of the template contains the javascript code which makes use of the ECMAScript client object model to query SharePoint and to get the total number of items having the same value.

The javascript function is declared in the header mode (previously known as headerpattern in CAML) in order to declare the function only once.

The Text_body mode is called for each list item, that's why it's not a good idea to define the javascript functions in that template since it would be declared as many times as the number of items displayed on the page. Although this would work, it's better to avoid it.

Note that at the time of writing, defining an XSL template only renders the field in list views not in the display/edit/new forms for which you should still use CAML or use code-behind (RenderFieldForDisplay for the display form) and .ascx user controls for the new/edit forms.

Happy coding!

<?xml version="1.0" encoding="utf-8"?>
version="1.0" exclude-result-prefixes="xsl msxsl ddwrt"
xmlns:ddwrt="http://schemas.microsoft.com/WebParts/v2/DataView/runtime"
xmlns:asp="http://schemas.microsoft.com/ASPNET/20"
xmlns:__designer="http://schemas.microsoft.com/WebParts/v2/DataView/designer"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt"
xmlns:SharePoint="Microsoft.SharePoint.WebControls" xmlns:ddwrt2="urn:frontpage:internal">













x:string

<script language="javascript">
function GetEntriesCount(str) {
var ClientContext = SP.ClientContext.get_current();
var CamlQuery = new SP.CamlQuery();
CamlQuery.set_viewXml('




'+str+'



');
this.Results = ClientContext.get_web().get_lists().getById(
SP.ListOperation.Selection.getSelectedList()).getItems(CamlQuery);
ClientContext.load(this.Results, 'Include(Title)');
ClientContext.executeQueryAsync(Function.createDelegate(this, this.QuerySucess), Function.createDelegate(this, this.QueryFailure));
}
function QuerySucess(sender, args) {
alert(this.Results.getEnumerator().$1k_0);
}
function QueryFailure(sender, args) {
alert(args.get_message());
}


Comments

Nice one

Really nice and clean example. I love it!
Helped me to find the way to attach xslt template to field type, not to the field name.
Thanks

Thanks stephane! XSLT example

Thanks stephane! XSLT example really helped.

Great post

Thanks for sharing that, the web lacks examples about that!