当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] SharePoint Online:使用 PowerShell 授予列出项目的权限

作者:精品下载站 日期:2024-12-14 14:47:10 浏览:13 分类:玩电脑

SharePoint Online:使用 PowerShell 授予列出项目的权限


SharePoint 中的权限是分层的,从顶级网站集到列表项级别。在列表或库中创建项目时,它们会继承该列表或库的权限。但是,这种继承可以被打破,并且权限可以直接应用于项目。要设置列表项的唯一权限,您必须在项目级别配置权限。具体方法如下:

如何授予对 SharePoint Online 中单个列表项的访问权限?

我们的业务要求是在列表项级别授予权限。要对 SharePoint Online 列表项设置显式权限,我们必须首先中断权限继承(停止继承权限),然后将用户或组添加到列表项。

  1. 转到您的 SharePoint Online 列表或库 >> 选择您想要提供唯一权限的项目。
  2. 单击信息面板中的“管理访问”链接。在管理访问页面上,单击“高级”链接。

    [玩转系统] SharePoint Online:使用 PowerShell 授予列出项目的权限

  3. 在“权限”选项卡的“继承”组中,单击“停止继承权限”按钮。确认提示。

    [玩转系统] SharePoint Online:使用 PowerShell 授予列出项目的权限

  4. 现在,单击“授予权限”。功能区上的按钮。在“共享”对话框中,输入姓名和电子邮件地址。单击显示选项按钮。在“选择权限级别”列表框中,选择适当的权限级别,例如“编辑”。

    [玩转系统] SharePoint Online:使用 PowerShell 授予列出项目的权限

  5. 单击共享。

拥有太多的项目级别权限通常会导致性能问题!所以,要小心。

SharePoint Online:使用 PowerShell 设置列表项权限:

如何授予 SharePoint Online 项目级权限?这是我在 SharePoint Online 中授予权限的 PowerShell。


#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"

Function Set-ListItemPermission
{
    param
    (   
        [Parameter(Mandatory=$true)] [string]$SiteURL,
        [Parameter(Mandatory=$true)] [string]$ListName,
        [Parameter(Mandatory=$true)] [string]$ItemID,
        [Parameter(Mandatory=$true)] [string]$PermissionLevel,
        [Parameter(Mandatory=$true)] [string]$UserID
    )
    Try {
        #Setup Credentials to connect
        $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
        
        #Get the List and Item
        $List = $Ctx.Web.Lists.GetByTitle($ListName)
        $ListItem=$List.GetItemByID($ItemID)
        $Ctx.Load($List)
        $Ctx.Load($ListItem)
        $Ctx.ExecuteQuery()

        #Check if Item has unique permission already
        $list.Retrieve("HasUniqueRoleAssignments")
        $Ctx.ExecuteQuery()

        #Break Item's permission Inheritance, if its inheriting permissions from the parent
        if (-not $ListItem.HasUniqueRoleAssignments)
        {
            $ListItem.BreakRoleInheritance($false, $false) #keep the existing permissions: No -  Clear listitems permissions: No
            $ctx.ExecuteQuery()
        }

        #Get the User
        $User = $Ctx.Web.EnsureUser($UserID)
        $Ctx.load($User)
        $Ctx.ExecuteQuery()

        #Get the role 
        $Role = $Ctx.web.RoleDefinitions.GetByName($PermissionLevel)
        $RoleDB = New-Object Microsoft.SharePoint.Client.RoleDefinitionBindingCollection($Ctx)
        $RoleDB.Add($Role)
         
        #Assign permissions
        $UserPermissions = $ListItem.RoleAssignments.Add($User,$RoleDB)
        $ListItem.Update()
        $Ctx.ExecuteQuery()
    
        Write-host -f Green "Permission granted to List Item successfully!"
    }
    Catch {
        Write-host -f Red "Error granting permission to List Item!" $_.Exception.Message
    }
}

#Set parameter values
$SiteURL="https://crescent.sharepoint.com"
$ListName="Projects"
$ItemID="1"
$UserID="[email protected]"
$PermissionLevel="Edit"

#Call the function
Set-ListItemPermission -SiteURL $SiteURL -ListName $ListName -ItemID $ItemID -UserID $UserID -PermissionLevel $PermissionLevel 

此脚本为给定用户授予项目级别的权限。如果您想向 SharePoint 组提供权限,而不是行


$User = $Web.EnsureUser($UserAccount)
#use:
$Group =$Web.SiteGroups.GetByName($GroupName)
#and then
$GroupPermissions = $Item.RoleAssignments.Add($Group,$RoleDB)

PnP PowerShell 设置项目级别权限

若要使用 PowerShell 更改 SharePoint Online 中的列表项的权限,请使用:Set-PnPListItemPermission cmdlet。


#Config Variables
$SiteURL = "https://Crescent.sharepoint.com/sites/Marketing"
$ListName ="Projects"
$ListItemID ="1"
$GroupName="Marketing Members"
$UserID="[email protected]"

#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Credentials (Get-Credential)

#Get the Group
$Group = Get-PnPGroup | where-Object {$_.Title -eq $GroupName}

#Grant permission to Group - Remove all existing permissions
Set-PnPListItemPermission -Identity $ListItemID -List $ListName -AddRole "Read" -Group $Group -ClearExisting

#Grant permission to User
Set-PnPListItemPermission -Identity $ListItemID -List $ListName -AddRole "Edit" -User $UserID

您可以使用 Get-PnPRoleDefinition cmdlet 获取要添加或删除的所有可用权限。 我们可以向所有列表项授予权限,如 SharePoint Online 中所示:使用 PowerShell 对列表中的所有项目授予权限

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯