2013-12-18 31 views
1

我正在嘗試更改列表中多個條目的字段內容。到目前爲止,我已經到了可以編輯列表的位置,真正添加列,但無法找到關於如何編輯字段文本的任何內容。這是我目前的:在SharePoint Online中使用Powershell更改列表字段

編輯: 我發現了一堆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 = "[email protected]" 
    $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 

回答

3

我猜你已經修剪腳本,因爲你缺少像定義$上下文等東西。您沒有任何ExecuteQuery()調用。

MSDN doc on SP 2013 CSOM List Item tasks,它具有您需要並可以轉換爲PowerShell的C#示例。

它通常看起來像你有一切,但如果你可以包括你的整個腳本,我可以嘗試直接運行你的腳本。

編輯:這裏的更新就是你需要

#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 = "[email protected]" 
$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條記錄的限制。如果您只想要「新建」的記錄,則應將CamlQuery更改爲僅將這些記錄拉回。

+0

我實際上修剪的唯一東西是腳本底部的'context.ExecuteQuery'。我以前從來沒有真正使用.ps1,所以我有點在上面的代碼在黑暗中刺傷。應該在這種情況下定義$ Context? – Benjooster

+0

我已更新$ Context,以便根據鏈接的C#代碼進行定義。 – Benjooster

+0

因此,腳本運行完成,但不會對列表進行任何更改...它不會排除任何錯誤,它只是不會實際更新任何內容? – Benjooster

相關問題