Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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
add comment

3 Answers

up vote 3 down vote accepted

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

http://office.microsoft.com/en-us/infopath-help/functions-in-infopath-HP001155281.aspx

share|improve this answer
add comment

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;
    else
        username = encodedString;
}
share|improve this answer
1  
Thanks for the post! –  user7400 Oct 3 '12 at 19:30
add comment

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

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
1  
You have the following XPath string functions to use: msdn.microsoft.com/en-us/library/ms256180(v=vs.90).aspx –  Wictor Wilen MCA MCM MVP May 15 '12 at 17:45
1  
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
add comment

Your Answer

 
discard

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.