【问题标题】:Change List Field with Powershell in SharePoint Online在 SharePoint Online 中使用 Powershell 更改列表字段
【发布时间】:2014-01-06 21:14:53
【问题描述】:

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

编辑: 我发现了一堆不适用的 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

【问题讨论】:

    标签: sharepoint powershell sharepoint-online


    【解决方案1】:

    我猜你已经修剪了脚本,因为你缺少定义 $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 以将这些记录拉回。

    【讨论】:

    • 我实际修剪的唯一内容是脚本底部的context.ExecuteQuery。我以前从未真正使用过 .ps1,所以我对上面的代码有点摸不着头脑。在这种情况下应该如何定义 $Context?
    • 我已经更新了 $Context 以便它按照链接的 C# 代码进行定义。
    • 所以该脚本运行完成,但没有对列表进行任何更改...它没有排除任何错误,它实际上没有更新任何内容?
    • 深入挖掘并在for 语句中添加另一个write-host 以尝试排除故障。它会在被要求时正确输出 ID #。 Id=$($item.Id) 在控制台中打印为“Id=11”。如果我尝试Id=$($item.Export_x0020_Flag),我只会得到“Id=”。作为输出。这就解释了为什么 If 语句失败了,但是知道为什么它没有读取列表项的内容吗?
    • 你不能做 $item.Export_x0020_Flag 因为那不是对象的属性。您需要使用 [] 并引用它以使其使用项目上的动态值。我确实测试了脚本,它对我有用。你能分享你最近运行的脚本吗?
    猜你喜欢
    • 1970-01-01
    • 2015-01-26
    • 2020-09-27
    • 2021-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-11
    • 2015-02-06
    相关资源
    最近更新 更多