Sunday, January 3, 2010

How to find out user permission in Sharepoint

Sometimes when we upload aspx pages with the custom application written for ourselves, we wish we can give certain controls and access rights of that aspx page based on the sharepoint sites.. The sample code below will help to bridge the gap between sharepoint permission and user uploaded aspx pages to sharepoint. We can find out the user's permission of the sharepoint within the aspx page





<script runat="server">
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

Dim username As String = Me.Page.User.Identity.Name
Dim url As String = SPContext.Current.Web.Url
usernameBox.Text = username
Dim site As New SPSite(url)

For Each web As SPWeb In site.AllWebs
If web.Url = url Then
For Each user As SPUser In web.AllUsers
If username = user.LoginName Then
Dim output As String = ""
Dim groupcount As Integer = user.Groups.Count

For Each group As SPGroup In user.Groups
output += group.Name & vbCr & vbLf
Next
Results.Text = ("This user belong to " & groupcount & " groups:" & vbCr & vbLf & vbCr & vbLf) + output
End If
Next
End If
Next
site.Dispose()
End Sub
</script>



<style type="text/css">
#form1
{
height: 300px;
}
</style>


<form id="form1" runat="server">
<?xml:namespace prefix = asp /><asp:label id="Label1" runat="server" text="Your Login Name is:"></asp:label>




<asp:textbox id="usernameBox" runat="server" width="362px"></asp:textbox>




<asp:label id="Label2" runat="server" text="You belong to the following groups:"></asp:label>




<asp:textbox id="Results" runat="server" width="366px" height="202px" textmode="MultiLine"></asp:textbox>
</form>