Question

I'm attempting to change the field contents for multiple entries in a list. So far I've gotten to the point that I can edit the list, add columns really, but can't find anything on how to edit the field text. Here is what I have currently:

EDIT: I've found a bunch of info for 2010 which isn't applicable but I've updated the code to almost get there. I'm getting 'null array' errors when I connect to the list now. I'm hopeful because I'm able to connect, but still can't get the field to change. I've updated my if statement as well to what is I believe a better format.

#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
Was it helpful?

Solution

I am guessing you have trimmed the script since you are missing things like defining $context and such. You don't have any ExecuteQuery() calls.

MSDN doc on SP 2013 CSOM List Item tasks, which has C# examples of what you need and can be translated to PowerShell.

It generally looks like you have everything but if you could include your whole script I can try and run your script directly.

EDIT: with the updates here is the code that you need

#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

You had a space in your Export Flag name and SharePoint will not have that in the name of the field. By default it will replace that with a _x0020_ string. This value will be based on the first name of the field. So if you change this field name in the future, you will still refer to it as 'Export_x0020_Flag' in this script. I am doing the .ExecuteQuery() for each update but you could do this once at the end of the loop. There is also the limit of 100 records in the query. If you only want records with "New" you should change the CamlQuery to just pull those records back.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top