Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I'm updating predefined content types with 2 new field, but I have to set the ContentType.Update(false,false). Which means that it will not propagate the changes to the list content type (instances of this site content type). For that I need a powershell script that will find the lists that are using the predefined content types and add the new field there, to the list content type.Can you guys please provide any guidance on how to achieve this using powershell. I just need the script to propagate the changes.

Thanks

share|improve this question
add comment

3 Answers

up vote 1 down vote accepted

Here is my script for the above question, hope it will help other people who come across this same problem

function ProcessContentType($rootWeb, $ct, $fldLinks)
{
Write-Host "Finding this "  $ct.Name  " usage in this root web "  $rootWeb.Title -ForegroundColor Magenta

$usages = [Microsoft.SharePoint.SPContentTypeUsage]::GetUsages($ct)

if($usages.Count -gt 0)
{
    Write-Host "Total number of this content type usage: "  $ct.Name  " found in this root web "  $rootWeb.Title  " is "  $usages.Count -ForegroundColor Magenta

    foreach($usage in $usages)
    {
        if($usage.IsUrlToList -eq $true)
        {
            $subWebUrl = ""

            if($usage.Url.ToLower().Contains("/pages"))
            {
                $subWebUrl = $usage.Url.ToLower().Replace("/pages", "")
                $subWebUrl = $subWebUrl.Replace("/en", "")
                $subWebUrl = $rootWeb.Url + $subWebUrl
            }

            $err = $null

            $gc = Start-SPAssignment

            $subWeb = $gc | Get-SPWeb $subWebUrl -erroraction SilentlyContinue -errorvariable err

            if($err -ne $null)
            {
                Write-Host  "The site " $usage.Url " could not be found " -ForegroundColor Red
            }

            if($subWeb -ne $null)
            {
                Write-Host "Site found "  $subWeb.Url -ForegroundColor Green

                $list = $subWeb.Lists["Pages"]

                Write-Host "Number of content types found in this list " $list.Title " are : " $list.ContentTypes.Count

                if($list.ContentTypes[$ct.Name] -ne $null)
                {
                    $listContentType = $list.ContentTypes[$ct.Name]

                    write-host "Working on List Content type : " $listContentType.Name " in this list : " $list.Title

                    foreach($spFieldLink in $fldLinks)
                    {
                        write-host "Checking : " $spFieldLink.DisplayName " existence in this content type : " $listContentType.Name

                        if($listContentType.FieldLinks[$spFieldLink.Id] -eq $null)
                        {
                            write-host "The field : " $spFieldLink.DisplayName " does not exists yet in this content type : " $listContentType.Name

                            try
                            {    
                                $listContentType.FieldLinks.Add($spFieldLink)
                                $listContentType.Update()

                                write-host "Field : " $spFieldLink.DisplayName "added to this content type : " $listContentType.Name -ForegroundColor Green
                            }
                            catch [Exception]
                            {
                                Write-Host "Adding Field : " $spFieldLink.DisplayName "to this content type : " $listContentType.Name ERROR! error $_.Exception.ToString() -ForegroundColor Red
                            }
                        }
                        else
                        {
                            write-host "The field : " $spFieldLink.DisplayName " ia already added to this list content type : " $listContentType.Name -ForegroundColor Green
                        }
                    }
                }
                else
                {
                    write-host "The List Content type : " $ct.Name " does not exists in this list : " $list.Title
                }
                $subWeb.Dispose()
            }
            Stop-SPAssignment $gc
        }
    }
}
else
{
    Write-Host "This content type usage: " + $ct.Name + " is not being used anywhere in this root web " + $rootWeb.Title  -ForegroundColor Magenta
}                

}

Using this script.

$site = Get-SPSite "http://sp2010"
$rootWeb = $site.RootWeb

$ct1 = $rootWeb.ContentTypes["Welcome Page"]
$ct2 = $rootWeb.ContentTypes["Tools Page"]

$ct = $ct1,$ct2

$spFieldLink = New-Object Microsoft.SharePoint.SPFieldLink ($rootWeb.Fields[[System.Guid]$field])

$fldLinks += @($spFieldLink)

ProcessContentType $rootWeb $ct $fldLinks

for the $field value, as you guys might have notice it is a guid field. What I did was, first create all the columns that I wants to add to the content types and then use SharePoint Manager to get the ID of that columns.

Thanks.

share|improve this answer
add comment

below link describes in details of use of adding site columns into content type.

http://thesharepointproject.com/2012/08/01/adding-site-columns-to-content-types-using-powershell/

Hope it helps...

share|improve this answer
    
Thanks, I've came across this site before posting this question. I'm looking for more efficient script, this script will iterate each subsite and document library. Im looking for a script that will use SPContentTypeUsage to find the library that has the predefined content types and add the fields. –  Agamand The True Jan 17 '13 at 2:06
add comment

You can try this:

$field = $web.Fields[$fieldName]
$contentType = $web.ContentTypes[$contentTypeName]
$link = new-object Microsoft.SharePoint.SPFieldLink $field
$contentType.FieldLinks.Add($link)
$contentType.Update($false, $false)

If you want to propagate use $true instead of $false in the first parameter of $contentType.Update($false, $false)

This is how you may perform the second part:

foreach($list in $web.Lists)
{
    if ($list.ContentTypes[$ContentTypeName] -ne null)
    {
        $listCT = $list.ContentTypes[$ContentTypeName];
        $link = new-object Microsoft.SharePoint.SPFieldLink $field
        $listCT.FieldLinks.Add($link)
        $listCT.Update($true)
        $list.Update()
    }
}
share|improve this answer
    
Thanks for trying to help, but it is not what I'm looking for. If you read my question carefully, you'll see that this part I have done. I need to propagate the changes manually. –  Agamand The True Jan 17 '13 at 2:00
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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