How can I modify the UserName() return string to display only the username as opposed to "i:0#.w|Domain\UserName"?

When I had the same form in my development environment it returned Domain\UserName and none of the prefix characters (i:0#.w|). Once I moved it to the production though I get this now: i:0#.w|Domain\UserName

I am open to using other alternatives as well.

5 Answers 5


Try a substring after "\" for the user name, or after "|" for the domain and user name.



The reason you're seeing this is that your production farm uses Claims AuthN whereas your dev enviroment the classic mode. You should probably make your code handle both scenarios.

Here are some details about the claims encoding: http://www.wictorwilen.se/Post/How-Claims-encoding-works-in-SharePoint-2010.aspx

  • @rjcup3 I am simply using UserName() builtin function in infopath to retrieve the username. How would I implement this using Xpath
    – user7400
    May 15, 2012 at 17:21
  • 1
    You have the following XPath string functions to use: msdn.microsoft.com/en-us/library/ms256180(v=vs.90).aspx May 15, 2012 at 17:45
  • 1
    Thanks guys! Got what I was looking for. I used substring-after(username(),"\")
    – user7400
    May 15, 2012 at 18:11
  • Wictor is right in saying you should handle this issue for both scenarios. Wish I would have seen your comment earlier but it wasn't on my answer so it didn't get flagged, sorry. May 15, 2012 at 20:42
  • Assuming all usernames have a back slash could potentially break your solution, if you start adding federated accounts for instance May 16, 2012 at 6:18

While researching this myself, I found a solution that might be a bit better than a substring. You might want to try the following:

SPClaimProviderManager manager = SPClaimProviderManager.Local;

if (manager != null)
    if (SPClaimProviderManager.IsEncodedClaim(encodedString))
        username = manager.DecodeClaim(encodedString).Value;
        username = encodedString;

You will have to parse the username from the returned string as explained by RJ above. But you might want to show the Display name instead of user name. you will need to query the user profile service from infopath to extract the Display Name for that username().


This is an alternative solution: http://zimmergren.net/technical/tip-getting-the-normal-domain-username-from-the-claims-username-in-sharepoint-2013

  • Please do not post link-only answers! Instead add details to your answer from the link you reference so that the link is mearly a source to the complete information Nov 4, 2014 at 10:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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