0

Good day folks…  Hope you are all doing well.

I have been banging my head against the wall on this one and it is driving me nuts (and preventing me from getting a job done for a client).

Anyway, here is the deal.

I have a custom solution, built originally on SharePoint 2010 about 6 years ago.   Upgraded the environment to SharePoint 2013 with the customizations (still in SharePoint 2010 WSP files).  They are asking me for some updates, so I need to build their environment first in my development environment.  I normally create powershell scripts to create a fresh site that mirrors their production environment and then start adding content types and site columns, lists etc.

So, I’m building new scripts to handle this for the complete environment in SharePoint 2013 with it’s unique distinctions.

I’m stuck here….  I have just added some content types using a standard SharePoint solution (WSP file), deployed and enabled features.  Then, I add, using powershell, those content types to the SharePoint lists as necessary.  This script worked back when it was originally installed on SharePoint 2010.

The content types are all visible from the SharePoint site using the web browser @ http://myserver/sites/cfms/

But, if I run the following script in power shell, the content types are missing from the output and the code (see below) that I’m using to add the content type fails..

$ver = $host | select version

if ($ver.Version.Major -gt 1) {$Host.Runspace.ThreadOptions = “ReuseThread”}
#Add-PsSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
#Remove-PSSnapin Microsoft.SharePoint.PowerShell -erroraction SilentlyContinue

cls
if ((Get-PSSnapin “Microsoft.SharePoint.PowerShell” -ErrorAction SilentlyContinue) -eq $null)
{
Add-PSSnapin “Microsoft.SharePoint.PowerShell”
}

# see http://adicodes.com/deploying-wsp-with-powershell/
$_siteCollectionRoot = “http://myserver:9999”
$_site = $_siteCollectionRoot + “/sites/cfms”
$_web = $_site + “/”
$RetractAndRemove = $false

iisreset -noforce

$siteObj = Get-SPSite $_site
$webObj = Get-SPWeb $_web

$out = “c:\ContentTypes.txt”

if (Test-Path $out)
{
Remove-Item $out
}

$cts = $webObj.ContentTypes

echo “Processing…”

‘”CT Name”‘ + `
‘,”CT ID”‘ + `
‘,”CT Description”‘ + `
‘,”CT Group”‘ +
‘,”Field Title”‘ + `
‘,”Field Internal Name”‘ + `
‘,”Field ID”‘ + `
‘,”Field Group”‘ + `
‘,”Field Max Length”‘ + `
‘,”Field Description”‘ | Out-File $out

ForEach ($id in $cts)
{
ForEach ($field in $id.Fields)
{
‘”‘ + $id.Name + `
‘”,”‘ + $id.Id + `
‘”,”‘ + $id.Description + `
‘”,”‘ + $id.Group + `
‘”,”‘ + $field.Title + `
‘”,”‘ + $field.InternalName + `
‘”,”‘ + $field.Id + `
‘”,”‘ + $field.Group + `
‘”,”‘ + $field.MaxLength + `
‘”,”‘ + $field.Description + `
‘”‘ | Out-File $out -append
}
}

Write-Host “Script completed.”

The actual code I’m using to add the content types is as follows:

$contentTypeToAdd = $siteObj.RootWeb.ContentTypes[“Cope Recording Log”]
$list= $webObj.Lists[“Item List”]
$list.ContentTypesEnabled = $true
$list.update()
$list.ContentTypes.Add($contentTypeToAdd)
$list.update()

The error I get when running this code is:

Exception calling “Add” with “1” argument(s): “Object reference not set to an instance of an object.”
At C:\Users\[userid]\Source\Workspaces\ChoicesForChange\CFMSUpdatesSept2015\CSOMUpdater\PreDeployPH2-Update1.ps1:206 char:1
+ $list.ContentTypes.Add($contentTypeToAdd)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : NullReferenceException

Any theories?

Please note that the “Cope Recording Log” content type is visible in the SharePoint 2013 web client.

Thanks!

(Visited 100 times, 1 visits today)
Add a Comment