我试图将发现here的脚本改为与PowerShell CSOM等效。在属性中使用散列表键
function setFieldVisibility(listTitle,fieldName,properties,success,failure) { var ctx = SP.ClientContext.get_current(); var web = ctx.get_web(); var list = web.get_lists().getByTitle(listTitle); var field = list.get_fields().getByTitle(fieldName); field.setShowInDisplayForm(properties.ShowInDisplayForm); field.setShowInNewForm(properties.ShowInNewForm); field.setShowInEditForm(properties.ShowInEditForm); field.set_hidden(properties.Hidden); field.update(); ctx.executeQueryAsync(success,failure); }
在推广它的过程中,我试图在哈希表中通过。我想迭代这个散列表来动态地构建要编辑的属性,但是我遇到了一些麻烦。我的功能和用法如下:
Function Set-FieldProperties{
param(
[Parameter(Mandatory=$true)][string]$Url,
[Parameter(Mandatory=$true)][string]$ListTitle,
[Parameter(Mandatory=$true)][string]$FieldName,
[Parameter(Mandatory=$true)][hashtable]$Properties
)
begin {
$context = New-Object Microsoft.SharePoint.Client.ClientContext($Url)
$context.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($credential.UserName, $credential.Password)
}
process{
$web = $context.Web
$list = $web.Lists.GetByTitle($ListTitle)
$field = $list.Fields.GetByTitle($FieldName)
foreach($key in $Properties.Keys){
$key
$Properties[$key]
#failing to make this part dynamic
$field.$key($Properties[$key])
}
<#$field.ShowInDisplayForm($properties.ShowInDisplayForm)
$field.ShowInNewForm($properties.ShowInNewForm)
$field.ShowInEditForm($properties.ShowInEditForm)
$field.Hidden($properties.Hidden)
try{
$field.update()
$context.executeQuery()
Write-Host -ForegroundColor Green "Field properties updated successfully"
}
catch{
Write-Host -ForegroundColor Red $_.Exception.Message
}
#>
}
end{
$context.Dispose()
}
}
Set-FieldPoperties -Url "https://tenant.sharepoint.com/teams/eric" -ListTitle "CalcColumns" -FieldName "Title" -Properties @{"SetShowInDisplayForm"=$true; "SetShowInEditForm"=$false}
我的奋斗是如何让这部分建立所需的输出:
foreach($key in $Properties.Keys){
$key
$Properties[$key]
#failing to make this part dynamic
$field.$key($Properties[$key])
}
如何使用密钥作为一个属性上设置在这种情况下$ field对象?那可能吗?我不希望有一些巨大的if块检查所有可能的属性,我希望它根据用户在$ Properties散列表中传递的内容来构建这些属性。
就是这样,谢谢。 –