1

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

3

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 as the basis.

$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.

1

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

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.