Frage

I have a powershell script (thanks to this link).

My environment: SP 2013. Content type created on root, but want to use in only one subsite.

It looks like given below and set content type = true (if not) and add the content type. But this is for the whole site collection.

I want it to run in the subsite called "DocCentre" only and add the content type in that subsite's libraries as default content type. How can I accomplish that?

> #Get site object and specify name of the content type to look for in each library  $site = Get-SPSite http://<site>  $lookForCT = "Sales
> Document"
> 
> #Walk through each site in the site collection  $site | Get-SPWeb -Limit all | ForEach-Object {
> 
>      write-host "Checking site:"$_.Title
> 
>      #Go through each document library in the site
>      $_.Lists | where { $_.BaseTemplate -eq "DocumentLibrary" } | ForEach-Object { 
> 
>          write-host "Checking list:"$_.Title
>          $_.ContentTypesEnabled = $true
> 
>          #Check to see if the library contains the content type specified
>          #at the start of the script
>          if (($_.ContentTypes | where { $_.Name -eq $lookForCT }) -eq $null)
>          {
>              write-host "No content type exists with the name" $lookForCT "on list" $_.Title
>          }
>          else
>          {
>              #Add site content types to the list
>              $ctToAdd = $site.RootWeb.ContentTypes["HR Document"]
>              $ct = $_.ContentTypes.Add($ctToAdd)
>              write-host "Content type" $ct.Name "added to list" $_.Title
>              $ctToAdd = $site.RootWeb.ContentTypes["IT Document"]
>              $ct = $_.ContentTypes.Add($ctToAdd)
>              write-host "Content type" $ct.Name "added to list" $_.Title
>              $_.Update()
>          }
>      }  }  #Dispose of the site object  $site.Dispose()
War es hilfreich?

Lösung

If you want to add custom content type from root site to document library in sub site and set the content type as default, the following PowerShell script for your reference.

Add-PSSnapin microsoft.sharepoint.powershell
$site = Get-SPWeb http://sp2013/sites/team/DocCentre
$lookForCT = "Sales Document"
#Go through each document library in the sub site
$site.Lists | where { $_.BaseTemplate -eq "DocumentLibrary" } | ForEach-Object { 
    write-host "Checking list:" $_.Title
    $_.ContentTypesEnabled = $true
    $_.Update()
    #Check to see if the library contains the content type specified
    #at the start of the script
    if (($_.ContentTypes | where { $_.Name -eq $lookForCT }) -eq $null)
    {
        write-host "No content type exists with the name" $lookForCT "on list" $_.Title
        $ctToAdd = $site.Site.RootWeb.ContentTypes[$lookForCT]
        $ct = $_.ContentTypes.Add($ctToAdd)
        write-host "Content type" $ct.Name "added to list" $_.Title
        $_.Update()
        $result=New-Object System.Collections.Generic.List[Microsoft.SharePoint.SPContentType]
        $result.Add($ct)
        $_.RootFolder.UniqueContentTypeOrder = $result
        $_.RootFolder.Update() 
    }   
}

enter image description here

Andere Tipps

Try following code snippet:

Add-PSSnapin microsoft.sharepoint.powershell

$site = Get-SPWeb http://sitename
$lookForCT = "Sales Document"
$site | ForEach-Object {
   write-host "Checking site:"$_.Title
   #Go through each document library in the site
   $_.Lists | where { $_.BaseTemplate -eq "DocumentLibrary" } | ForEach-Object { 

   write-host "Checking list:"$_.Title
   $_.ContentTypesEnabled = $true

   #Check to see if the library contains the content type specified
   #at the start of the script
   if (($_.ContentTypes | where { $_.Name -eq $lookForCT }) -eq $null)
   {
     write-host "No content type exists with the name" $lookForCT "on list" $_.Title
   }
   else
   {
     #Add site content types to the list
     $ctToAdd = $site.RootWeb.ContentTypes["HR Document"]
     $ct = $_.ContentTypes.Add($ctToAdd)
     write-host "Content type" $ct.Name "added to list" $_.Title
     $ctToAdd = $site.RootWeb.ContentTypes["IT Document"]
     $ct = $_.ContentTypes.Add($ctToAdd)
     write-host "Content type" $ct.Name "added to list" $_.Title
     $_.Update()
   }
}  }  #Dispose of the site object  $site.Dispose()
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit sharepoint.stackexchange
scroll top