简体   繁体   English

从列表Office 365中删除ContentType

[英]Remove ContentType from list Office 365

I want to remove a Content-Type from a list in office 365 using powershell. 我想使用PowerShell从office 365中的列表中删除Content-Type。 I found this code but the $Doclibrary is always null. 我找到了这段代码,但$ Doclibrary始终为null。

$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
    }
}

solution: 解:

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"
        }
}

Please, Can you help me? 拜托,你能帮帮我吗? I'm crazy! 我疯了!

Thanks in advance. 提前致谢。

Here you go the solution, There are two methods one to remove by content type ID and other by content type name. 在这里你得到解决方案,有两种方法可以按内容类型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()

} 

Cheers, Kiran 干杯,基兰

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM