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

[玩转系统] SharePoint Online:如何使用 PowerShell 将列表字段设为只读?

作者:精品下载站 日期:2024-12-14 14:58:29 浏览:15 分类:玩电脑

SharePoint Online:如何使用 PowerShell 将列表字段设为只读?


要求:将 SharePoint Online 列表表单中的字段设置为只读。

如何使用 PowerShell 将 SharePoint Online 中的列表字段设置为只读?

您是否希望将 SharePoint Online 列表字段设置为只读?也许您没有找到通过 UI 执行此操作的方法,但我想知道 PowerShell 是否可以提供帮助。在这篇博文中,我们将了解如何使用 PowerShell 将 SharePoint Online 列表字段设置为只读。


#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 to set a Field to Read Only in SharePoint Online List
Function Set-SPOFieldReadOnly($SiteURL, $ListName, $FieldInternalName, [Bool]$IsReadOnly)
{
    Try { 
        #Get Credentials to connect
        $Cred= Get-Credential
 
        #Setup the context
        $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
        $Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
 
        #Get the List
        $List=$Ctx.Web.Lists.GetByTitle($ListName)

        #Get the Field
        $Field = $List.Fields.GetByInternalNameOrTitle($FieldInternalName)
        $Field.ReadOnlyField = $IsReadOnly
        $Field.Update()
        $Ctx.ExecuteQuery()
 
        Write-host -f Green "Read Only Settings Update for the Field Successfully!"
    }
    Catch {
        write-host -f Red "Error:" $_.Exception.Message
    }
}
#Set parameter values
$SiteURL = "https://Crescent.sharepoint.com"
$ListName = "Team Documents"
$FieldInternalName = "Classification" #Internal Name
$IsReadOnly = $True

Set-SPOFieldReadOnly -SiteURL $SiteURL -ListName $ListName -FieldInternalName $FieldInternalName -IsReadOnly $IsReadOnly

这会将字段设置为只读,即使在快速编辑模式下也是如此!

[玩转系统] SharePoint Online:如何使用 PowerShell 将列表字段设为只读?

PnP PowerShell 将列表字段设置为只读

您还可以使用 PnP PowerShell 将 SharePoint Online 中的字段设为只读:


#Config Variables
$SiteURL = "https://Crescent.sharepoint.com"
$ListName = "Team Documents"
$ColumnName = "Classification" #Internal Name

#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive

#Get the Context
$Context = Get-PnPContext

#Get the Field from List
$Field = Get-PnPField -List $ListName -Identity $ColumnName

#Set the Field to Read Only
$Field.ReadOnlyField = $True
$Field.Update() 
$Context.ExecuteQuery() 

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

取消回复欢迎 发表评论:

关灯