1

我正在尝试更改列表中多个条目的字段内容。到目前为止,我已经到了可以编辑列表、添加列的地步,但找不到任何关于如何编辑字段文本的内容。这是我目前拥有的:

编辑:我发现了一堆不适用的 2010 年信息,但我已经更新了代码以几乎到达那里。现在连接到列表时出现“空数组”错误。我充满希望,因为我能够连接,但仍然无法改变该领域。我也将我的 if 语句更新为我认为更好的格式。

#Load necessary module to connect to SPOService
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client") | Out-Null
    [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client.Runtime") | Out-Null
    #Login Information for script

    $User = "user@email.com"
    $Pass = "password"
    $creds = New-Object System.Management.Automation.PSCredential($User, (ConvertTo-SecureString $Pass -AsPlainText -Force));

    #Connect to SharePoint Online service
    Write-Host "Logging into SharePoint online service." -ForegroundColor Green
    Connect-SPOService -Url https://site-admin.sharepoint.com -Credential $creds

    #Get the Necessary List
    Write-Host "Getting the required list." -ForegroundColor Green
    $WebUrl = 'https://site.sharepoint.com/'
    $Context = New-Object Microsoft.SharePoint.Client.ClientContext($WebUrl)
    $List = $Context.Web.Lists.GetByTitle("VacationRequestForm")

    #Edit existing list items
    $items = $List.items
    foreach($item in $items)
    {
    if($item["Export Flag"] -eq "New")
    {
    Write-Host "Changing export flags to Complete." -ForegroundColor Green
    $item["Export Flag"] = "Complete"
    $item.Update()
    }
    }

    Write-Host "Your changes have now been made." -ForegroundColor Green
4

1 回答 1

3

我猜你已经修剪了脚本,因为你缺少定义 $context 之类的东西。您没有任何 ExecuteQuery() 调用。

SP 2013 CSOM List Item tasks 上的 MSDN 文档,其中包含您需要的 C# 示例,并且可以翻译为 PowerShell。

通常看起来您拥有一切,但如果您可以包含整个脚本,我可以尝试直接运行您的脚本。

编辑:这里的更新是您需要的代码

#Load necessary module to connect to SPOService
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client") | Out-Null
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client.Runtime") | Out-Null

#Login Information for script
$User = "user@email.com"
$Pass = "password"

$WebUrl = "https://site.sharepoint.com/"

#Connect to SharePoint Online service
Write-Host "Logging into SharePoint online service." -ForegroundColor Green

$Context = New-Object Microsoft.SharePoint.Client.ClientContext($WebUrl)
$Context.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($User, (ConvertTo-SecureString $Pass -AsPlainText -Force))

#Get the Necessary List
Write-Host "Getting the required list." -ForegroundColor Green
$List = $Context.Web.Lists.GetByTitle("VacationRequestForm")

$Query = [Microsoft.SharePoint.Client.CamlQuery]::CreateAllItemsQuery(100); 
$Items = $List.GetItems($Query);

$Context.Load($Items);
$Context.ExecuteQuery();

#Edit existing list items
foreach($item in $Items)
{
    if($item["Export_x0020_Flag"] -eq "New")
    {
        Write-Host "Changing export flags to Complete for Id=$($item.Id)." -ForegroundColor Green
        $item["Export_x0020_Flag"] = "Complete"
        $item.Update()
        $Context.ExecuteQuery();
    }
}

Write-Host "Your changes have now been made." -ForegroundColor Green

您的导出标志名称中有一个空格,而 SharePoint 字段名称中将没有该空格。默认情况下,它将用 _x0020_ 字符串替换它。此值将基于字段的名字。因此,如果您将来更改此字段名称,您仍将在此脚本中将其称为“Export_x0020_Flag”。我正在.ExecuteQuery()为每次更新执行此操作,但您可以在循环结束时执行一次。查询中还有 100 条记录的限制。如果您只想要带有“New”的记录,您应该更改 CamlQuery 以将这些记录拉回来。

于 2014-01-02T07:13:16.523 回答