A Practical Guide to SharePoint 2013

A Practical Guide to SharePoint 2013
A Practical Guide to SharePoint 2013 - Book by Saifullah Shafiq

Friday, April 1, 2005

SPS 2003: CheckRights(sitename, username) function checks user permissions in a site

Following is the code to check the rights of the users. Following code will show only those sites for which user has at least "Reader" rights.

Function CheckRights(ByVal sitePath As String, Byval strUser as string) As Boolean


//sitePath contains the site that you retrieved in the code shown above


// strUser contains the user for whom you want to check the rights and retrieve sites


Try
Dim blnStatus As Boolean = False
If Not sitePath Is Nothing Or Not sitePath = "" Then


Dim rtn As String() = parseURL(sitePath)


Dim siteCollection As SPSite
siteCollection = New SPSite(sitePath)
Dim site As SPWeb = siteCollection.OpenWeb(rtn(0))


Dim allUsers As SPUserCollection = site.Users
Dim user As SPUser


For Each user In allUsers
If user.LoginName.ToUpper = strUser Then


Dim allGroups As SPRoleCollection = user.Roles
Dim group As SPRole


For Each group In allGroups


Dim right As Integer
right = group.PermissionMask And SPRights.ViewListItems


If right = SPRights.ViewListItems Then


blnStatus = True
Return blnStatus
Exit Function


End If
Next


End If
Next


Return blnStatus


End If


Catch ex As Exception
//Exception handling code here!
End Try


End Function


The function shown above (CheckRights) uses another function called as parseURL(). Following is the code for the parseURL() function:


Public Function parseURL(ByVal _fullPath As String) As String()


_fullPath = System.Web.HttpUtility.UrlDecode(_fullPath)


Dim site As SPSite = New SPSite(_fullPath)


Dim web As SPWeb = site.RootWeb


Dim webPath As String = _fullPath.Replace(web.Url & "/", "")


Dim rtnObject As String() = {"", "", ""}


Dim w As SPWeb


Dim folderPath As String = ""


Dim docLibPath As String = ""


Dim i, j As Integer


While CType(w, Object) Is Nothing


Try


w = site.OpenWeb(webPath)


Dim s As String = w.ID.ToString


'//above will raise the exception too


Catch ex As Exception


w = Nothing


i = webPath.LastIndexOf("/")


If i = -1 Then


Exit While '//means the end of subsite lookup


End If


j = webPath.Length


webPath = webPath.Remove(i, j – i)


End Try


End While


'//if there was some sub-site object then


If Not CType(w, Object) Is Nothing Then


folderPath = _fullPath.Substring(_fullPath.IndexOf(webPath) + webPath.Length + 1)


If folderPath.IndexOf("/") > 0 Then


docLibPath = folderPath.Substring(0, folderPath.IndexOf("/"))


folderPath = folderPath.Remove(0, folderPath.IndexOf("/") + 1)


End If


If docLibPath = webPath Then webPath = "" '_fullPath


rtnObject(0) = webPath


rtnObject(1) = docLibPath


rtnObject(2) = folderPath


Else '//there was no sub-site obect was found


folderPath = _fullPath.Substring(_fullPath.IndexOf(webPath))


If folderPath.IndexOf("/") > 0 Then


docLibPath = folderPath.Substring(0, folderPath.IndexOf("/"))


folderPath = folderPath.Remove(0, folderPath.IndexOf("/") + 1)


End If


If docLibPath = webPath Then webPath = "" '_fullPath


rtnObject(0) = webPath


rtnObject(1) = docLibPath


rtnObject(2) = folderPath


End If


Return rtnObject


End Function

—————-

This code will look complex but it is easy to use. Just supply the arguments and get the results. I could have created a web part as well but i have published the code so that max. number of users can take advantage. Of course, you can create a web part as well.

Regards,

-SSA