0

this code add my content type to all sites but i want to add my content type to a specifik site for example under http://portal i have a site created named site1 how could i rewrite my code to add only content type to http://portal/site1

#Get site object and specify name of the library to look for in each site
$site = Get-SPSite http://portal
$lookForList = "Shared Documents"

#Walk through each site and change content types on the list specified
$site | Get-SPWeb -Limit all | ForEach-Object {

    write-host "Checking site:"$_.Title

    #Make sure content types are allowed on the list specified
    $docLibrary = $_.Lists[$lookForList]

    if ($docLibrary -ne $null)
    {
        $docLibrary.ContentTypesEnabled = $true
        $docLibrary.Update()

        #Add site content types to the list
        $ctToAdd = $site.RootWeb.ContentTypes["Sales Document"]
        $ct = $docLibrary.ContentTypes.Add($ctToAdd)
        write-host "Content type" $ct.Name "added to list" $docLibrary.Title
        $docLibrary.Update()
    }
    else
    {
        write-host "The list" $lookForList "does not exist in site" $_.Title
    }
}
#Dispose of the site object
$site.Dispose()

1 Answer 1

1

The code is getting the content type from the root site collection and adding it to a List named "Shared Documents", in root site. Moreover, it is looking for the same named list in all sub sites. If it finds one, it is adding the content type to the list. If your requirement is to add the content type to a particular list in a single sub site, you can modify the code like this:

#Get site object and specify name of the library to look for in each site
$web = Get-SPWeb http://portal/site1
$lookForList = "Shared Documents"

#Make sure content types are allowed on the list specified
$docLibrary = $web.Lists[$lookForList]

if ($docLibrary -ne $null)
{
    $docLibrary.ContentTypesEnabled = $true
    $docLibrary.Update()

    #Add site content types to the list
    $ctToAdd = $web.Site.RootWeb.ContentTypes["Sales Document"]
    $ct = $docLibrary.ContentTypes.Add($ctToAdd)
    write-host "Content type" $ct.Name "added to list" $docLibrary.Title
    $docLibrary.Update()
}
else
{
    write-host "The list" $lookForList "does not exist in site" $_.Title
}
1
  • How could i check if content type already exists in subsite write-host "the content type already exists on subsite"
    – Batistuta
    Commented Apr 13, 2015 at 6:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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