I'm trying to add some custom property to list content type at SharePoint Online site by following script:
$context=New-Object Microsoft.SharePoint.Client.ClientContext($URL)
$context.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Username, $AdminPassword)
$context.ExecuteQuery()
$web = $context.Web
$context.Load($web)
$context.ExecuteQuery()
$lib = $web.Lists.GetByTitle("Document Library")
$context.Load($lib)
$context.ExecuteQuery()
$cts = $lib.ContentTypes
$context.Load($cts)
$context.ExecuteQuery()
ForEach ($c in $cts) {
if($c.Name -eq "Document") {
$ct = $c
}
}
$fields = $lib.Fields
$context.Load($fields)
$context.ExecuteQuery()
ForEach ($fl in $fields) {
if($fl.Title -eq "Keyword") {
$keyword = $fl
$context.Load($keyword)
$context.ExecuteQuery()
}
}
$link=new-object Microsoft.SharePoint.Client.FieldLinkCreationInformation
$link.Field=$keyword
$ct.FieldLinks.Add($link)
$ct.Update($false)
$context.ExecuteQuery()
But I got exception below while executing.
Exception setting "Field": "Cannot convert the
"Microsoft.SharePoint.Client.FieldText" value of type
"Microsoft.SharePoint.Client.FieldTe xt" to type
"Microsoft.SharePoint.Client.Field"." At D:\tmp\AddContentType.ps1:47
char:44
+ [cultureinfo]::CurrentUICulture = 'en-US'; $link.Field=$keyword
+ ~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], SetValueInvocationException
+ FullyQualifiedErrorId : ExceptionWhenSetting Cannot convert argument "parameters", with value:
"Microsoft.SharePoint.Client.FieldLinkCreationInformation", for "Add"
to type "Microsof t.SharePoint.Client.FieldLinkCreationInformation":
"Cannot convert the
"Microsoft.SharePoint.Client.FieldLinkCreationInformation" value o f
type "Microsoft.SharePoint.Client.FieldLinkCreationInformation" to
type "Microsoft.SharePoint.Client.FieldLinkCreationInformation"." At
D:\tmp\AddContentType.ps1:51 char:1
+ $ct.FieldLinks.Add($link)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodException
+ FullyQualifiedErrorId : MethodArgumentConversionInvalidCastArgument
Samples I have found so far are all about adding site columns to content type, not list column. Is there any way to add list column to list content type via PowerShell?
Thanks in advance.