How to add list column to list content type via SharePoint online PowerShell?
-
08-02-2021 - |
سؤال
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.
المحلول
Try using below code:
#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
Function Add-ColumnToListContentType()
{
param
(
[Parameter(Mandatory=$true)] [string] $SiteURL,
[Parameter(Mandatory=$true)] [string] $ListName,
[Parameter(Mandatory=$true)] [string] $ContentTypeName,
[Parameter(Mandatory=$true)] [string] $ColumnName,
[Parameter(Mandatory=$false)] [bool] $IsSiteColumn=$True
)
Try {
$Cred= Get-Credential
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = $Credentials
#Get the List
$List = $Ctx.Web.Lists.GetByTitle($ListName)
$Ctx.Load($List)
$Ctx.ExecuteQuery()
#Get the content type from list
$ContentTypeColl = $List.ContentTypes
$Ctx.Load($ContentTypeColl)
$Ctx.ExecuteQuery()
#Check if the content type exists in the list
$ContentType = $ContentTypeColl | Where {$_.Name -eq $ContentTypeName}
If($ContentType -eq $Null)
{
Write-host "Content Type '$ContentTypeName' doesn't exists in '$ListName'" -f Yellow
Return
}
#Get the column to add: Either site column or existing list's column
If($IsSiteColumn)
{
$ColumnColl = $Ctx.Web.Fields
}
else #List Column
{
$ColumnColl = $List.Fields
}
$Ctx.Load($ColumnColl)
$Ctx.ExecuteQuery()
$Column = $ColumnColl | Where {$_.Title -eq $ColumnName}
#Check if given column exists
if($Column -eq $Null)
{
Write-host "Column '$ColumnName' doesn't exists!" -f Yellow
Return
}
else
{
#Check if column already added to the content type
$FieldCollection = $ContentType.Fields
$Ctx.Load($FieldCollection)
$Ctx.ExecuteQuery()
$Field = $FieldCollection | Where {$_.Title -eq $ColumnName}
if($Field -ne $Null)
{
Write-host "Column '$ColumnName' Already Exists in the content type!" -f Yellow
Return
}
#Add field to content type
$FieldLink = New-Object Microsoft.SharePoint.Client.FieldLinkCreationInformation
$FieldLink.Field = $Column
[Void]$ContentType.FieldLinks.Add($FieldLink)
$ContentType.Update($false)
$Ctx.ExecuteQuery()
Write-host "Column '$ColumnName' Added to '$ContentTypeName' Successfully!" -ForegroundColor Green
}
}
Catch {
write-host -f Red "Error Adding Column to Content Type!" $_.Exception.Message
}
}
#Set parameter values
$SiteURL="https://crescent.sharepoint.com"
$ListName="Projects"
$ContentTypeName="Projects"
$ColumnName="Head Count"
$IsSiteColumn=$false
#Call the function
Add-ColumnToListContentType -SiteURL $SiteURL -ListName $ListName -ContentTypeName $ContentTypeName -ColumnName $ColumnName
Source:
SharePoint Online: Add Column to List Content Type using PowerShell
لا تنتمي إلى sharepoint.stackexchange