2015-07-07 2 views
3

Я хочу удалить Content-Type из списка в офисе 365 с помощью powershell. Я нашел этот код, но $ Doclibrary всегда имеет значение null.Удалить ContentType из списка Office 365

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

Пожалуйста, Вы можете мне помочь? Я сумасшедший!

Заранее спасибо.

+0

решил, что я должен добавить контекст нагрузки. –

ответ

0

Здесь вы принимаете решение, Существует два метода удаления идентификатора типа контента и другого по типу содержимого.

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() 

} 

Приветствия, Киран

Смежные вопросы