Hi,
I am planning to create content types and site columns through script.
Some site columns format is Number and some site coumns format is of Managed Metadata
Post creating site columns,I am suppose add those all site columns to content types.
For this I had written script as below
//sitecolumns.csv
fieldname,fieldtype,columngroupname
fieldname1, Managed Metadata, stdcolumngroupname
fieldname2, Managed Metadata, stdcolumngroupname
fieldname3, Number, stdcolumngroupname
fieldname4, Number, stdcolumngroupname
// ContentTypes.csv
contenttypename,contenttypesinherits
contenttype1,Enterprise Wiki Page
contenttype2,Document
contenttype3,Image
//automationscript.ps1
Start-SPAssignment –Global
$web = get-spweb “http://abc.com”
foreach($ctypes in $ctypescsv)
{
$ctypeName = $ctypes.contenttypename
##$web.AvailableContentTypes | Select-Object Name
$ctypeParent = $web.availablecontenttypes[$ctypes.contenttypesinherits]
$ctype = new-object Microsoft.SharePoint.SPContentType($ctypeParent, $web.contenttypes, $ctypeName)
$ctype.Group = $ctypegroupname
$web.contenttypes.add($ctype)
foreach($scolumns in $sitecolumnscsv)
{
if(($scolumns.fieldtype -eq "Managed Metadata") -and (($ctypes.contenttypename -eq " contenttype1") -or ($ctypes.contenttypename -eq " contenttype2") -or ($ctypes.contenttypename -eq " contenttype3")))
{
#Creating Metadata column
$fieldName = $scolumns.fieldname
#Open the Site + Web
$site = Get-SPSite “http://abc.com”
$web = $site.RootWeb
#Gets a TaxonomySession object
$taxonomy = Get-SPTaxonomySession -Site “http://abc.com”
#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.Update()
$fieldLink = new-object Microsoft.SharePoint.SPFieldLink($field)
$ctype.fieldlinks.add($fieldLink)
$ctype.update()
}
elseif(($scolumns.fieldtype -eq "Number") -and (($ctypes.contenttypename -eq " contenttype1") -or ($ctypes.contenttypename -eq " contenttype2") -or ($ctypes.contenttypename -eq " contenttype3")))
{
//Add Fields to content type
$web.fields.add($scolumns.fieldname,$scolumns.fieldtype, $false)
$field = $web.fields.getfield($scolumns.fieldname)
$field.Group = $scolumns.fieldname
$fieldLink = new-object Microsoft.SharePoint.SPFieldLink($field)
$ctype.fieldlinks.add($fieldLink)
$ctype.update()
}
}
}
#-----------------------------------------------------------------------------------
Stop-SPAssignment -Global
#---------------------------------------------------------------------------------------------------
#EndRegion
Now after executing the above script,noticed that each site columns is created for 3 times and
At certain cases i.e it is throwing message as ‘Cannot convert argument to SPFieldlinkAdd’
Please let me know if I am making any mistake.
Regards,
Sudheer
Thanks & Regards, Sudheer