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:
Possible Errors:
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);
[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:
Use SPSecurity.RunwithElevatedPrivileges and also while giving permission add the MembershipProvider with ":" separated User name
how to do this if i am running the above code from a console app.
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.
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.
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());
}
}
});
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.
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.
Sandeep K Nahta, thanks! It works if we add to our console project app.config file with connectionstring and membership and role providers. ;)
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?
Jigar, put file app.config file in same folder where your .csproj file is. In my case this file is:
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.]
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.]
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.
Post a Comment