Monday, January 26, 2015

Strange user login name in SharePoint when using Claims?

Hey, if you are like me and use claims to login to your SharePoint, you probably noticed the user names went nuts.

Having a user name like domain\user will now return something more like i:0#.w|domain\user if you are lucky, but I’ve seen stranger formats as well…

Now, say a user sends you a login name (domain\user) and you need to get this value and set it to the assigned to of a task, or set permissions for this user – you will quickly find out you cannot get an SPUser object from this standard login name.

Also, when you want to send the login name to another system or work with it in your code – you might want to get just the domain\user part, without the claims decorations.

True, the claims login formats are documented and you can hope to parse the format yourself to extract the user login from it, but I recently found a hidden (to me at least) gem in SharePoint API that can help with this task.

First, start using this namespace: Microsoft.SharePoint.Administration.Claims;

Now, you can work with SPClaimProviderManager object to check if a string is a claims encoded user name, and convert it to a standard user name, and the other way around.

I’ve created this simple utility class for me to use in my code for now, here is a code sample explaining how to use this object:

public class ClaimsHelper
{
    public static string ClaimsToLogin(string login)
    {
        try{
            if(SPClaimProviderManager.IsEncodedClaim(login))
                return SPClaimProviderManager.Local.ConvertClaimToIdentifier(login);
        }
        catch{
            //log error, return the origina value we got
        }
        return login;
    }
    public static string LoginToClaims(string login)
    {
        try
        {
            if (!SPClaimProviderManager.IsEncodedClaim(login))
                return SPClaimProviderManager.Local.ConvertIdentifierToClaim(login, SPIdentifierTypes.WindowsSamAccountName).ToEncodedString();
        }
        catch
        {
            //log error, return the origina value we got
        }
        return login;
    }
}

(Code is for example purposes only, use at own risk, feel free to change the names of the helpers)


Notice I’m expecting to get a windows login user name (SPIdentifierTypes.WindowsSamAccountName) but you can change this to support your own authentication.


Good luck, hope this help.

Tuesday, January 20, 2015

Proudly Canadian?

Great! So am I.
So, visit our site and get your choice of a free SharePoint product:
http://www.kwizcom.com/special-offers/canadieneh/
Canadian_campaign_800_320
My personal favorites you should be looking at:
  1. Data View Plus – since it is the Swiss army knife of all SharePoint viewers out there!
  2. List inline editor – I use it all the time, this one saves me literally hours of data entry or updating bugs every single day.
  3. Forms w/Custom actions – need I say more?? If you don’t know what its all about, check out this quick video http://www.kwizcom.com/sharepoint-add-ons/sharepoint-list-custom-actions/overview/
  4. Wiki Plus – Company procedures? Publish articles? Want to be able to use *real* wiki language with quick TOC?
Wow – it was hard coming up with just 4, anyway – one is yours to keep for free. Sound off in the comments for questions or feedback (guys, no support questions here please – for that visit http://support.kwizcom.com/Default.aspx or http://forum.kwizcom.com/Default.aspx as I can’t really help you in the comments)