Downloading a file from library using PowerShell
		
	
							Hello, 
     I have a requirement to download files automatically from my scripts server, however I cannot find a good way to do it.   Most of the scripts available out there use SharePoint modules which only exist on servers running
 SharePoint, not on the box where I need to run the script.    I found a way to use .Net WebClient, but that downloads HTML rendered file, instead of full file.     Can someone help?
Kory
		
					September 9th, 2015 7:42am
			
	 
			
	
			
	
							I tried the Powershell / .Net approach, however the InfoPath xml file I am downloading comes down as an HTML file rather then XML.   Seems like it downloads the webpage vs the actual file.
Also cant use SharePoint approach since I don't have those modules installed.
		
					September 9th, 2015 8:11am
			
	 
			
	
							Add-Type Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
$siteUrl = Read-Host -Prompt "Enter web URL"
$username = Read-Host -Prompt "Enter your username"
$password = Read-Host -Prompt "Enter password" -AsSecureString
$source = "/filepath/sourcefilename.dat" #server relative URL here
$target = "C:/detinationfilename.dat" #URI of the file locally stored
$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteUrl)
$credentials = New-Object Microsoft.SharePoint.New-Object System.Net.NetworkCredential($username, $password)
$ctx.Credentials = $credentials
[Microsoft.SharePoint.Client.FileInformation] $fileInfo = [Microsoft.SharePoint.Client.File]::OpenBinaryDirect($ctx,$source);
[System.IO.FileStream] $writeStream = [System.IO.File]::Open($target,[System.IO.FileMode]::Create);
$fileInfo.Stream.CopyTo($writeStream);
$writeStream.Close();
you need to use the above script which is a combination of Powershell and SharrPoint Client Object Model.
Please check the following link to have and initial understanding of how it works:
http://www.sharepointnutsandbolts.com/2013/12/Using-CSOM-in-PowerShell-scripts-with-Office365.html
Next copy the relevant SharePoint dlls to your local server and put them in a folder.
Change the dll references in the above script accordingly.
Then run the script locally.
Hope this will help. I got the script from this link.
                        
                
                        
            
                    - 
                        Edited by
                            Avijit Sur
                        18 hours 47 minutes ago
                            edited		
September 9th, 2015 8:38am
			
	 
			
	
							can you verify this code?    I copied the files locally, added them, but the credential part errors out.		
		
					September 9th, 2015 9:53am
			
	 
			
	
							At first, please check if you have adequate access to the SharePoint file or not.
If you are using Office 365 instead of SharePoint on-premise  then change the following line:
$credentials = New-Object Microsoft.SharePoint.New-Object System.Net.NetworkCredential($username, $password)
to
$credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $password)
		September 9th, 2015 10:11am
			
	 
			
	
							I am using on prem SharePoint.     I get this error:
Exception calling "OpenBinaryDirect" with "2" argument(s): "Cannot invoke HTTP DAV request. There is a pending query."
when I execute the query, I don't think its mine query, so I get unauthorized.
		
					September 9th, 2015 10:16am
			
	 
			
	
							Please try this revised code:
Add-Type Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
$siteUrl = Read-Host -Prompt "Enter web URL"
$username = Read-Host -Prompt "Enter your username"
$password = Read-Host -Prompt "Enter password" -AsSecureString
$source = "/filepath/sourcefilename.dat" #server relative URL here
$target = "C:/detinationfilename.dat" #URI of the file locally stored
$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteUrl)
$credentials = New-Object System.Net.NetworkCredential($username, $password)
$ctx.Credentials = $credentials
[Microsoft.SharePoint.Client.FileInformation] $fileInfo = [Microsoft.SharePoint.Client.File]::OpenBinaryDirect($ctx,$source);
[System.IO.FileStream] $writeStream = [System.IO.File]::Open($target,[System.IO.FileMode]::Create);
$fileInfo.Stream.CopyTo($writeStream);
$writeStream.Close();
Just one line changed, from:
$credentials = New-Object Microsoft.SharePoint.New-Object System.Net.NetworkCredential($username, $password)
to 
$credentials = New-Object System.Net.NetworkCredential($username, $password)
		
September 9th, 2015 10:31am
			
	 
			
	
							Thanks.   This helped but now I get the issue of not finding the file I am trying to download.    Would you be able to help with command to get file listings?		
		
					September 9th, 2015 6:50pm
			
	 
			
	
							Quick question: If this is a local SharePoint farm, why not just use the \\sharepoint\webapplication\sitecollection\library\ virtual directory and use standard file copy processes?
                        
                
                        
            
September 9th, 2015 6:54pm
			
	 
			
	
							Honestly, because I choose this route and completely forgot you can map a drive to SharePoint directly.   Thank you for reminding me
                        
                
                        
            
                    - 
                        Marked as answer by
                            KoryGrin32434
                        8 hours 31 minutes ago		
					September 9th, 2015 7:02pm
			
	 
			
	
							Hi
I am very glad to here that the issue is solved.
Have a nice day.
Best Regards,
Lisa Chen 
		September 10th, 2015 12:58am