Saturday, August 6, 2011

Sharepoint 2010 Claims site - Add FBA users to Sharepoint group programmatically

Adding FBA users to Sharepoint group programmatically is simple. The hunch is the format "i:0#.f|fbamembershipprovider|" + username

In the format, 
i  - stands for Identity provider 
f  - stands for fba authentication
fbamembershipprovider  - is my providername

Here you go, a simple sample that servers the pupose.

SPSite sitename = new SPSite("SiteURL");
        SPWeb spWeb = sitename.OpenWeb();
        
        String strusername= "i:0#.f|fbamembershipprovider|" + UsernameTextbox.Text;
        spWeb.AllUsers.Add[strusername, EmailTextbox.Text, UsernameTextbox.Text, strusername];
       
         
            if (spUser != null)
            {
                SPGroup spGroup = spWeb.Groups["GROUP_NAME"];
            if (spGroup != null)
            spGroup.AddUser(spUser);
            }


Happy Coding :)

1 comment: