【问题标题】:How do I properly perform a subquery for an ID when adding items to a list using Sharepoint CSOM?使用 Sharepoint CSOM 将项目添加到列表时,如何正确执行 ID 子查询?
【发布时间】: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 的值才会提交给数据库。这是我运行上述查询四次的结果:在 secondfourth 行中,我将 $newItem['CustomerCode'] = ... 移动到了 $newItem['GroupCode'] = ... 的上方:

事实上,如果我调用getLookupValueID(),那么当新项目添加到列表时$newItem['Title']$newItem['PjCode'] 显示为空。将上面的屏幕截图与我在 Sharepoint 中添加项目但注释掉这些函数调用的屏幕截图进行比较:

我做错了什么?我真的是 Powershell 的新手,更不用说 Sharepoint 和 CSOM,所以我确信我犯了一些非常根本的错误。

【问题讨论】:

    标签: powershell sharepoint csom


    【解决方案1】:

    在客户端对象模型中,调用 Load 和 ExecuteQuery 方法的顺序很重要。每个 API 调用(如 Load 或 AddItem)都将在下次调用 ExecuteQuery 期间执行。例如,您在函数的最顶部调用 AddItem 并且您希望它在底部的 ExecuteQuery 方法期间执行,但事实并非如此。 AddItem 调用将在第一次 getLookupValueID 调用期间执行,因为它调用 ExecuteQuery。试试这个:

    $groupCode = getLookupValueID $ctx "Group" "GroupCode" "LA"
    $customerCode =  getLookupValueID $ctx "Customer" "CustomerCode" "C00001"
    $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'] = $groupCode
    $newItem['CustomerCode'] = $customerCode
    $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()
    

    【讨论】:

    • 生成第二个上下文来处理这些子查询会不会太昂贵?因此,我没有将$ctx 传递给getLookupValudID(),而是在函数内部创建一个新函数并使用它执行我的子查询,而不是处理列表项插入的上下文?
    • 嗯,这取决于您要运行多少查询。如果是一次性工作,那可能没什么大不了的。
    猜你喜欢
    • 1970-01-01
    • 2018-02-13
    • 1970-01-01
    • 1970-01-01
    • 2021-02-25
    • 2021-01-29
    • 2015-12-20
    • 2021-07-11
    • 1970-01-01
    相关资源
    最近更新 更多