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

I'm facing a problem with Sharepoint as I must change the Login name of my users. I'm in a claim based installation and I decide to use GUID as unique identifier.

By Sharepoint PowerShell I found how to do it and works.. How can I change it programmatically? LoginName properties seems to be read only!

Here's the way I rename it on Powershell:

Move-SPUser -Identity $User2 -NewAlias "i:05.t|saml provider|MYBASE65GUID==@DOMAIN.dom" -IgnoreSID

Anybody knows a way to do that programmatically in Sharepoint?

Thank you!

share|improve this question
add comment

3 Answers 3

See the ISPUserMigrator interface.

share|improve this answer
    
Suggestion on how to use it? I can see no example over there! –  Ziba Leah Oct 29 '12 at 19:25
add comment
up vote 1 down vote accepted

To modify the login name of an User I used the SPFarm object.

The code looks simple like this:

 Microsoft.SharePoint.SPSecurity.RunWithElevatedPrivileges(delegate()
             {
                using (SPSite elevatedSite = new SPSite(url))
                {
                    using (SPWeb web = elevatedSite.OpenWeb())
                    {
                    foreach (Principal user in ps.FindAll()) //Searches the user in LDAP
                    {
                        if (user.UserPrincipalName == EmailUser)
                        {
                             var BASE64 = System.Convert.ToBase64String((user.Guid.Value).ToByteArray()); //Convert the guid in BASE64
                            SPUser mi = web.EnsureUser("i:05.t|saml provider|"+EmailUser); //get the user I want to change
                            SPFarm farm = SPFarm.Local; //get the local farm
                            farm.MigrateUserAccount(mi.LoginName, "i:05.t|saml provider|" + BASE64 + "@fakemail.dom", false);
                            break;
                        }
                    }
                    }
                }
            });

In this way the user has a new LoginName as I wanted.

share|improve this answer
add comment

SPWeb.EnsureUser(loginName)

This will create the user within SharePoint if it doesn't already exist (i.e. the user should be allowed access but hasn't actually visited the site).

share|improve this answer
    
But I want to "rename" an user not to create! –  Ziba Leah Oct 29 '12 at 16:45
1  
Ah, I was confused, I thought you were trying to get an SPUser object given a login name. I will edit the title to make it clearer. –  lgaud Oct 29 '12 at 17:45
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.