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

[玩转系统] SharePoint Online:使用 PowerShell 将内容类型设置为只读

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

SharePoint Online:使用 PowerShell 将内容类型设置为只读


要求:在 SharePoint Online 中将内容类型设置为只读。

如何在 SharePoint Online 中将内容类型设为只读?

在 SharePoint Online 中,您可以将内容类型设为只读以防止用户对其进行编辑。如果您想要限制谁可以修改特定内容类型或者希望确保数据一致,这会很有用。本文将向您展示如何在 SharePoint Online 中将内容类型设置为只读。

要将内容类型设置为只读,请执行以下操作:

  1. 转到设置>>站点设置>>单击“站点内容类型”链接
  2. 从列出的内容类型中,单击内容类型的名称
  3. 转到内容类型设置页面中的高级设置。
  4. 将此内容类型是否应为只读设置为“是”?”

    [玩转系统] SharePoint Online:使用 PowerShell 将内容类型设置为只读

SharePoint Online:PowerShell 将内容类型设置为只读

如果您需要通过脚本将内容类型设置为只读怎么办?让我向您展示如何使用 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"

#Config Parameters
$SiteURL="https://crescent.sharepoint.com"
$ContentTypeName="Business Contacts"

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 content type from the web
    $ContentTypeColl = $Ctx.Web.ContentTypes
    $Ctx.Load($ContentTypeColl)
    $Ctx.ExecuteQuery()
 
    #Get the content type to Add
    $CType = $ContentTypeColl | Where {$_.Name -eq $ContentTypeName}
    If($CType -ne $Null)
    {
        $CType.ReadOnly=$True
        $CType.Update($True)
        $Ctx.ExecuteQuery()

        Write-host "Content Type is Set to Read Only!" -ForegroundColor Green
    }
    else
    {
        Write-host "Content Type Doesn't Exist!" -ForegroundColor Yellow
    }
}
Catch {
    write-host -f Red "Error Setting Content Type to Read Only!" $_.Exception.Message
} 

PnP PowerShell 将内容类型设置为只读:

当您想要阻止用户对特定内容类型(例如重要文档或记录)进行更改时,在 SharePoint Online 中将内容类型设置为只读会非常有用。以下是如何使用 PnP PowerShell 将内容类型设置为只读。


#Config Variables
$SiteURL = "https://Crescent.sharepoint.com"
$ContentTypeName ="Crescent Invoice Template V2"

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

#Get the Client Context
$Context = Get-PnPContext
 
#Get the content type from site
$ContentType = Get-PnPContentType -Identity $ContentTypeName

If($ContentType)
{
    #Set the Content type to Read Only
    $ContentType.ReadOnly = $True
    $ContentType.Update($False) #Update children
    $Context.ExecuteQuery()

    Write-host -f Green "Content Type '$ContentTypeName' is Set to Read Only!"
}

我们还可以在列表级别将内容类型设置为只读:


#Config Variables
$SiteURL = "https://Crescent.sharepoint.com"
$ContentTypeName ="Crescent Invoice Template V2"
$ListName ="Team Documents"

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

#Get the Client Context
$Context = Get-PnPContext
 
#Get the content type from List
$ContentType = Get-PnPContentType -Identity $ContentTypeName -List $ListName

If($ContentType)
{
    #Set the Content type to Read Only
    $ContentType.ReadOnly = $True
    $ContentType.Update($False) #Update children
    $Context.ExecuteQuery()

    Write-host -f Green "Content Type '$ContentTypeName' is Set to Read Only!"
}

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

取消回复欢迎 发表评论:

关灯