Monday, July 2, 2012

SharePoint 2010 FBA : Add FBA users to SharePoint group programmatically


SharePoint stores FBA users to below format

i:0#.f|sqlmembershipprovider|Surya (please replace with your membership provider name)

 here i is identity provider
      f is membership provider name 



you may get below error when the format is wrong

The user does not exist or is not unique.


   void AddUserstoGroup()
        {
            string siteurl = “yoursiteurl”;

          
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                using (SPSite site = new SPSite(siteurl))
                {
                    using (SPWeb web = site.OpenWeb())
                    {
                        web.AllowUnsafeUpdates = true;
                        //string userName = "i:0#.f|sqlmembershipprovider|kumar";
                        //userName = userName.Split('|')[2];

                        string userName = "i:0#.f|sqlmembershipprovider|" + txtEmail.Text;
                        web.SiteUsers.Add(userName, txtEmail.Text,txtLastName.Text + "," + txtFirstName.Text, "");

                        SPUser user = web.SiteUsers[userName];
                        if (user != null)
                        {
                            //TNI Visitors
                            web.Groups["Visitors"].AddUser(user);
                            web.Update();
                            web.AllowUnsafeUpdates = false;
                        }

                        web.Update();
                        web.AllowUnsafeUpdates = false;

                    }
                }
            });



        }

1 comment:

  1. Good Work..

    i was facing issue while adding user to the site.. in the username..

    By your help ..i am able to solve that issue..
    Thanks..

    ReplyDelete

Followers