0

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

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

0

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()
  • Perhaps I should have modified the script a bit. I only want to add one content type: 'DCDocument' to the libraries in only one subsite. So I want to check only sitenam/DocCentre (the subsite) for the content type and if not there, I want to add it and set as default. – Heste Nov 7 '18 at 15:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.