none
how to add link to download entire folder in one go RRS feed

  • Question

  • Hello,

    I followed this BLOG and was able to add a download link for each item in a library. However my requirement is that i want the link to download all the contents of a folder in one click. 

    Can anyone please help me achieve that ? 

    Here is the code i'm currently using :

    <script type="text/javascript">
    function runAfterEverythingElse(){
    var elementArray = document.getElementsByClassName("ms-vb-lastCell");
    var idArray = document.getElementsByClassName("ms-listlink");
    for(var i = 0; i < elementArray.length; i++)
    {
        elementArray[i].innerHTML = '<a unselectable="on" href="javascript:;" onclick="window.open(\''+ idArray[i]['href'] + '\')" class="ms-cui-ctl-large " aria-describedby="Ribbon.Documents.Copies.Download_ToolTip" mscui:controltype="Button" role="button" id="Ribbon.Documents.Copies.Download-Large"><span unselectable="on" class="ms-cui-ctl-largeIconContainer"><span unselectable="on" class=" ms-cui-img-32by32 ms-cui-img-cont-float"><img unselectable="on" alt="" src="/_layouts/15/1033/images/formatmap32x32.png?rev=31" style="top: -409px; left: -35px;"></span></span><span unselectable="on" class="ms-cui-ctl-largelabel">Download a<br>Copy</span></a>';
    }  
    }
    _spBodyOnLoadFunctionNames.push("runAfterEverythingElse");  
    </script>

    Thank You.

    Wednesday, May 16, 2018 6:32 AM

Answers

  • Hi John,

    For a folder download link, it is not to achieve like the function above, as we can not get the files in the folders directly in the code.

    A workaround is to download the files from folder using PowerShell script directly:

    ######################## Start Variables ########################
    ######################## Varun's Script######################
    $destination = "C:\\Test"
    $webUrl = "http://sp/sites/dev/"
    $listUrl = "http://sp/sites/dev/Shared Documents"
    ##############################################################
    
    $web = Get-SPWeb -Identity $webUrl
    $list = $web.GetList($listUrl)
    
    function ProcessFolder {
        param($folderUrl)
        $folder = $web.GetFolder($folderUrl)
        foreach ($file in $folder.Files) {
            #Ensure destination directory
            $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()
            }
    }
    
    
    ProcessFolder($list.RootFolder.Url)

    Thanks

    Best Regards


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnsf@microsoft.com.


    Click here to learn more. Visit the dedicated forum to share, explore and talk to experts about Microsoft Teams.

    • Marked as answer by John.P.Smith Thursday, May 24, 2018 7:24 AM
    Tuesday, May 22, 2018 10:14 AM

All replies

  • Hi John,

    For a folder download link, it is not to achieve like the function above, as we can not get the files in the folders directly in the code.

    A workaround is to download the files from folder using PowerShell script directly:

    ######################## Start Variables ########################
    ######################## Varun's Script######################
    $destination = "C:\\Test"
    $webUrl = "http://sp/sites/dev/"
    $listUrl = "http://sp/sites/dev/Shared Documents"
    ##############################################################
    
    $web = Get-SPWeb -Identity $webUrl
    $list = $web.GetList($listUrl)
    
    function ProcessFolder {
        param($folderUrl)
        $folder = $web.GetFolder($folderUrl)
        foreach ($file in $folder.Files) {
            #Ensure destination directory
            $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()
            }
    }
    
    
    ProcessFolder($list.RootFolder.Url)

    Thanks

    Best Regards


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnsf@microsoft.com.


    Click here to learn more. Visit the dedicated forum to share, explore and talk to experts about Microsoft Teams.

    • Marked as answer by John.P.Smith Thursday, May 24, 2018 7:24 AM
    Tuesday, May 22, 2018 10:14 AM
  • Thanks a lot! this helps.
    Thursday, May 24, 2018 7:24 AM