27 02 2009
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.
- Open Windows Live ID Authentication project with Visual Studio.
- Open file Code/liveinfo.cs.
- Move to Submit_Click() method and find the following line:
- 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(
- 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;
web.AllowUnsafeUpdates = false;
site.AllowUnsafeUpdates = false;
- 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.