Retrieving SharePoint User Profile Properties in XSLT

A common request is to be able to retrieve user information from XSLT (XsltListViewWebPart, DataFormWebPart, etc). It is possible to obtain some information through server variables (LOGON_USER), CAML variables (UserID) or ddwrt:UserLookup extension function. It works, to some extent, but sometimes it is not enough.

It is well known that one can have server controls in XSLT stylesheets. As it happens, the ProfilePropertyValue control retrieves any value from the user profile. It just needs to have a ProfilePropertyLoader declared before it, which can be placed in the master page. For example:

   1: <SPSWC:ProfilePropertyLoader LoadFullProfileOfCurrentUser="true" runat="server"/>
   2: <SPSWC:ProfilePropertyValue PropertyName="FirstName" ApplyFormatting="false" runat="server"/>

Just remember to register the Microsoft.SharePoint.Portal.WebControls assembly with the SPSWC tag prefix, either in the Web.config or in the page itself.

But, what about XSLT? Well, if the SPSWC tag prefix is registered in Web.config, we can do this:

   1: <xsl:template name="GetProfilePropertyValue">
   2:     <xsl:param name="Property"/>
   3:     <SPSWC:ProfilePropertyValue PropertyName="{$Property}" ApplyFormatting="false" runat="server" />
   4: </xsl:template>
   5:  
   6: <xsl:variable name="Property">WorkEmail</xsl:variable>
   7:  
   8: <xsl:variable name="WorkEmail">
   9:     <xsl:call-template name="GetProfilePropertyValue">
  10:         <xsl:with-param name="Property" value="$Property"/>
  11:     </xsl:call-template>
  12: </xsl:variable>
  13:  
  14: Work Email: <xsl:copy-of select="$WorkEmail"/>

With this technique, you can retrieve any of the profile properties. Ah, don’t forget to add:

   1: xmlns:spswc="Microsoft.SharePoint.Portal"

To the <xsl:stylesheet> declaration.

Happy SharePointing! Winking smile




                             

2 Comments

Add a Comment

As it will appear on the website

Not displayed

Your website