6

I'm trying to define a display template for one of my site columns by using XSLT. One of the things I would need is the loginname (DOMAIN\user) of the currently logged in user.

There's one way described here, using parameterbindings. But I'm trying to make this as generic as possible, so setting the parameterbindings for each web part is unrealistic.

I found a way to get the user's ID on this page. And, based on the ddwrt namespace I discovered the method UserLookup

So I tried the following snippet:

<xsl:value-of select="ddwrt:UserLookup,($Userid,'ID')" />

But unfortunately this just returns a blank string. Where am I going wrong? If my current approach won't work, what other way is there?

5
  • 1
    You can try to use <xsl:value-of select="$UserID" /> to display the current username. Sep 4 '14 at 7:31
  • I tried that but got an XslLoadException that the parameter 'UserID' doesn't exist. 'Userid' does exist, but that's the ID of the user (the number), not the loginname. Sep 4 '14 at 7:36
  • This might help you: blogs.msdn.com/b/joshuag/archive/2008/06/03/… Sep 4 '14 at 7:45
  • You might have to add the parameter binding; see my answer below.
    – bgmCoder
    Dec 10 '15 at 16:59
  • I've tried this - it can work in a DispForm.aspx without issue, but it doesn't work in fldtypes.xsl - I think because the regular list-views do not include the parameter binding.
    – bgmCoder
    Dec 10 '15 at 17:02
3

You have to do a couple things.

First, you might need to make sure your xsl connector has this parameter binding:

<ParameterBinding Name="UserID" Location="CAMLVariable" DefaultValue="CurrentUserName"/>

And make sure this is in there:

xmlns:ddwrt="http://schemas.microsoft.com/WebParts/v2/DataView/runtime"

Then, add this parameter to give your xsl access to that value:

<xsl:param name="UserID"/>

Now, in your xsl, you can do this:

UserID: <xsl:value-of select="$UserID"/><br />
<xsl:value-of select="ddwrt:UserLookup(string($UserID) ,'ID')" /><br />
<xsl:value-of select="ddwrt:UserLookup(string($UserID) ,'EMail')" />

I figured this out from this page and from here, especially. I tried this in a custom DispForm.aspx page (because it was an easy place to test) and it works like a charm.

2

Try this.

<xsl:value-of select="ddwrt:UserLookup(string($Userid) ,'ID')" />

You had "," after UserLookup.

0

I use a different way to do the same thing. I use the _spPageContextInfo (an object that is possible to return in every sp page).

Example for returning name of Current User:

<script type="text/javascript"​>  
var userid= _spPageContextInfo.userId;  
var requestUri = _spPageContextInfo.webAbsoluteUrl + "/_api/web/getuserbyid(" + userid + ")";             
var requestHeaders = { "accept" : ​"application/json;odata=verbose" };   
$.ajax({
        url : requestUri,
        contentType :"application/json;odata=verbose",
        headers : requestHeaders,
        success : onSuccess,
        error : onError   }); 
function onSuccess(data, request)
{  ​
var loginName = data.d.Title;
    alert(loginName);   
}  
function onError(error) 
{
     alert("error");   
} ​​
</script>

More about _spPageContextInfo:

Click Here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.