kwizcom banner advertisement

SharePoint Online: Update List Items using PowerShell

PowerShell CSOM Script to update List Items in SharePoint Online:
Here is the example for SharePoint online PowerShell to update list item.
#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
  
#Variables for Processing
$SiteUrl = "https://crescent.sharepoint.com/"
$ListName="Projects"

$UserName="admin@crescent.com"
$Password ="Password goes here"
 
#Setup Credentials to connect
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName,(ConvertTo-SecureString $Password -AsPlainText -Force))
 
#Set up the context
$Context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrl) 
$Context.Credentials = $credentials

try{ 
  
    #Filter and Get the List Items using CAML
    $List = $Context.web.Lists.GetByTitle($ListName)

    #Get List Item by ID
    $ListItem = $List.GetItemById(1)  

    #Update List Item title
    $ListItem["Title"] = "Project Darwin"  
    $ListItem.Update()  

    $Context.ExecuteQuery()
    write-host "Item Updated!"  -foregroundcolor Green  
}
catch{  
    write-host "$($_.Exception.Message)" -foregroundcolor red  
} 
This updates the Item with ID: 1 for the given list.
sharepoint online powershell update list item
The above script updates a particular list item. What if you want to update all items in the list?

PowerShell to Update All List Items in SharePoint Online:
#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
 
#Variables
$SiteURL="https://crescent.sharepoint.com"
$ListName="Projects"

Try {
    $Cred= Get-Credential
    $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)

    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = $Credentials
    $Web = $Ctx.web

    #Get the List    
    $List = $Ctx.Web.Lists.GetByTitle($ListName)
    $Ctx.Load($List)
    $Ctx.ExecuteQuery()

    #Get All List items
    $ListItemsCAML = New-Object Microsoft.SharePoint.Client.CamlQuery
    $ListItemsCAML.ViewXml = "<View Scope='RecursiveAll'></View>"
    $ListItems = $List.GetItems($ListItemsCAML)
    $Ctx.Load($ListItems)
    $Ctx.ExecuteQuery()

    Write-host "Total Items Found:"$List.ItemCount
    #Iterate through each item and update
    Foreach ($ListItem in $ListItems)
    {
        #Set New value for List column
        $ListItem["Reference"] = $ListItem["ID"]
        $ListItem.Update()
        $Ctx.ExecuteQuery() 
    }
    
    Write-host "All Items in the List: $ListName Updated Successfully!" -ForegroundColor Green  
}
Catch {
    write-host -f Red "Error Updating List Items!" $_.Exception.Message
} 
Tags: sharepoint online powershell edit list item, sharepoint online powershell update list item, powershell update list item sharepoint online
SharePoint Online: Update List Items using PowerShell SharePoint Online: Update List Items using PowerShell Reviewed by Salaudeen Rajack on 7:52 PM Rating: 5

4 comments:

Please Login and comment to get your questions answered!

Powered by Blogger.