Update Sharepoint Online library document using Powershell

…this post was published on 2017 January 10.

if you need to update a Sharepoint online library d​ocument, or picture, you can use the following script.
The file, zipped can be found at the bottom , with the link to a video presentation of the script

<#
DESCRIPTION
This script can be useful to update a specific field on a library located on a Sharepoint Online tenant
You need to know 5 mandatory parameters:
    – URL         : the URL for the tenant
    – Library     : the name of the library where you need to update a doc/picture
    – ID          : the ID for that doc
    – Field       : the name for the field
    – NewValue    : the new value. If necessary change the type from [int] to [string]
CopyRight
This script can be use as it is or can be changed based on your needs. For more details please feel free and contact me
Romeo Donca, January 2017
romeodonca@outlook.com
http://www.romeodonca.ro
#>
param(
[Parameter(Mandatory=$true,HelpMessage=”The url for the tenant where the picture library is, like https://TENANT-public.sharepoint.com”)][string]$Url,
[Parameter(Mandatory=$true,HelpMessage=”The name for the library”)][string]$Library,
[Parameter(Mandatory=$true,HelpMessage=”The ID for the picture in that library”)][int]$ID,
[Parameter(Mandatory=$true,HelpMessage=”The field name that you want to be changed”)][string]$Field,
[Parameter(Mandatory=$true,HelpMessage=”The new value for that field”)][int]$NewValue
)
cls
Add-Type -Path “E:\DLLs\Microsoft.SharePoint.Client.dll”
Add-Type -Path “E:\DLLS\Microsoft.SharePoint.Client.Runtime.dll”
Import-Module MSOnline
$global:cred = Get-Credential
Connect-MsolService -Credential $global:cred -ErrorAction SilentlyContinue
$clientContext = New-Object Microsoft.SharePoint.Client.ClientContext($url)
$cred = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($cred.username, $cred.Password) 
$clientContext.Credentials = $cred
$web = $clientContext.Web
$clientContext.Load($web)
$clientContext.ExecuteQuery()
$mylist = $web.GetList($url+”/”+$library)
$clientContext.Load($mylist)
$clientContext.ExecuteQuery()
 
$caml=”<View><Query><Where><Geq><FieldRef Name=’ID’/><Value Type=’Number’>$ID</Value></Eq></Where></Query></View>”
$cquery = New-Object Microsoft.SharePoint.Client.CamlQuery
$cquery.ViewXml=$caml   
$listItemsinFlickr = $mylist.GetItems($cquery)
$clientContext.Load($listItemsinFlickr)
$clientContext.ExecuteQuery()

$continue = Read-Host ” You really want to modify fields for  picture ”  $listItemsinFlickr.FieldValues.Title ” ? (Y/N)”
while (!(($continue.ToUpper() -match ‘Y’) -or ($continuare.ToUpper() -match ‘N’)))
    {
    $continue = Read-Host ” You really want to modify fields for picture ”  $listItemsinFlickr.FieldValues.Title ” ? (Y/N)”
    }
switch ($continue.ToUpper())
{
    ‘Y’ {
            foreach ($item in $listItemsinFlickr)
                {
                $item[$field]=$newValue
                $item.Update()
                $clientContext.ExecuteQuery()
                cls
                write-host “The picture was been updated!”
                }
        }
    ‘N’ {
        cls
        write-host “Nothing has been changed!”
        }
}

You can download the script here

This entry was posted in Powershell, Sharepoint Online. Bookmark the permalink.