SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
up vote 4 down vote accepted

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

share|improve this answer

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:

share|improve this answer
@rjcup3 I am simply using UserName() builtin function in infopath to retrieve the username. How would I implement this using Xpath – user7400 May 15 '12 at 17:21
You have the following XPath string functions to use: – Wictor Wilen MCA MCM MVP May 15 '12 at 17:45
Thanks guys! Got what I was looking for. I used substring-after(username(),"\") – user7400 May 15 '12 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. – RJ Cuthbertson May 15 '12 at 20:42
Assuming all usernames have a back slash could potentially break your solution, if you start adding federated accounts for instance – Wictor Wilen MCA MCM MVP May 16 '12 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;
share|improve this answer
Thanks for the post! – user7400 Oct 3 '12 at 19:30

This is an alternative solution:

share|improve this answer
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 – Robert Lindgren Nov 4 '14 at 10:15

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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