Tìm kiếm trang web

SharePoint Online: Đặt quyền thư mục bằng PowerShell


Yêu cầu: Thay đổi quyền đối với thư mục trong SharePoint Online bằng PowerShell.

Làm cách nào để đặt quyền cấp thư mục trong SharePoint Online?

Một trong những tác vụ quản trị SharePoint Online phổ biến nhất là quản lý quyền truy cập thư mục. Trong bài đăng trên blog này, chúng tôi sẽ chỉ cho bạn cách sử dụng PowerShell để đặt quyền thư mục cho trang SharePoint Online. Chúng tôi cũng sẽ đề cập đến cách điều chỉnh quyền cho các thư mục trong SharePoint Online thông qua giao diện trình duyệt web. Giữ nguyên!

Làm cách nào để cấp quyền duy nhất cho một thư mục trong SharePoint Online? Để quản lý các quyền của thư mục như Thêm hoặc Hạn chế trong SharePoint Online, hãy làm theo các bước sau:

  1. Dẫn hướng đến thư viện tài liệu SharePoint Online của bạn, nơi chứa thư mục đích.
  2. Nhấp vào “Quản lý quyền truy cập” từ ngăn chi tiết hoặc từ menu ngữ cảnh của thư mục.

  3. Trong ngăn Chi tiết, nhấp vào liên kết “Nâng cao”. Thao tác này sẽ đưa bạn đến trang “Quyền nâng cao”.

  4. Từ dải băng, hãy nhấp vào nút “Dừng quyền kế thừa” và xác nhận lời nhắc (Nếu thư mục đang kế thừa quyền từ thư mục mẹ của nó).
  5. Bây giờ, bạn sẽ nhận được danh sách người dùng và nhóm đã có quyền trên thư mục. Khi bạn ngắt quyền, SharePoint sẽ sao chép các quyền từ cấp độ mẹ của nó (Danh sách/thư viện trong trường hợp của chúng tôi!). Nhấp vào nút “Cấp quyền” từ ruy-băng để thêm người dùng vào thư mục.

  6. Nhập tên của người dùng và nhóm bạn muốn thêm quyền vào thư mục và chọn cấp độ quyền thích hợp bằng cách nhấp vào liên kết “Hiển thị tùy chọn” trên trang chia sẻ. Nhấp vào nút “Chia sẻ” để thêm quyền vào thư mục.

Tương tự, để hạn chế quyền truy cập vào thư mục trong SharePoint Online, hãy chọn tất cả người dùng trong trang quyền của thư mục và nhấp vào “Xóa quyền của người dùng”.

Được rồi! Hãy sử dụng PowerShell để thêm người dùng vào thư mục SharePoint Online.

PowerShell để thay đổi quyền cấp thư mục SharePoint Online:

Bạn có thể hạn chế quyền truy cập vào các thư mục trong SharePoint Online bằng PowerShell không? Tất nhiên là có! Hãy thêm quyền vào thư mục SharePoint Online bằng PowerShell. Tập lệnh PowerShell này phá vỡ quyền của thư mục và cấp quyền bằng mô hình đối tượng phía máy khách (CSOM).


#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
 
#Variables
$SiteURL="https://crescent.sharepoint.com" #Or https://crescent.sharepoint.com/sites/Marketing
$FolderURL="/Project Documents/Active" #Or /sites/Marketing/Project Documents/Active - Server Relative URL of the Folder!
$GroupName="Team Site Members"
$UserAccount="Salaudeen@crescent.com"
$PermissionLevel="Edit"

Try {
    $Cred= Get-Credential
    $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)

    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = $Credentials
    $Web = $Ctx.web

    #Get the Folder
    $Folder = $Web.GetFolderByServerRelativeUrl($FolderURL)
    $Ctx.Load($Folder)
    $Ctx.ExecuteQuery()
    
    #Break Permission inheritence of the folder - Keep all existing folder permissions & keep Item level permissions
    $Folder.ListItemAllFields.BreakRoleInheritance($False,$True)
    $Ctx.ExecuteQuery()
    Write-host -f Yellow "Folder's Permission inheritance broken..."
     
    #Get the SharePoint Group & User
    $Group =$Web.SiteGroups.GetByName($GroupName)
    $User = $Web.EnsureUser($UserAccount)
    $Ctx.load($Group)
    $Ctx.load($User)
    $Ctx.ExecuteQuery()

    #sharepoint online powershell set permissions on folder
    #Get the role required
    $Role = $web.RoleDefinitions.GetByName($PermissionLevel)
    $RoleDB = New-Object Microsoft.SharePoint.Client.RoleDefinitionBindingCollection($Ctx)
    $RoleDB.Add($Role)
         
    #add sharepoint online group to folder using powershell
    $GroupPermissions = $Folder.ListItemAllFields.RoleAssignments.Add($Group,$RoleDB)

    #powershell add user to sharepoint online folder
    $UserPermissions = $Folder.ListItemAllFields.RoleAssignments.Add($User,$RoleDB)
    $Folder.Update()
    $Ctx.ExecuteQuery()
    
    Write-host "Permission Granted Successfully!" -ForegroundColor Green  
}
Catch {
    write-host -f Red "Error Granting permission to  Folder!" $_.Exception.Message
}

PowerShell này phá vỡ quyền kế thừa của thư mục và cấp quyền truy cập cho người dùng và nhóm. Đây là kết quả của sự thay đổi quyền đối với thư mục:

  • Để xóa người dùng hoặc nhóm khỏi thư mục trong SharePoint Online, hãy sử dụng tập lệnh PowerShell này: Làm cách nào để xóa người dùng hoặc nhóm khỏi quyền thư mục trong SharePoint Online?
  • Nếu bạn muốn đặt quyền trên tất cả các thư mục trong thư viện tài liệu SharePoint Online, hãy tham khảo: SharePoint Online: Cấp quyền cho từng thư mục trong Thư viện tài liệu bằng PowerShell

PnP PowerShell để thay đổi quyền thư mục trong SharePoint Online

Để cấp quyền cho một thư mục trong SharePoint Online, hãy sử dụng PnP PowerShell này:


#Config Variables
$SiteURL = "https://crescent.sharepoint.com/sites/marketing"
$ListName="Documents"
$FolderServerRelativeURL = "/sites/marketing/Shared Documents/2019"
$UserAccount = "Salaudeen@crescent.com"
 
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Credentials (Get-Credential)

#Get the Folder from URL
$Folder = Get-PnPFolder -Url $FolderServerRelativeURL
    
#Grant Permission to a Folder
Set-PnPListItemPermission -List $ListName -Identity $Folder.ListItemAllFields -User $UserAccount -AddRole 'Contribute'

Làm cách nào để hạn chế quyền truy cập vào một thư mục trong SharePoint Online?

Bạn cũng có thể hạn chế quyền truy cập thư mục SharePoint Online bằng lệnh ghép ngắn Set-PnPFolderPermission. Đây là một ví dụ:


#Config Variables
$SiteURL = "https://crescent.sharepoint.com/sites/Marketing"
$ListName ="Branding"
$FolderServerRelativeURL = "/Sites/Marketing/Branding/2020"
 
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive

#PowerShell to add user to sharepoint online folder
Set-PnPfolderPermission -List $ListName -identity $FolderServerRelativeURL -User "salaudeen@crescent.com" -AddRole "Edit"

#To remove user, use: Set-PnPfolderPermission -List $ListName -identity $FolderSiteRelativePath -User "salaudeen@crescent.onmicrosoft.com" -RemoveRole "Edit"

Làm cách nào để thay đổi quyền của thư mục trong SharePoint Online bằng PnP PowerShell?

Tương tự, để cấp quyền cho nhóm SharePoint Online, hãy sử dụng:


#Config Variables
$SiteURL = "https://crescent.sharepoint.com/sites/Marketing"
$ListName ="Branding"
$FolderServerRelativeURL = "/Sites/Marketing/Branding/Active"
  
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive
 
#Grant folder permissions to SharePoint Group
Set-PnPfolderPermission -List $ListName -identity $FolderServerRelativeURL -AddRole "Edit" -Group "Marketing Members"

Tập lệnh này phá vỡ tính kế thừa quyền của thư mục đã cho và thêm hoặc xóa quyền đối với thư mục đối với người dùng hoặc nhóm nhất định. Tương tự, bạn có thể đặt quyền cho Nhóm AD bằng cách sử dụng tên của nó làm giá trị tham số cho “người dùng”. Đây là một bài đăng khác trên SharePoint Online: PowerShell để nhận quyền thư mục

Các câu hỏi thường gặp:

Làm cách nào để hạn chế quyền truy cập vào thư viện tài liệu trong SharePoint Online?

Duyệt đến thư viện tài liệu >> Nhấp vào Cài đặt >> “Cài đặt thư viện” >> Nhấp vào “Quyền đối với thư viện tài liệu này” >> Nhấp vào “Dừng quyền kế thừa”. Giờ đây, bạn có thể thêm hoặc loại bỏ người dùng và nhóm khỏi thư viện tài liệu để hạn chế quyền.
Thông tin thêm: Đặt quyền cho Thư viện tài liệu trong SharePoint Online

Làm cách nào để mọi người có thể truy cập thư mục SharePoint Online?

Chỉ cần cấp quyền truy cập cho nhóm “Mọi người” trên thư mục! Làm cách nào để cấp quyền truy cập cho mọi người trong SharePoint Online?

Bài viết liên quan