Sunday, November 2, 2008

Add FBA users to sharepoint programmatically

Scenario:
You want to add FBA users to one of the SharePoint Group

Solution:
1) Create FBA Membership User
2) Create SharePoint User
3) Get your site instance
4) Add SharePoint User to a Desired group

Code:


MembershipUser membershipUser = Membership.CreateUser(login, password, email);

SPWeb spWeb = SPControl.GetContextWeb(HttpContext.Current);

SPUser spUser = spWeb.EnsureUser(login);
SPGroup spGroup = spWeb.SiteGroups["VISITORS"];
spGroup.AddUser(spUser);

Possible Errors:
[SPException: The user does not exist or is not unique.]
or 
When You try to put this new user into a sp group, the web is redirected to the login page


Article:
http://social.msdn.microsoft.com/forums/en-US/sharepointdevelopment/thread/db46ab8f-9594-4878-9bab-a18fe3fba959/

13 comments:

Anonymous,  August 16, 2010 at 8:45 AM  

Use SPSecurity.RunwithElevatedPrivileges and also while giving permission add the MembershipProvider with ":" separated User name

Anonymous,  February 16, 2011 at 3:45 PM  

how to do this if i am running the above code from a console app.

Anonymous,  February 16, 2011 at 3:47 PM  

what if the above code is put in a console app and run. Will ensureuser will work there as currently is throwing error "User doesnot exists". FYI I am using the using statement to get the SPSite object.

Sandeep February 16, 2011 at 9:27 PM  

hmm.. I will try on my end over weekend but as long as you were able to create a SPSite object , the code should work.

Rockie June 10, 2011 at 6:01 AM  

Doesnt worked for me. Can You post real working example in console Application? I tried so:

SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite site = new SPSite("http://localhost/"))
using (SPWeb spWeb = site.OpenWeb())
{

try
{
MembershipUser membershipUser = Membership.CreateUser(login, password);

// fba_member is providr from web.config, but it doesn't help
SPUser spUser = spWeb.EnsureUser("fba_member:" + login);
SPGroup spGroup = spWeb.SiteGroups["FBA_Users"];
spGroup.AddUser(spUser);
}
catch (Exception ex)
{
System.Console.WriteLine(ex.ToString());
}
}
});

Sandeep June 10, 2011 at 6:03 AM  

This is a working example. I usually only code from my projects.

But I will take a look over weekend.
Also you can try adding a FBA user first from Sharepoint UI , and then note the format { membershipprovidername:username } , usually that causes issue.

Rockie June 10, 2011 at 6:35 AM  

Sandeep K Nahta, thank You for Your response!

I mean working with real values of user, membership provider, etc..

I'm getting an error in translate it will be like "password-answer is invalid"

If You mean solution from codeplex as sharepoint UI, I can't install it on this server. So I wanted to create simple console application to add one User to my FBA.

Rockie June 15, 2011 at 3:55 AM  

Sandeep K Nahta, thanks! It works if we add to our console project app.config file with connectionstring and membership and role providers. ;)

Jigar,  July 13, 2011 at 5:29 PM  

Hello Sandeep,

I am having exact same problem as rookie but i am not sure how he got it to work. I am getting User dont exist or not unique exeption. Can you please help me here?

Rockie July 14, 2011 at 9:30 AM  

Jigar, put file app.config file in same folder where your .csproj file is. In my case this file is:

K.Anantharengan July 22, 2011 at 12:38 PM  

hi sandeep I am getting the following exception when running your code. How to get rid of this exception?. Thanks in advance
[SPException: The user does not exist or is not unique.]

K.Anantharengan July 22, 2011 at 12:39 PM  

hi sandeep I am getting the following exception when running your code. How to get rid of this exception?. Thanks in advance
[SPException: The user does not exist or is not unique.]

Anonymous,  August 9, 2011 at 11:02 AM  

I am trying to set up a user self-registration process for a SharePoint site using Forms/Claims Authentication.

The process is, however failing at the line:

SPUser spUser = spWeb.EnsureUser(login);

Where I am re-directed to the login screen.

This appears to be due to the fact that during registration no user is not logged in - i.e. the site context is anonymous and that the process does not have sufficient authority to make user changes.