如何从SharePoint中的文档下载版本历史记录中的文档

时间:2018-02-17 12:07:43

标签: sharepoint dynamics-crm

我想从"版本历史记录"下载所有文件。 SharePoint中的文件。

我需要通过接口或C#代码来获取文档。

我也尝试过以下API网址,但收到错误。

https://XXXX.sharepoint.com/_api/Web/GetFileByServerRelativeUrl('fileURL')/Versions

5 个答案:

答案 0 :(得分:0)

Per MSDN,我猜您无法一次性下载所有版本。您必须在versionid括号内附加versions(<version id>)以获取特定项目。

http://<site url>/_api/web/getfilebyserverrelativeurl('/<folder name>/<file name>')/versions(<version id>)

Make sure如果您想要次要版本,则可以正确设置预期的网址。

答案 1 :(得分:0)

确保您在2017年9月之后使用最新版本的SharePoint Online CSOM或至少使用版本。

您可以从Nuget获取最新的CSOM版本并将其添加到您的项目中。

完成后,您可以使用下面提到的代码下载文件版本。根据您的环境,信誉和文件名修改它:

var siteUrl = "https://XXXX.sharepoint.com";
var userName = "user.name@tenantname.onmicrosoft.com";
var password = "password";

using (ClientContext context = new ClientContext(siteUrl))
{
    SecureString securePassword = new SecureString();
    foreach (char c in password.ToCharArray())
    {
        securePassword.AppendChar(c);
    }

    context.AuthenticationMode = ClientAuthenticationMode.Default;
    context.Credentials = new SharePointOnlineCredentials(userName, securePassword);

    var file = context.Web.GetFileByServerRelativeUrl(siteUrl + "/Documents/test.docx");
    var fileVersions = file.Versions; 
    context.Load(file); 
    context.Load(fileVersions);
    context.ExecuteQuery();

    int index = 0;
    foreach (var version in fileVersions)
    {    
       var str = version.OpenBinaryStream();
       context.ExecuteQuery();

       string filename = string.Format("c:\\Downloads\\test-{0}.docx", index);
       using (var fileStream = new FileStream(filename, FileMode.OpenOrCreate))
       {
         str.Value.CopyTo(fileStream);
       }
       index++;     
    }

}

参考 - CSOM September 2017 update

答案 2 :(得分:0)

以下代码供您参考。

string _SharePointSiteURL = @"https://lz.sharepoint.com/sites/lz";

var _SharePointSiteUser = "lz@lz.onmicrosoft.com";
var password = "Password";

var localPath = @"c:\test\";

var filePath="Shared Documents/ABC/Test.pdf";

var securePassword = new SecureString();

foreach (char c in password)
{
    securePassword.AppendChar(c);
}

using (var clientContext = new ClientContext(_SharePointSiteURL))
{
    var onlineCredentials = new SharePointOnlineCredentials(_SharePointSiteUser, securePassword);
    clientContext.RequestTimeout = 10000000;
    clientContext.Credentials = onlineCredentials;
    Web web = clientContext.Web;
    clientContext.Load(web, website => website.ServerRelativeUrl, website => website.Url);
    clientContext.ExecuteQuery();

    var spFile = clientContext.Web.GetFileByServerRelativeUrl((web.ServerRelativeUrl.EndsWith("/") ? web.ServerRelativeUrl : web.ServerRelativeUrl + "/") + filePath);
    clientContext.Load(spFile);
    FileVersionCollection versions = spFile.Versions;
    clientContext.Load(versions);
    var oldVersions = clientContext.LoadQuery(versions.Where(v => v != null));
    clientContext.ExecuteQuery();
    if (oldVersions != null)
    {
        foreach (Microsoft.SharePoint.Client.FileVersion _version in oldVersions)
        {

            if (!Directory.Exists(localPath))
            {
                Directory.CreateDirectory(localPath);
            }

            using (var wc = new System.Net.WebClient())
            {                       
                wc.Credentials = onlineCredentials;
                wc.Headers.Add("X-FORMS_BASED_AUTH_ACCEPTED", "f");
                wc.Headers["User-Agent"] = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; MDDC)";
                wc.DownloadFile(web.Url + "/" + _version.Url, localPath+"Test.pdf");
            }   
        }
    }
}

答案 3 :(得分:0)

此答案适用于可能正在寻找Powershell CSOM脚本的其他人,该脚本将下载当前文档以及版本历史记录。

毫无疑问,我是Powershell的新手,我一直在努力寻找一个脚本来从SharePoint下载所有当前文件及其版本。由于我尝试过的各种脚本都提出了(404)Not Found或(403)Forbidden,因此访问版本证明是一个挑战。我能够组合一个CSOM函数,该函数从另一个我发现的脚本中调用,该脚本从指定的库/列表中下载所有当前版本。由于原始脚本不是我的,因此我无法发布整个解决方案。你可以在这里找到它: http://www.sharepointdiary.com/2017/03/sharepoint-online-download-all-files-from-document-library-using-powershell.html#ixzz5YpC0YB00

在上述站点的Download-AllFilesFromLibrary函数中,我添加了GetFileVersions作为Foreach($ FilesColl中的$ File)循环的最后一条语句。

正如我所说,我是Powershell的新手,因此我确定可以对该功能进行一些改进,但是它可以工作。

Function GetFileVersions
{
#Get versions of specific document from library
$fileVersions = $file.Versions;
$ctx.Load($fileVersions);
$ctx.ExecuteQuery();
$vcount = $fileversions.count;

If ($vcount -ne “0” ) { 
# Download all versions of specific file as individual docs
$fname,$fext = $file.name.split('.');   # Separate filename and extension to different variables
 foreach ($fversion in $fileVersions) {
    $str = $fversion.OpenBinaryStream(); 
    $ctx.ExecuteQuery(); 
    $dtime = $fversion.created.ToString(“u”) -replace “:”,””;   # Convert date/time created to formatted string and replace semicolons in the time with blank
    $filename =$localfolder + “\” + $fname + “~” + $dtime + “.” + $fext ; # Compose the filename as the target folder, file name + ~+ datetime created.fileextension (i.e. book~2019-02-12 153659Z.xlsx)
    $fs = New-Object IO.FileStream $filename ,'Create','Write','Read'; 
    $str.Value.CopyTo($fs); 

    #To use the Write-Log function below, see the script at https://gallery.technet.microsoft.com/scriptcenter/Write-Log-PowerShell-999c32d0

   #$vmessage = $filename + “*” + $fversion.versionlabel;  # Message to write to the log file 
  #Write-Log $vmessage;

    $fs.Close();
   }
        if ($fs.isdisposable) {$fs.Dispose();}  
}
}

答案 4 :(得分:0)

我为自己开发的迁移工具提供了一个可行的解决方案。必备条件是将文件和较早版本从SharePoint环境迁移到另一个环境。

要下载旧版本,请执行以下操作:

public byte[] DownloadFile(string url, bool isHistory = false)
{
    bool isSharepointOnline = (SPCurrentContext.Credentials is SharePointOnlineCredentials);
    HttpResponseMessage response = null;
    byte[] buffer = null;

    if (!isHistory)
    {
        using (FileInformation fileInfo = File.OpenBinaryDirect(SPCurrentContext, url))
        using (IO.MemoryStream memory = new IO.MemoryStream())
        {
            fileInfo.Stream.CopyTo(memory);
            buffer = memory.ToArray();
        }
    }
    else if (isSharepointOnline)
    {
        using (WebClient httpClient = new WebClient())
        {
            httpClient.Credentials = SPCurrentContext.Credentials;
            httpClient.Headers.Add("X-FORMS_BASED_AUTH_ACCEPTED", "f");
            httpClient.Headers["User-Agent"] = this.NONISV; // This is the TRICK!!! 

            buffer = httpClient.DownloadData(string.Concat(SPCurrentContext.Url, url));
        }
    }
    else
    {
        using (HttpClientHandler httpHandler = new HttpClientHandler() { Credentials = SPCurrentContext.Credentials })
        using (HttpClient client = new HttpClient(httpHandler))
        {
            response = client.GetAsync(string.Concat(SPCurrentContext.Url, url)).Result;

            response.EnsureSuccessStatusCode();

            response.Content.LoadIntoBufferAsync().Wait();

            buffer = response.Content.ReadAsByteArrayAsync().Result;
        }
    }

    return buffer;
}

说明:

由于您具有文件URL(File.ServerRelativeUrl和FileVersion.Url应该可以帮助您完成此步骤),因此该方法可以很好地下载文件的当前版本或较旧版本。然后,当您尝试像在“ SP On-Premises”中那样从“ SharePoint Online”下载文件版本时,将收到403 HTTP错误。

当您放置一些用户代理和X-FORMS_BASED_AUTH_ACCEPTED标头时,该解决方案开始起作用。但是...当然,由于429 HTTP错误,您将收到超时错误。 要绕过SharePoint Throttling,您必须将程序注册为SharePoint加载项。

为此,请转到:

  

{您的共享点在线根URL} / _ layouts / 15 / AppRegNew.aspx

并生成您的加载项注册(有关说明和更多信息,请参见this linkthis link

获取后,您可以使用NONISV | {您的公司名称} | {您的加载项名称} /1.0作为用户代理HTTP标头。祝你好运!