通过身份验证从SharePoint 2013下载文件

时间:2018-06-29 10:27:02

标签: .net vb.net sharepoint-2013 sharepoint-2007

我有一个vb.net工具,可以下载 *。来自SharePoint Server的Zip 文件。到目前为止,它在 SharePoint 2007 下可以正常工作。
SharePoint 2013 下,我不再需要下载文件。在SharePoint 2013下,用户必须通过浏览器中的此掩码“身份验证”端进行身份验证。

enter image description here

这是适用于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 ")

对于新解决方案,它不起作用,我尝试了一些方法:

第一次申请:

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 ")

第二种方法:

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 = "TestUser@Test.com"
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 ")

两种批准均无效,并且没有从Sharepoint下载文件。
他们只下载了Authenticaton端并将其命名为“ File.zip”

欢迎任何帮助


下载中的原始请求

enter image description here

2 个答案:

答案 0 :(得分:0)

我不知道它是否适用于您的身份验证方法,但是我可以下载这样的文件(设置对 Microsoft.SharePoint.Client 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_URLLIST_TITLEITEM_ID是我在浏览器中浏览要下载的文件时从URL获得的。

我希望这会有所帮助。

答案 1 :(得分:0)

尝试此解决方案。不要忘记替换站点详细信息和凭据

    Using context As ClientContext = New ClientContext("https://example.com")
        context.Credentials = New NetworkCredential("TestUser@Test.com", "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