I'm using FBA authentication and I want to add external user to a sharepoint groups of 2 different Sharepoint site at the same time
-
Thursday, May 17, 2012 8:12 AM
My actual code is shown below :
MembershipUser user = Membership.GetUser(userID);
if (user != null)
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
string siteURL = SPContext.Current.Site.Url;
using (SPSite site = new SPSite(siteURL))
{
using (SPWeb myWeb = site.OpenWeb())
{
myWeb.AllowUnsafeUpdates = true;
SPUser spUser = myWeb.EnsureUser(user.UserName);
SPGroup spGroup = myWeb.SiteGroups[ddlRoles.SelectedValue.ToString()]; //Adding users into SP Groups
int noofcounts = spGroup.Users.Count;
spGroup.AddUser(spUser);
}
}
});
SPSecurity.RunWithElevatedPrivileges(delegate()
{
string siteURL = "http://...";
using (SPSite site = new SPSite(siteURL))
{
using (SPWeb myWeb = site.OpenWeb())
{
myWeb.AllowUnsafeUpdates = true;
// string name = "i:0#.f|aspnetmembership1|" + user.UserName.ToString();
SPUser spUser = myWeb.EnsureUser(user.UserName);
SPGroup spGroup = myWeb.SiteGroups[ddlRoles.SelectedValue.ToString()]; //Adding users into another site collection SP Groups
int noofcounts = spGroup.Users.Count;
spGroup.AddUser(spUser);
}
}
});I'm getting a error on the highlighted line. The error is: "SPException was unhandled by user code : The specified user could not be found"
NOTE:
1. The external users are added to the groups of 1st sharepoint site alone and while checking in the group of 2nd sharepoint site " The users are added to the form authentication of 2nd sharepoint site but the users are not visible in their particular groups "
2. I have configured the Membership user for the both site collections.
3. I have tried hardcoding also.
But still face the error "SPException was unhandled by user code : The specified user could not be found".
- Edited by -vijay Thursday, May 17, 2012 9:46 AM
All Replies
-
Friday, May 18, 2012 5:34 AM
Hi Friends,
Can anybody help to fix the issue ASAP..
Thanks & Regards,
Vijay
-
Friday, May 18, 2012 9:03 AMModerator
Hi –vijay,
Instead of using SPWeb.EnsureUser() method, would you please try to use SPWeb.SiteUsers.AddUser() to add the user to the expected web to check the result, or try to use:
string provider = spSite.WebApplication.IisSettings[SPUrlZone.Internet].MembershipProvider;
SPUser spUser = spWeb.EnsureUser(provider + ":" + iUser);The exception may occur when you use this method with a custom membership provider, here is an article about this, please refer to it for more information:
http://blog.mastykarz.nl/inconvenient-programmatically-sharepoint-users-spweb-ensureuser/More information, a similar thread:
http://social.technet.microsoft.com/Forums/en-US/sharepointdevelopment/thread/8030d35f-fe49-4dd4-8416-a281ed4c0228Thanks,
QiaoQiao Wei
TechNet Community Support
-
Monday, May 21, 2012 8:10 AM
Hi Qiao Wei
I have tried your code, but am getting an error like "[SPUrlZone.Internet] is not declared in my site "
-
Monday, May 21, 2012 10:28 AMModerator
Hi –vijay,
SPUrlZone in SPWebApplication.IisSettings property is an enum that specifies the originating zone of a request received by Windows SharePoint Services, more information, please refer to:
http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.administration.spurlzonePlease change it to the expected value based on your environment to check the result.
Thanks,
QiaoQiao Wei
TechNet Community Support
- Marked As Answer by -vijay Wednesday, May 23, 2012 10:20 AM
-
Monday, May 21, 2012 11:45 AM
I'm going to suggest an easier way to allow an external user access to multiple SharePoint sites at once:
Create roles in your membership database.
Add the roles to the SharePoint groups on all of the different sites you want the user to have access to.
Finally, just add the user to the role in the membership database, and they will be able to access all of the SharePoint sites you've allowed the roles to access.

