Download file from SharePoint 2013 with authentication

3.7k Views Asked by At

I have a vb.net Tool, that Downloads a *. Zip file from a SharePoint Server. Till now it worked under SharePoint 2007.
Under SharePoint 2013 and I’m not abel to Download the File anymore. Under SharePoint 2013 the User has to Authenticate himself over this mask Authentication side in the Browser.

enter image description here

This was the Old Solution that worked on SharePoint 2007

Dim WebIntranet As New WebClient
Dim strURL as String = "https://example.com/content/P123456/Documents/File.zip"

WebIntranet.UseDefaultCredentials = True
'Download the File from the Server
 WebIntranet.DownloadFile(strURL,
     "D:\Temp\File.zip ")

For the new Solution it didn’t work and i tryed a few things:

First Approch:

Dim client As New WebClient 
Dim _UserAgend As String = "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.87 Safari/537.36"
Dim strURL as String = "https://example.com/content/P123456/Documents/File.zip"

client.Headers.Add(HttpRequestHeader.UserAgent, _UserAgend)

WebIntranet.DownloadFile(strURL,
     "D:\Temp\File.zip ")

Second Approch:

Dim securedPassword As SecureString = New SecureString
Dim _UserAgend As String = "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.87 Safari/537.36"
Dim username As String = "[email protected]"
Dim password As String = "123456"
client.Headers.Add(HttpRequestHeader.UserAgent, _UserAgend)


For Each c In password.ToCharArray()
    securedPassword.AppendChar(c)
Next
client.Headers.Add(HttpRequestHeader.UserAgent, _UserAgend)
client.Credentials = New NetworkCredential(username, securedPassword) WebIntranet.DownloadFile(strURL,
     "D:\Temp\File.zip ")

Both of the Approches didn’t work, they did not Donwloaded the File from the Sharepoint.
They only Downloaded the Authenticaton side and named it "File.zip"

Any help is welcomed


Raw Request from the Download

enter image description here

2

There are 2 best solutions below

5
Nostromo On

I don't know, if it works for your authentication method, but I could download a file like this (set reference to Microsoft.SharePoint.Client and Microsoft.SharePoint.Client.Runtime):

Imports System.Security
Imports Microsoft.SharePoint.Client

...
Dim securePwd As SecureString
Dim credentials As SharePointOnlineCredentials
Dim list As List
Dim listItem As ListItem
Dim listItemFile As File
Dim fileRef As String
Dim fileInfo As FileInformation
Dim fileName As String

...
credentials = New SharePointOnlineCredentials(userName, securePwd)

Using context As ClientContext = New ClientContext(BASE_URL)
    context.Credentials = credentials

    list = context.Web.Lists.GetByTitle(LIST_TITLE)
    listItem = list.GetItemById(ITEM_ID)
    listItemFile = listItem.File

    context.Load(list)
    context.Load(listItem, Function(x As ListItem) x.File)
    context.Load(listItemFile, Function(x As File) x.ServerRelativeUrl)
    context.ExecuteQuery()

    fileRef = listItem.File.ServerRelativeUrl
    fileInfo = File.OpenBinaryDirect(context, fileRef)
    fileName = IO.Path.Combine(destinationPath, listItem.File.Name)

    Using stream As IO.FileStream = System.IO.File.Create(fileName)
        fileInfo.Stream.CopyTo(stream)
    End Using
End Using

BASE_URL, LIST_TITLE and ITEM_ID I got from the URL when browsing the file-to-download in the browser.

I hope this helps.

2
Arulraj On

Try this solution. Don't forget to replace site details and credentials

    Using context As ClientContext = New ClientContext("https://example.com")
        context.Credentials = New NetworkCredential("[email protected]", "123456")
        Dim file As File = context.Web.GetFileByServerRelativeUrl("/content/P123456/Documents/File.zip")
        Dim filestrem As ClientResult(Of IO.Stream) = file.OpenBinaryStream()
        context.Load(file)
        context.ExecuteQuery()
        Using stream As IO.MemoryStream = New IO.MemoryStream()
            filestrem.Value.CopyTo(stream)
            IO.File.WriteAllBytes("D:\Temp\" + file.Name, stream.ToArray())
        End Using
    End Using