Upload File to SharePoint using Web Services and PowerShell

Requirement: For a file upload issue troubleshooting, had to call SharePoint web services to upload a file into SharePoint document library.

Upload File to SharePoint Library using Web Services – PowerShell

Here is my PowerShell script to upload file to SharePoint document library using web services:

$SourceFile = "D:\Reports\MonthlyRpt-Jan14.csv"
$DestinationPath ="https://operations.crescent.com/Reports/MonthlyRpt-Jan14.csv" 

$WebServiceURL = "https://operations.crescent.com/_vti_bin/copy.asmx?WSDL" 
$CopyWebService = new-WebServiceProxy -Uri $WebServiceURL  -UseDefaultCredential

#For custom Credentials, use:
#$Cred = New-Object System.Management.Automation.PSCredential -ArgumentList @($UserName,$SecurePasssword)
#$UserName = "Domain\UserName"
#$Password = "PASSWORD"
#$SecurePasssword = ConvertTo-SecureString -String $Password -AsPlainText -Force
#$CopyWebService= new-WebServiceProxy -Uri $WebServiceURL -Credential $cred 

#Get the File from Disk
$FileData = [System.IO.File]::ReadAllBytes($SourceFile)    
#Get Filename
$FileName = [System.IO.Path]::GetFileName($SourceFile).ToString()

$Metadata = @() 

$results = $null
#Upload file to SharePoint library using Web Service
$ret= $CopyWebService.CopyIntoItems($FileName, $DestinationPath, $Metadata, $FileData, [ref]$results)

Write-host "Upload Status:" $results[0].ErrorCode

Salaudeen Rajack

Salaudeen Rajack - Information Technology Expert with Two-decades of hands-on experience, specializing in SharePoint, PowerShell, Microsoft 365, and related products. He has held various positions including SharePoint Architect, Administrator, Developer and consultant, has helped many organizations to implement and optimize SharePoint solutions. Known for his deep technical expertise, He's passionate about sharing the knowledge and insights to help others, through the real-world articles!

2 thoughts on “Upload File to SharePoint using Web Services and PowerShell

  • anything for Sharepoint sites that are https and use ADFS

    Reply
  • Great!!! It worked.Thanks a lot.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *