Wednesday, February 10, 2010

adding users to sharepoint group programmatically

When I was working on form authunticated portal site in sharepoint 2007, I need
to add users to sharepoint group programmatically, all what we need is the following:

Dim membershipUser As MembershipUser
Dim membershipStatus As MembershipCreateStatus
membershipUser = Membership.CreateUser( pUsername, pPassword, pEmail, pSecurityQuestion,
pSecurityAnswer, True,membershipStatus)

' after creating user, call  _addUserToGroup() method using SPSecurity.RunWithElevatedPrivileges(AddressOf  )

SPSecurity.RunWithElevatedPrivileges(AddressOf _addUserToGroup)

Private Sub _addUserToGroup()
        Dim site As New SPSite("http://localhost:1200")
        Dim web As SPWeb = site.OpenWeb()
        Dim spUser As SPUser
        web.AllowUnsafeUpdates = True
       ' call EnsureUser method and pass user name with Membership Provider Name  like the following row
        spUser = web.EnsureUser("AspNetSqlMembershipProvider:" + txtUsername.Text)
End Sub
Thats all
Mohammad AlShafe'i

No comments:

Post a Comment