Saturday, October 8, 2016

SharePoint Online: Check if File Exists in Document Library using PowerShell CSOM

Requirement: Check if a file exists in SharePoint online document library using PowerShell CSOM.

sharepoint online check file exists powershell

PowerShell to check if a file exists in SharePoint online document library:
Lets check if file exists in SharePoint document library with PowerShell.
#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"
#Function to Check if file exists in given URL
Function Check-FileExists($SiteURL, $FileRelativeURL, $Credentials)
    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = $Credentials

    Try {
        #Try to get the File from URL
        $File = $Ctx.web.GetFileByServerRelativeUrl($FileRelativeURL)
        Return $True
    Catch {
        Return $False

#Set Variables for Site URL, List Name and Column Name
$SiteURL= ""

#Setup Credentials to connect
$Cred = Get-Credential
$Cred = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)

#Call the function to Check Column Exists in given list
$FileExists = Check-FileExists -SiteURL $SiteURL -FileRelativeURL $FileRelativeURL -Credentials $Cred

if($FileExists) {
    write-host "File Exists in the Given URL!" -f Green
    #Proceed with your script
 else {
    write-host "File Doesn't Exists in the given URL!" -f Red
Tags: check if file exists in sharepoint document library csom powershell, how to check file exists in sharepoint library, sharepoint check file exists document library, sharepoint csom check file exists, sharepoint powershell check if file exists in library

You might also like:
SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Document SharePoint Farm
Automatically generate SharePoint documentation.

Check out these SharePoint products:

No comments :

Post a Comment

Please Login and comment to get your questions answered!

You might also like:

Related Posts Plugin for WordPress, Blogger...