Hi All
The code snippet shows you how to add users to SharePoint Programmatically. This code looks for a User who registers with FBA site and Pull them in to SharePoint Group.
[C# code snippet]
MembershipCreateStatus status;
If (Membership.GetUser("UserName") == null)
{
//Insert into MemberShip's Database
Membership.CreateUser("UserName", "Password", "Email", "Not used", "Not used", true, out status);
If (status.ToString().ToLower().Equals("success"))
{
MembershipUser User=Membership.GetUser ("UserName");
Using (SPSite site = new SPSite ("http://sharepoint site url"))
{
Using (SPWeb web = site.OpenWeb())
{
string _usernameWithProvider = String.Format("{0}:{1}", System.Web.Security.Membership.Provider.Name, User.UserName);
web.AllowUnsafeUpdates = true;
SPGroup grp = GetGroup ("Visitor", web); //Visitor is group available with you SharePoint site.
if (grp!= null)
{
web.SiteUsers.Add(_usernameWithProvider, User.Email, User.UserName, "");
web.Groups [group].AddUser(_usernameWithProvider, User.Email, User.UserName, "");
}
}
}
} }
private SPGroup GetGroup(string groupName, SPWeb web)
{
SPGroup spReturn = null;
try
{
spReturn = web.Groups[groupName];
}
catch (Exception ex)
{
return spReturn;
}
return spReturn; }
Hopefully This helps...
Coming Soon
7 years ago
No comments:
Post a Comment