【发布时间】:2014-09-27 13:41:52
【问题描述】:
我正在使用 Powershell 将项目添加到使用 Sharepoint CSOM 的列表中。由于某些字段是Microsoft.Sharepoint.Client.FieldLookupValues,因此我必须查询其他列表以获取相应的 ID 值,然后将其添加到最近添加的项目中。以下是我查询 ID 的方式:
function getLookupValueID($_ctx, $_lookupListName, $_colName, $_value)
{
$lookupList = $_ctx.Web.Lists.GetByTitle($_lookupListName)
$_ctx.Load($lookupList)
$query = New-Object Microsoft.SharePoint.Client.CamlQuery
$query.ViewXML =
'<View>
<Query>
<Where>
<Eq>
<FieldRef Name="{0}"/>
<Value Type="Text">{1}</Value>
</Eq>
</Where>
</Query>
</View>' -f $_colName, $_value
$col = $lookupList.GetItems($query)
$_ctx.Load($col)
$_ctx.ExecuteQuery()
if($col.Count -gt 0)
{
return $col[0].ID
}
else
{
return $null
}
}
这是我如何添加项目的分解图:
$listCreateItem = $ctx.Web.Lists.GetByTitle("ListTitle")
$itemCreateInfo = New-Object Microsoft.SharePoint.Client.ListItemCreationInformation
$newItem = $listCreateItem.AddItem($itemCreateInfo)
$newItem['Title'] = "An actual title goes here"
$newItem['PjCode'] = "000-C00001CA"
$newItem['GroupCode'] = getLookupValueID $ctx "Group" "GroupCode" "LA"
$newItem['CustomerCode'] = getLookupValueID $ctx "Customer" "CustomerCode" "C00001"
$newItem['TermStart'] = "1/1/2013 8:00"
$newItem['TermEnd'] = "12/31/2015 8:00"
$newItem['ChargeFlag'] = "TRUE"
$newItem['AllDayFlag'] = "FALSE"
$newItem['PjColor'] = "Red"
$newItem.Update()
$ctx.ExecuteQuery()
我遇到的问题是,只有第二次调用 getLookupValueID 的值才会提交给数据库。这是我运行上述查询四次的结果:在 second 和 fourth 行中,我将 $newItem['CustomerCode'] = ... 移动到了 $newItem['GroupCode'] = ... 的上方:
事实上,如果我调用getLookupValueID(),那么当新项目添加到列表时$newItem['Title'] 和$newItem['PjCode'] 显示为空。将上面的屏幕截图与我在 Sharepoint 中添加项目但注释掉这些函数调用的屏幕截图进行比较:
我做错了什么?我真的是 Powershell 的新手,更不用说 Sharepoint 和 CSOM,所以我确信我犯了一些非常根本的错误。
【问题讨论】:
标签: powershell sharepoint csom