0

I have a content type which is getting deployed through a wsp package. The elements.xml of it is

<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
<!-- Parent ContentType: Page (xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx) -->
<ContentType ID="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" Name="My ContentType" 
    Group="Content Types" 
    Description="Content type" 
    Inherits="FALSE" 
    Version="0">
    <FieldRefs>
        <FieldRef ID="{xxxxxxxxxx}" DisplayName="Col 1" Required="FALSE" Name="Col1" />
    </FieldRefs>
</ContentType>
</Elements>

So if you notice the xml contains Inherits="FALSE",

I need a PowerShell script which will set Inherits to TRUE.

2 Answers 2

2

As far as I know, that property isn't mapped to an actual member on the SPContentType object. Setting the Inherits flag in a CAML content type declaration only result in a different set of fields begin added to the content type field link during the initial creation process.

This means that you cannot modify that property at a later time. What you can do, instead, is adding the missing fields you require to the field link collection.

Something like this should do the trick:

Add-PSSnapin Microsoft.SharePoint.PowerShell 
// register the PSSnapin if required by your context, otherwise skip it.

$webInstance = Get-SPWeb "http://somesite"
$contentTypeInstance = $webInstance .ContentTypes["My ContentType"]

$webFields = $webInstance.Fields

$fieldToAdd = $webFields.GetFieldByInternalName("field name goes here")
$contentTypeInstance.FieldLinks.Add($fieldToAdd)

// ... repeat to add the other fields as needed ...

$contentTypeInstance.Update($true)
1
  • Thanks for your answer, based on your suggestion I have prepared a CSOM PowerShell function which add columns to content type from its parent content type.
    – Asad Refai
    Commented Jun 10, 2015 at 9:29
0

Once content type is deployed and if we are finding difficulty in setting it to inherit it's parent's column, then this PowerShell function using client object model to add columns in content type from it's parent content type is helpful.

Try{
Add-Type -Path 'C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.dll'
Add-Type -Path 'C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Runtime.dll'
Add-Type -Path 'C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Publishing.dll'
Add-Type -Path 'C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Taxonomy.dll'
}

catch {
    Throw "Unable to load SharePoint Client runtime"
}

function AddFieldFromParentContentType()
{
    param(
        [Parameter(Mandatory=$true)][string]$url,
        [Parameter(Mandatory=$false)][System.Net.NetworkCredential]$credentials,
        [Parameter(Mandatory=$true)][string]$ContentTypeName,
        [Parameter(Mandatory=$true)][string]$FieldInternalName
    )

    begin{
        try
        {
            #Get Client Object
            #
            $Context = New-Object Microsoft.SharePoint.Client.ClientContext($url) 
            $Context.Credentials = $Credentials 

            #Load web object
            #
            $web = $Context.Web
            $fields = $web.Fields;         
            $site = $context.Site 
            $Context.Load($web)
            $Context.Load($site)
            $Context.Load($fields)
            $Context.ExecuteQuery()

        }
        catch
        {
            Write-Host "Error while getting context. Error -->> "  + $_.Exception.Message -ForegroundColor Red
        }
    }
    process{
        try
        {
            #Get content type collections from root web
            #
            $contentTypes = $Context.Site.RootWeb.ContentTypes
            $Context.Load($contentTypes)
            $Context.ExecuteQuery()

            #Get the desired content type from collection
            #
            $contentType = $contentTypes | Where {$_.Name -eq $ContentTypeName}

            #Get parent content type
            #
            $parentContentType = $contentType.Parent
            $Context.Load($parentContentType)
            $Context.ExecuteQuery()

            #Get fields of parent content type
            #
            $fieldCollection = $parentContentType.Fields
            $Context.Load($fieldCollection);
            $Context.ExecuteQuery();

            #Check if field exists in parent content type
            #
            $IsFieldExists = $false
            foreach($fld in $fieldCollection)
            {
                if($fld.get_InternalName() -eq $FieldInternalName){
                    $IsFieldExists = $true
                    $fieldToAdd = $fld
                }
            }


            if($IsFieldExists -eq $true)
            {
                #Get all the columns associated with content type
                #
                $fieldRefCollection = $contentType.Fields;
                $Context.Load($fieldRefCollection);
                $Context.ExecuteQuery();

                #Check if field exists in content type
                #
                $IsFieldInContentTypeExists = $false
                foreach($fldInCT in $fieldRefCollection)
                {
                    if($fldInCT.get_InternalName() -eq $fieldToAdd.InternalName){
                        $IsFieldInContentTypeExists = $true
                    }
                }

                if($IsFieldInContentTypeExists -eq $false) {
                    #Add a new column to the content type
                    #
                    $fieldReferenceLink = New-Object Microsoft.SharePoint.Client.FieldLinkCreationInformation
                    $fieldReferenceLink.Field = $fieldToAdd;
                    $contentType.FieldLinks.Add($fieldReferenceLink);

                    $contentType.Update($true)
                    $Context.Load($contentType)
                    $Context.ExecuteQuery()

                    Write-Host "Field" $FieldInternalName "from parent content type added successfully" -ForegroundColor Green
                    Write-Host ""
                }
                else {
                    Write-Host "Field in content type" $contentType.Name "already exists"  -ForegroundColor Green
                    Write-Host ""
                }
            }
            else
            {
                Write-Host "Field" $FieldInternalName "in parent content type" $parent.Name "do not exists" -ForegroundColor Cyan
                Write-Host ""
            }
        }
        catch
        {
            Write-Host ("Error while adding field in contet type. Error -->> " + $_.Exception.Message) -ForegroundColor Red
        }
    }
    end{
        $Context.Dispose()
    }
}

#Call function this way
$credentials = Get-Credential
AddFieldFromParentContentType "http://yoursite" $credentials "Your Content Type" "ColumnInternalName"

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.