Monday, 22 August 2011

Getting today's date and current user in CQWP

If you've ever used a Data Form Web Part​ you'll know that you can set up some Parameter Bindings to pull in various SharePoint-specific values like querystring parameters and server/environment variables etc. It's only a small leap of faith to understand that since the Content by Query Web Part derives from the same web part these parameter bindings may also be available to the Content by Query Web Part and therefore able to be exposed in the XSL using the xsl:param command.

I've used the current Page Url and querystring in 2010 and have since used it in 2007 using the above tweaking for CQWPs hard-coded into a page layout. Two more useful values I came across were the User ID, which is the currently logged on user returned as a the User Name, and today's date returned using the ISO format we're familiar with in SharePoint. To use these, simply add a Parameter Binding as follows to a Parameter Bindings section in the CQWP parameters and then add an xsl:param into the XSL and hey-presto.

Add this section to your the CQWP HTML in your page:

<ParameterBinding Name="UserID" Location="CAMLVariableDefaultValue="CurrentUserName"/>
<ParameterBinding Name="Today" Location="CAMLVariableDefaultValue="CurrentDate"/>

and in the XSL add the following:

<xsl:param name="UserID />
<xsl:param name="Today" />

then use them as $Today and $UserID in the XSL.


  1. See also my more recent blog about Parameter Bindings and their wider use:

    1. This comment has been removed by the author.

  2. its not working in sharepoint designer 2010...

    1. You'll have to give me a little more information to go on Ajay...

    2. This comment has been removed by the author.