2

I need to create a content type from one site coll to another, I found this link:

http://get-spscripts.com/2011/02/export-and-importcreate-site-content.html

The export works perfectly:

$sourceWeb = Get-SPWeb http://mysite.com.com/sites/ict
$xmlFilePath = "C:\whatever\Script-SiteContentTypes.xml"

#Create Export File
New-Item $xmlFilePath -type file -force

#Export Content Types to XML file
Add-Content $xmlFilePath "<?xml version=`"1.0`" encoding=`"utf-8`"?>"
Add-Content $xmlFilePath "`n<ContentTypes>"
$sourceWeb.ContentTypes | ForEach-Object {
    if ($_.Group -eq "XYZ - ICT Documents - Content Type Group") {
        Add-Content $xmlFilePath $_.SchemaXml
    }
}
Add-Content $xmlFilePath "</ContentTypes>"

$sourceWeb.Dispose()

The import however doesnt work

All I get is this:

New-Object : Constructor not found. Cannot find an appropriate constructor for type Microsoft.SharePoint.SPFieldLink.
At C:\whatever\Import ICT Content Type.ps1:19 char:28
+             $spFieldLink = New-Object Microsoft.SharePoint.SPFieldLink ($destWeb ...
+                            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (:) [New-Object], PSArgumentException
    + FullyQualifiedErrorId : CannotFindAppropriateCtor,Microsoft.PowerShell.Commands.NewObjectCommand

The script is like this:

$destWeb = Get-SPWeb http://mysite.com.com
$xmlFilePath = "C:\whatever\Script-SiteContentTypes.xml"

#Create Site Content Types
$ctsXML = [xml](Get-Content($xmlFilePath))
$ctsXML.ContentTypes.ContentType | ForEach-Object {

    #Create Content Type object inheriting from parent
    $spContentType = New-Object Microsoft.SharePoint.SPContentType ($_.ID,$destWeb.ContentTypes,$_.Name)

    #Set Content Type description and group
    $spContentType.Description = $_.Description
    $spContentType.Group = $_.Group

    $_.Fields.Field  | ForEach-Object {
        if(!$spContentType.FieldLinks[$_.DisplayName])
        {
            #Create a field link for the Content Type by getting an existing column
            $spFieldLink = New-Object Microsoft.SharePoint.SPFieldLink($destWeb.Fields[$_.DisplayName])

            #Check to see if column should be Optional, Required or Hidden
            if ($_.Required -eq "TRUE") {$spFieldLink.Required = $true}
            if ($_.Hidden -eq "TRUE") {$spFieldLink.Hidden = $true}

            #Add column to Content Type
            $spContentType.FieldLinks.Add($spFieldLink)
        }
    }

    #Create Content Type on the site and update Content Type object
    $ct = $destWeb.ContentTypes.Add($spContentType)
    $spContentType.Update()
    write-host "Content type" $ct.Name "has been created"
}

$destWeb.Dispose()
2

The problem came from the argument passed to SPFieldLink contructor :

$destWeb.Fields[$_.DisplayName]

if the column you are looking for is custom and not created yet in the destination site collection it's normal that you get an error. And it's clear that this script doesn't take in charge the creation of the custom Columns.

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.