我正在尝试更改列表中多个条目的字段内容。到目前为止,我已经可以编辑列表,真正添加列了,但是找不到关于如何编辑字段文本的任何内容。这是我目前拥有的:
编辑:
我找到了2010年的大量信息,但这些信息不适用,但我已经更新了代码,几乎可以实现。现在连接到列表时出现“空数组”错误。我充满希望,因为我能够联系,但仍然无法改变 Realm 。我也将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
最佳答案
我猜您已经修剪了脚本,因为您缺少定义$ 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以将这些记录拉回。
关于sharepoint - 在SharePoint Online中使用Powershell更改列表字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20662448/