Title: Update List items on Conditional based using CAML Query
- 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"
- $SiteURL="https://hpeblore.sharepoint.com/sites/Omi"
- $ListName="Employee"
- 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 and load the List
- $List = $Ctx.Web.Lists.GetByTitle($ListName)
- $Ctx.Load($List)
- $Ctx.ExecuteQuery()
- #Get Selected List items
- $CAMLQuery = New-Object Microsoft.SharePoint.Client.CamlQuery
- #$CAMLQuery.ViewXml = "<View><Query><Where><Eq><FieldRef Name='Title' /><Value Type='Text'>Bramhendra</Value></Eq></Where></Query></View>"
- $CAMLQuery.ViewXml = "<View><Query><Where><And><Eq><FieldRef Name='Title' /><Value Type='Text'>Bramhendra</Value></Eq><Eq><FieldRef Name='Location' /><Value Type='Text'>Bangalore</Value></Eq></And></Where></Query></View>"
- # yyyy-MM-dd # <FieldRef Name='DOJ' /><Value IncludeTimeValue='TRUE' Type='DateTime'>2018-04-13</Value>
- $ListItems = $List.GetItems($CAMLQuery)
- $Ctx.Load($ListItems)
- $Ctx.ExecuteQuery()
- Write-host $ListItems.Count
- if( $ListItems.Count -gt 1)
- {
- write-host "Many items found for "
- }
- elseif( $ListItems.Count -eq 1)
- {
- Foreach ($ListItem in $ListItems) # it loops only above filter item
- {
- if($ListItem["Title"] -eq "Bramhendra" -and $ListItem["Location"] -eq "Bangalore") # add if dropaccount=SVC_Sharepoint
- {
- $ListItem["Location"] = "Bangalore2"
- #write-host $ListItem["ID"]
- $ListItem.Update()
- $Ctx.ExecuteQuery()
- }
- }
- }
- else
- {write-host " items not found"}
- }
- Catch {
- write-host -f Red "Error Updating List Items!" $_.Exception.Message
- }