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

[玩转系统] SharePoint Online:使用 PowerShell 从网站集中的所有列表中删除内容类型

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

SharePoint Online:使用 PowerShell 从网站集中的所有列表中删除内容类型


要求:从 SharePoint Online 网站集中的所有列表和库中删除内容类型。

PowerShell 从网站集中的所有列表和库中删除内容类型

在 SharePoint Online 中,有时您可能想要从网站集中的所有列表中删除内容类型。例如,也许您创建了一种内容类型,但后来意识到它不是最佳选择,或者您希望阻止用户将该类型的项目添加到网站上的列表中。这可以通过 SharePoint 用户界面手动完成,但这可能非常耗时。在这篇文章中,我将向您展示如何使用 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"
 
#Function to remove content type from a site collection
Function Remove-SPOContentType([String]$SiteURL, [String]$ContentTypeName)
{
    Try{
        Write-host -f Yellow "Processing Site:" $SiteURL
 
        #Setup the context
        $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
        $Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
     
        #Get All Lists of the Web
        $Ctx.Load($Ctx.Web)
        $Ctx.Load($Ctx.Web.Lists)
        $Ctx.Load($ctx.Web.Webs)
        $Ctx.ExecuteQuery()
 
        #Get content types of each list from the web
        ForEach($List in $Ctx.Web.Lists)
        {
            $ContentTypes = $List.ContentTypes
            $Ctx.Load($ContentTypes)
            $Ctx.ExecuteQuery()             

            #Get each content type data
            ForEach($CType in $ContentTypes)
            {
                If($CType.Name -eq $ContentTypeName)
                {
                    Try{
                        #Remove Content Type from List
                        $CType.DeleteObject()
                        $Ctx.ExecuteQuery()
                        Write-host -f Green "Removed Content Type from List/Library:" $List.Title
                        }
                    Catch {
                    write-host -f Red "`tError Removing Content Type from '$($List.Title)' :" $_.Exception.Message
                    }
                }
            }
        }
 
         #Iterate through each subsite of the current web and call the function recursively
        foreach ($Subweb in $Ctx.web.Webs)
        {
            #Call the function recursively to process all subsites underneaththe current web
            Remove-SPOContentType $Subweb.url $ContentTypeName
        }
    }
    Catch {
    write-host -f Red "Error Removing Content Type:" $_.Exception.Message
    }
}
 
#Config Parameters
$SiteURL="https://crescent.sharepoint.com/sites/marketing"
$ContentTypeName="Project Proposal"
 
#Get Credentials to connect
$Cred= Get-Credential
 
#Call the function to get the content type usage
Remove-SPOContentType -SiteURL $SiteURL -ContentTypeName $ContentTypeName

此方法比通过用户界面从列表中手动删除内容类型更快、更高效。通过使用 PowerShell,您可以自动化该过程并确保在网站集中的所有列表中一致地删除内容类型。

相关文章:

  • SharePoint Online:使用 PowerShell 从列表中删除内容类型
  • SharePoint Online:使用 PowerShell 查找网站集中内容类型的使用情况

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

取消回复欢迎 发表评论:

关灯