使用CSOM更新SharePoint Online列/内容类型

时间:2014-06-01 18:39:56

标签: powershell sharepoint csom sharepoint-online

我有以下csom脚本来更新网站列并按下更改:

Add-Type -Path "Libraries\Microsoft.SharePoint.Client.dll" 
Add-Type -Path "Libraries\Microsoft.SharePoint.Client.Runtime.dll" 

$webUrl = "enter_url_here" 
$username = "enter_username_here"
$password = "enter_password_here"
$securePass = ConvertTo-SecureString $password -AsPlainText -Force

$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($webUrl)
$ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $securePass)

#Add description to the "Other category" field
$fieldTitle = "Other category"
$fieldDesc = "Search for an existing name before adding new entries"
$field = $ctx.Site.RootWeb.Fields.GetByInternalNameOrTitle($fieldTitle)
$field.Description = $fieldDesc
$field.UpdateAndPushChanges($true)

#Add description to the "Initiator location" field
$field2Title = "Initiator location"
$field2 = $ctx.Site.RootWeb.Fields.GetByInternalNameOrTitle($field2Title)
$field2.Description = $fieldDesc
$field2.UpdateAndPushChanges($true)

#Setting the "Main-category" field as required/mandatory
$field3Title = "Main-category"
$field3 = $ctx.Site.RootWeb.Fields.GetByInternalNameOrTitle($field3Title)
$field3.Required = $true
$field3.UpdateAndPushChanges($true)

$ctx.ExecuteQuery()

以上字段/列都是名为“主要文档”的网站内容类型的一部分。

到目前为止,这工作正常,但对于“Main-category”字段,如何在内容类型级别将其设置为“Required”?目前这只是在网站列级别设置,实现这一目标的最简单方法是什么?

非常感谢。

1 个答案:

答案 0 :(得分:2)

使用ContentType.FieldLinks property获取内容类型中的字段引用,然后使用FieldLink.Required property设置一个值,该值指定字段是否需要值。

实施例

如何通过内容类型为字段设置Required属性:

$ct = $ctx.Web.ContentTypes.GetById($ctId) #get Content Type 
$fieldLink = $ct.FieldLinks.GetById($fieldId) #get Field Link
$fieldLink.Required = $true
$ct.Update($true)
$ctx.ExecuteQuery()