powershell - 从列表中删除 ContentType Office 365

标签 powershell sharepoint powershell-2.0 office365

我想使用 powershell 从 office 365 的列表中删除内容类型。我找到了这段代码,但 $Doclibrary 始终为空。

$lookForList = "Document"
$stringCTRemove = "Your Content type"
        $docLibrary = $_.Lists[$lookForList]


    if($docLibrary -ne $null)
    {
        $ctToRemove = $docLibrary.ContentTypes[$stringCTRemove]
        write-host "Removing content type" $ctToRemove.Name "from list" $docLibrary.Title
        $docLibrary.ContentTypes.Delete($ctToRemove.Id)
        $docLibrary.Update()
    }
    else
    {
        write-host "The list" $lookForList "does not exist in site" $_.Title
    }
}

解决方法:

function removeBannersCT ($web)
{
    $listName = "Banners"
    $list = $context.Web.Lists.GetByTitle($listName)
    $context.Load($list)
    $context.ExecuteQuery()
    $stringCTRemove = "Picture"

        if($list -ne $null)
        {
            $lcontentTypes = $list.ContentTypes
            $context.Load($lcontentTypes)
            $context.ExecuteQuery()
            foreach($cts in $lcontentTypes)
            {
                if($cts.Name -eq $stringCTRemove)
                {
                    $list.ContentTypes.GetById($cts.Id).DeleteObject()
                    $list.Update()
                    write-host "Content-Type removed"
                }
            }
        }
        else
        {
            write-host "The list" $listName "does not exist in site"
        }
}

拜托,你能帮帮我吗?我疯了!

提前致谢。

最佳答案

给你解决方案, 有两种方法,一种是通过内容类型 ID 删除,另一种是通过内容类型名称删除。

function Set-SPORemoveContentTypeByName {
[cmdletbinding()]
    param (
        [Microsoft.SharePoint.Client.ClientContext]$ctx, 
        [string]$spList,
        [string]$ctName
    )

    $web = $ctx.Web
    $list = $ctx.Web.Lists.GetByTitle($spList)
    $cntTypeColl =$list.ContentTypes
    $ctx.Load($web) 
    $ctx.Load($list)
    $ctx.Load($cntTypeColl)
    $ctx.ExecuteQuery()



    foreach($ct in $cntTypeColl)
    {

    if($ct.Name -eq $ctName)
        {
        $ct.DeleteObject()
        break;
        }
    }

    $ctx.ExecuteQuery()

} 

function Set-SPORemoveContentTypeById {
[cmdletbinding()]
    param (
        [Microsoft.SharePoint.Client.ClientContext]$ctx, 
        [string]$spList,
        [string]$ctId
    )

    $web = $ctx.Web
    $list = $ctx.Web.Lists.GetByTitle($spList)
    $cntTypeColl =$list.ContentTypes
    $ctx.Load($web) 
    $ctx.Load($list)
    $ctx.Load($cntTypeColl)
    $ctx.ExecuteQuery()

  write-host "Required Content Type Id :" $ctId

    foreach($ct in $cntTypeColl)
    {
    write-host "Intertaed ID " $ct.Id


    if($ct.Id.ToString() -eq $ctId)
        {
        $ct.DeleteObject()
        break;
        }
    }

    $ctx.ExecuteQuery()

} 

干杯, 基兰

关于powershell - 从列表中删除 ContentType Office 365,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31270304/

相关文章:

windows - 任务运行时等待?/任务完成后跳转下一行?

web-services - 尝试比较 Web 服务响应和文件中的预期 xml

sharepoint - SharePoint 中的空白弹出窗口

arrays - 从 powershell 中的管道属性获取值(value)

powershell,写入主机文件的所有文件名

sharepoint - 工作流程当前项 bool 值不起作用

powershell - 在多个 powershell 函数之间重用参数

shell - 艰难地学习 Python 练习 46 : mkdir bin NAME tests docs

node.js - 语法错误 : Invalid or Unexpected Token when running a nodejs file using Visual Studio Code (Using powershell)

c# - 我可以连接此代码来部署解决方案(在 Sharepoint 2010 中创建用户组)