Quantcast
Channel: SharePoint 2013 - Setup, Upgrade, Administration and Operations forum
Viewing all articles
Browse latest Browse all 21070

Exception calling Add with 1 argument

$
0
0

Hi,

My requirement is to create content types,site columns through powershell.

For this I am writing script as below,I am getting values content types,site columns from csv

//Structure of ContentTypes.csv

contenttypename,contenttypesinherits,columngroupname

ctype1,Enterprise Wiki Page,ctgrpname

ctype2,Document, ctgrpname

ctype3,Image, ctgrpname

//Structure of SiteColumns.csv

fieldname,fieldtype,columngroupname

mm1,Managed Metadata,clgrpname

mm2,Managed Metadata, clgrpname

mm3,Managed Metadata, clgrpname

mm4,Managed Metadata, clgrpname

sc1,Number, clgrpname

sc2,Number, clgrpname

//SiteColumns.ps1

#Path to Import Content Types csv

$ctypescsv = import-csv E:\ContentTypes.csv

#Path to Import Site Columns csv

$sitecolumnscsv = import-csv E:\ SiteColumns.csv

$wikiurl = “http://abc.com”

web = get-spweb $wikiurl

foreach($ctypes in $ctypescsv)

{

$ctypeName = $ctypes.contenttypename

$ctypeParent = $web.availablecontenttypes[$ctypes.contenttypesinherits]

$ctype = new-object Microsoft.SharePoint.SPContentType($ctypeParent, $web.contenttypes, $ctypeName)

$ctype.Group = $ctypegroupname

$web.contenttypes.add($ctype)

}

#----------Region to create Site Coulmns---------

foreach($scolumns in $sitecolumnscsv)

{

if($scolumns.fieldtype -eq "Managed Metadata")

{

#Creating Metadata column

$fieldName = $scolumns.fieldname

#Open the Site + Web

$site = Get-SPSite $wikiurl

$web = $site.RootWeb

#Gets a TaxonomySession object

$taxonomy = Get-SPTaxonomySession -Site $wikiurl

#Gets the GUID of the MMS Application

$sspId = $taxonomy.TermStores |

    Where-Object {$_.Name -eq $mmsappname} |

    Select-Object -ExpandProperty Id |

    Select-Object -ExpandProperty Guid

#Gets the GUID of the Term Set

$termSetId = $taxonomy.TermStores.Groups |

    Where-Object {$_.Name -eq $mmsgname} |

    Select-Object -ExpandProperty TermSets |

    Where-Object {$_.Name -eq $mmstsetname} |

    Select-Object -ExpandProperty Id |

    Select-Object -ExpandProperty Guid

#Creates a Field

$field = $web.Fields.CreateNewField("TaxonomyFieldType",$fieldName)

$web.fields.add($field)

#Updates with Term Set + SspId

$field = $web.fields.GetField($fieldName)

##$field.TermSetId = $termSetId

$field.SspId =  $sspId

$field.Required = $true

$field.Update()

$fieldLink = new-object Microsoft.SharePoint.SPFieldLink($field)

$ctype.fieldlinks.add($fieldLink)

$ctype.update()

}

else

{

#Add Fields to content type

$web.fields.Add($scolumns.fieldname,$scolumns.fieldtype, $false)

$field = $web.fields.getfield($scolumns.fieldname)

$field.Group = $scolumns.fieldname

$field.Required = $true

$fieldLink = new-object Microsoft.SharePoint.SPFieldLink($field)

$ctype.fieldlinks.add($fieldLink)

$ctype.update()

}

#----------End Region---------

Now the problem is while creating site columns which are of ‘Managed Metadata’

i.e after column mm1,mm2 getting created getting exception as follows

Exception calling "Add" with "1" argument(s): ""

At E:\Naga\SiteColumns.ps1:198 char:1

+ $ctype.fieldlinks.add($fieldLink)

+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException

    + FullyQualifiedErrorId : SPException

 

It is pointing to below line

$ctype.fieldlinks.add($fieldLink)

But when I am creating one Managed Metadata site column, it is working fine.

Please let me know if I am making any mistake..

Thanks & Regards,

Sudheer


Thanks & Regards, Sudheer


Viewing all articles
Browse latest Browse all 21070

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>