1. 程式人生 > >Sharepoint列表項許可權修改

Sharepoint列表項許可權修改

場景:刪除列表項所有當前許可權,替換為要求的許可權(此處為 建立者可以修改,總監、組長可以檢視)

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim oweb As SPWeb = New SPSite("http://localhost:881/").OpenWeb("/wdb")
        Dim olist As SPList = oweb.Lists("話務中心客戶電話記錄表")
        Dim slist As SPList = oweb.Lists("客服列表")
        Dim GLname As String
        Dim oitm As SPListItem
        For Each oitm In olist.Items
            Dim sitm As SPListItem
            For Each sitm In slist.Items
                If InStr(oitm("客服"), sitm("姓名")) Then      '取組長名
                    GLname = sitm("GL")
                    Exit For
                End If
            Next
            If GLname = "" Then
                T1.AppendText(oitm.ID & "," & oitm("客服") & "找不到組長" & vbCrLf)
                Exit Sub
            End If

            Dim spp As SPRoleAssignment
            oitm.BreakRoleInheritance(True)                     '中斷繼承許可權
            If oitm.RoleAssignments.Count > 0 Then              '清空當前許可權
                For i = oitm.RoleAssignments.Count - 1 To 0 Step -1
                    oitm.RoleAssignments.Remove(i)
                Next
            End If
            For Each usr As SPUser In oweb.SiteUsers            '查詢所有使用者名稱
                If InStr(oitm("客服"), usr.Name) Then           '如果是當前建立者,加編輯許可權
                    T1.AppendText(usr.LoginName)
                    spp = New SPRoleAssignment(usr)
                    spp.RoleDefinitionBindings.Add(oweb.RoleDefinitions("編輯_CantDel"))
                    oitm.RoleAssignments.Add(spp)
                    oitm.Update()
                    T1.AppendText(oitm.ID)
                    spp = Nothing
                ElseIf usr.Name = "總監" Or InStr(GLname, usr.Name) Then                '如果是總監或者組長,加檢視許可權
                    T1.AppendText(usr.LoginName)
                    spp = New SPRoleAssignment(usr)
                    spp.RoleDefinitionBindings.Add(oweb.RoleDefinitions("讀取"))
                    oitm.RoleAssignments.Add(spp)
                    oitm.Update()
                    T1.AppendText(oitm.ID)
                    spp = Nothing
                End If
            Next
        Next
    End Sub