我試圖將發現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散列表中傳遞的內容來構建這些屬性。
就是這樣,謝謝。 –