SharePoint: creating SharePoint accounts for Live ID users

Couple of weeks ago I started adding Windows Live ID authentication support to SharePoint. I used Community Kit for SharePoint and I also made here some notes about it. There was one problem – although user is authenticated there is no SharePoint user and it is not possible assign it to any roles. Here is some advices how to create SharePoint user account when new user is registered after logging in using Live ID.

  1. Open Windows Live ID Authentication project with Visual Studio.
  2. Open file Code/liveinfo.cs.
  3. Move to Submit_Click() method and find the following line:


    bup.Update();


  4. Add the following code after line mentioned above. This code runs method AddNewSPUser then creates user account also to SharePoint. This way you achieve link between LiveID authenticated user account and SharePoint user account.

    var sec = new SPSecurity.CodeToRunElevated(

        delegate()

        {

            AddNewSPUser(User.Identity.Name, tbDisplayName.Text);

        });

    SPSecurity.RunWithElevatedPrivileges(sec);


  5. Add the following method to the end of the class:

    private static void AddNewSPUser(string userName, string name)

    {

        using (SPSite site = new SPSite(SPContext.Current.Web.Url))

        {

            using (SPWeb web = site.RootWeb)

            {

                site.AllowUnsafeUpdates = true;

                web.AllowUnsafeUpdates = true;

     

                SPUser user = web.EnsureUser(userName);

                user.Name = name;

                user.Update();

                web.SiteGroups["MySite visitor"].AddUser(user);

                web.Update();

                web.AllowUnsafeUpdates = false;

            }

            site.AllowUnsafeUpdates = false;

        }

    }


  6. Compile the code and deploy it to server. Also don’t forget to recycle application bool of web application to where you deployed the solution.

Now, after new user joins your site, the new user account is also created to SharePoint. By default the new user account is assigned to “MySite visitor” group. Make sure you have this group created or change the name of default group before compiling the code.


One thought on “SharePoint: creating SharePoint accounts for Live ID users

Leave a Reply

Your email address will not be published. Required fields are marked *