1

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!

1

See the ISPUserMigrator interface.

  • 1
    Suggestion on how to use it? I can see no example over there! – Ziba Leah Oct 29 '12 at 19:25
1

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.

0

Actual code for this Command is under Microsoft.SharePoint.Powershell.dll and I used "ILSpy" to see the code under these command. which is this.

using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint.PowerShell.Resources;
using System;
using System.Management.Automation;

namespace Microsoft.SharePoint.PowerShell
{
    [Cmdlet("Move", "SPUser", SupportsShouldProcess = true, ConfirmImpact = ConfirmImpact.High)]
    internal sealed class SPCmdletMoveUser : SPCmdlet
    {
        private SPUserPipeBind m_UserPipeBind;

        private SPUser m_User;

        private string m_OldAlias;

        private string m_NewAlias;

        private SwitchParameter m_IgnoreSid;

        [Alias(new string[]
        {
            "UserAlias"
        }), Parameter(Mandatory = true, ValueFromPipeline = true, Position = 0), ValidateNotNull]
        public SPUserPipeBind Identity
        {
            get
            {
                return this.m_UserPipeBind;
            }
            set
            {
                this.m_UserPipeBind = value;
            }
        }

        [Parameter(Mandatory = true), ValidateNotNullOrEmpty]
        public string NewAlias
        {
            get
            {
                return this.m_NewAlias;
            }
            set
            {
                this.m_NewAlias = value;
            }
        }

        [Parameter(Mandatory = false)]
        public SwitchParameter IgnoreSID
        {
            get
            {
                return this.m_IgnoreSid;
            }
            set
            {
                this.m_IgnoreSid = value;
            }
        }

        protected override string ConfirmationMessage
        {
            get
            {
                if (this.m_User == null)
                {
                    return this.m_OldAlias;
                }
                return this.m_User.ToString();
            }
        }

        protected override void InternalValidate()
        {
            if (this.Identity != null)
            {
                this.m_User = this.Identity.Read();
                this.m_OldAlias = ((this.m_User != null) ? this.m_User.LoginName : this.Identity.RawIdentity);
            }
            if (string.IsNullOrEmpty(this.m_OldAlias))
            {
                base.WriteError(new PSArgumentNullException("Identity", StringResourceManager.GetResourceString("SPUserPipeBindRequired", new object[0])), ErrorCategory.InvalidArgument, null);
                base.SkipProcessCurrentRecord();
            }
        }

        protected override void InternalProcessRecord()
        {
            SPFarm localOrThrow = SPFarm.LocalOrThrow;
            localOrThrow.MigrateUserAccount(this.m_OldAlias, this.NewAlias, !this.IgnoreSID.IsPresent);
        }
    }
}
-1

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).

  • 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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