2

I have created a WorkFlow to create a document populating it with data from a list. I now need a powershell script that can copy the file in the document library to a file share on our server.

I am new to using powershell in SharePoint, is this possible with a script?

2

1 Answer 1

3

You can use this method.

$fileUrl = url for file on sharepoint

$destinationfolder = destinationFolder


    function ProcessURL {
        param($fileUrl,$destinationfolder)
        $pathToTry = "http://www.mysharepointsite.com/somesite"
        $site = New-Object -Type Microsoft.SharePoint.SPSite -ArgumentList $pathToTry
        $web = $site.OpenWeb()  
        $file = $web.GetFile($fileUrl)
        $destinationfolder = $destination + "/" + $folder.Url 
        if (!(Test-Path -path $destinationfolder))
        {
            $dest = New-Item $destinationfolder -type directory 
        }
        #Download file
        $binary = $file.OpenBinary()
        $stream = New-Object System.IO.FileStream($destinationfolder + "/" + $file.Name), Create
        $writer = New-Object System.IO.BinaryWriter($stream)
        $writer.write($binary)
        $writer.Close()
        #Delete file by deleting parent SPListItem
        $list.Items.DeleteItemById($file.Item.Id)
        $web.Close()
        $site.Close()
    }
6
  • Where are we passing Username and Password? or How do we pass Username and Password?
    – AXA240778
    Mar 8, 2018 at 17:52
  • @AXA240778 You shouldn't need a username/password. This should be ran on the SharePoint server while logged in with an account that has the necessary privileges and you shouldn't get prompted. If you are, you may need to check your privileges.
    – vapcguy
    Apr 2, 2019 at 17:59
  • 1
    Thanks @vapcguy
    – AXA240778
    Apr 4, 2019 at 16:36
  • @AXA240778 One caveat: I noticed if you just specify the URL directly & try to go to it directly without properly setting up your objects, I could see how you might get prompted. This happens on an HttpRequest, for example, if you don't include CredentialsCache.DefaultNetworkCredentials. SPSite & SPWeb must be appropriately defined, above. It should be: $pathToTry = "http://mysharepointsite.com/somesite" $site = New-Object -Type Microsoft.SharePoint.SPSite -ArgumentList $pathToTry $web = $site.OpenWeb() and then you need to close them at the end: $web.Close() $site.Close()
    – vapcguy
    Apr 4, 2019 at 17:57
  • 1
    Thansk much @vapcguy
    – AXA240778
    Apr 4, 2019 at 18:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.