Pages

Senin, 07 Mei 2012

Add User to Group Sharepoint programmatically (VB)

Public Function setPermissionSP() As Boolean
Using site As SPSite = New SPSite("http://localhost/blablabla")

Using oWebsite As SPWeb = site.OpenWeb()

Try

sUserId = "DOMAIN\USERID"
Try
oWebsite.AllowUnsafeUpdates = True
Dim usr As SPUser = oWebsite.EnsureUser(sUserId)
Dim grp As SPGroup = oWebsite.Groups("Group Name")

If usr Is Nothing Then
Return False
Else
If grp Is Nothing Then
Return False
Else
grp.AddUser(usr)
End If
End If

Catch ex As Exception
Throw New Exception(ex.Message)
Finally
oWebsite.AllowUnsafeUpdates = False

End Try

Return True

Catch ex As Exception

Throw New Exception(ex.Message)
End Try

End Using
End Using

End Function