locked
Downloading Infopath XML file using PowerShell RRS feed

  • Question

  • Hello,

         I have a requirement to download XML 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

    Wednesday, September 9, 2015 11:56 AM

Answers

  • 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();
    Hope this will help. I got the script from this link.



    Avijit Sur

    • Proposed as answer by Sara Fan Monday, September 21, 2015 1:47 AM
    • Marked as answer by Sara Fan Wednesday, October 7, 2015 7:38 AM
    Wednesday, September 9, 2015 12:59 PM

All replies

  • 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();
    Hope this will help. I got the script from this link.



    Avijit Sur

    • Proposed as answer by Sara Fan Monday, September 21, 2015 1:47 AM
    • Marked as answer by Sara Fan Wednesday, October 7, 2015 7:38 AM
    Wednesday, September 9, 2015 12:59 PM
  • Hi,

    If these replies are helpful to you, you could mark answer to close the case. If you have any question about this issue, please feel free to reply.

    Best regards,

    Sara Fan

    TechNet Community Support
    Please remember to mark the replies as answers if they help, and unmark the answers if they provide no help. If you have feedback for TechNet Support, contact tnmff@microsoft.com.

    Monday, September 21, 2015 1:47 AM