在SharePoint Online中使用Powershell更改列表字段

时间:2013-12-18 15:45:13

标签: sharepoint powershell sharepoint-online

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

编辑: 我发现2010年的一堆信息不适用,但我已经更新了代码,几乎到了那里。我现在连接到列表时出现'null array'错误。我很有希望,因为我能够连接,但仍然无法改变领域。我已经更新了我的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

1 个答案:

答案 0 :(得分:3)

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

MSDN doc on SP 2013 CSOM List Item tasks,它包含您需要的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以便将这些记录拉回来。