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

[玩转系统] SharePoint Online:使用 PowerShell 从列表中删除内容类型

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

SharePoint Online:使用 PowerShell 从列表中删除内容类型


要求:从 SharePoint Online 列表中删除内容类型。

如何从 SharePoint Online 列表中删除内容类型?

内容类型是可重用的元数据集合,用于定义列表或库中项目的列和行为。在某些情况下,您可能需要从列表或库中删除内容类型。当您不再需要与 SharePoint Online 中的列表关联的内容类型时,可以将其删除。在本文中,我们将指导您完成使用 PowerShell 从 SharePoint Online 列表中删除内容类型的过程。

要从 SharePoint Online 列表中删除内容类型,请执行以下操作:

  1. 首先,登录到您的 SharePoint Online 站点 >> 导航到列表或库设置。
  2. 接下来,在“内容类型”下,单击您想要从列表中删除的相应内容类型名称。
  3. 最后,单击“列表内容类型”页面上的“删除此内容类型”链接,并确认一次提示以从列表中删除该内容类型。

    [玩转系统] SharePoint Online:使用 PowerShell 从列表中删除内容类型

请记住,这只会从列表中删除内容类型 - 它不会删除内容类型本身。如果您需要完全删除内容类型,则需要从网站设置页面执行此操作。

在 SharePoint Online 中使用 PowerShell 从列表中删除内容类型

可以删除列表或库中未使用的内容类型。若要从 SharePoint Online 列表中删除内容类型,请使用此 PowerShell 脚本。您需要指定站点 URL、内容类型名称和列表名称作为参数。


#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 Remove-ContentTypeFromList()
{ 
    param
    (
        [Parameter(Mandatory=$true)] [string] $SiteURL,
        [Parameter(Mandatory=$true)] [string] $ListName,
        [Parameter(Mandatory=$true)] [string] $ContentTypeName
    )

    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
    
        #Get the List
        $List = $Ctx.Web.Lists.GetByTitle($ListName)
        $Ctx.Load($List)

        #Get the content type from list
        $ContentTypeColl = $List.ContentTypes
        $Ctx.Load($ContentTypeColl)
        $Ctx.ExecuteQuery()

        #Get the content type to remove
        $CTypeToRemove = $ContentTypeColl | Where {$_.Name -eq $ContentTypeName}
        If($CTypeToRemove -ne $Null)
        {
            #Remove content type from list
            $CTypeToRemove.DeleteObject()
            $Ctx.ExecuteQuery()

            Write-host "Content Type '$ContentTypeName' Removed From '$ListName'" -f Green
        }
        else
        {
            Write-host "Content Type '$ContentTypeName' doesn't exist in '$ListName'" -f Yellow
            Return 
        }
   }
    Catch {
        write-host -f Red "Error Removing Content Type from List!" $_.Exception.Message
    }
}

#Set parameter values
$SiteURL="https://crescent.sharepoint.com"
$ListName="Projects"
$ContentTypeName="ProjectMilestone"

#Call the function
Remove-ContentTypeFromList -SiteURL $SiteURL -ListName $ListName -ContentTypeName $ContentTypeName

要确认内容类型已成功从列表中删除,请导航到 SharePoint Online 网站中的列表设置并检查内容类型列表。您删除的内容类型不应再列出。

使用 PnP PowerShell 从 SharePoint Online 列表中删除内容类型:

作为管理员,您可能需要从 SharePoint Online 列表中删除内容类型。让我向您展示如何连接到 SharePoint Online 列表或库并使用 PnP PowerShell 删除内容类型:


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

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

#Get the content type from list
$ListContentType = Get-PnPContentType -list $ListName -Identity $ContentTypeName
 
#Delete the content type from List
$ListContentType.DeleteObject() 
$Context.ExecuteQuery()

您还可以使用Remove-PnPContentTypeFromList cmdlet 从列表中删除内容类型:


#Set Variables
$SiteURL = "https://crescent.sharepoint.com/sites/Marketing"
$ListName = "Projects"
$ContentTypeName ="Crescent Project Proposal V2"

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

#Remove content type from List
Remove-PnPContentTypeFromList -List $ListName -ContentType $ContentTypeName

通过执行本文中概述的步骤,您可以根据需要轻松地从列表中删除内容类型。这使您可以维护干净且有组织的 SharePoint Online 环境,确保您的列表和库对用户保持相关且最新。

请注意,如果列表中包含根据您要删除的特定内容类型创建的任何项目,则您无法从列表中删除该内容类型!这可能会导致“内容类型仍在使用中”错误,无论是从 SharePoint UI 还是使用 PowerShell 从列表中删除内容类型。这是另一篇文章可以提供帮助:在 SharePoint Online 中获取“列表内容类型仍在使用中”?查找使用 PowerShell 的位置

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

取消回复欢迎 发表评论:

关灯