下载 SharePoint 文件夹中的最新文件

Download Latest File in a SharePoint Folder

我当前的代码从文件的绝对 URL 下载 SharePoint 文件并写入本地。

我想将其更改为使用文件夹 URL,并根据某些过滤器将文件下载到文件夹中。

可以做到吗?

下面是我当前的代码片段:

string fullFilePath = DownloadSPFile("http://MySharePointSite.com/sites/Collection1/Folder1/File1.docx/");

public static string DownloadSPFile(string urlPath)
{
    string serverTempdocPath = "";

    try
    {
        var request = (HttpWebRequest)WebRequest.Create(urlPath);
        var credentials = new NetworkCredential("username", "password", "domain");

        request.Credentials = credentials;
        request.Timeout = 20000;
        request.AllowWriteStreamBuffering = false;

        using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
        {
            using (Stream stream = response.GetResponseStream())
            {
                serverTempdocPath = Path.Combine(AppConfig.EmailSaveFilePath + "_DOWNLOADED.eml");

                using (FileStream fs = new FileStream(serverTempdocPath, FileMode.Create))
                {
                    byte[] read = new byte[256];
                    int count = stream.Read(read, 0, read.Length);
                    while (count > 0)
                    {
                        fs.Write(read, 0, count);
                        count = stream.Read(read, 0, read.Length);
                    }
                }
            }
        }
    }
    catch (Exception ex)
    {
        AppLogger.LogError(ex, "");
        throw ex;
    }
    return serverTempDocPath;
}

您尝试格式化您的 urlPath 了吗?

喜欢: string urlPath = "path/to/folder/"; string fileFilter = "Cat.png"; string finalPath= String.format("{0}{1}", urlPath, fileFilter); (或类似的东西)

(希望我理解你的问题并且我能够帮助你^^)

如果您的 sharepoint 站点启用了 sharepoint rest api,您可以很容易地获得详细信息。

获取文件列表

url: http://site url/_api/web/GetFolderByServerRelativeUrl('/Folder Name')/Files
method: GET
headers:
    Authorization: "Bearer " + accessToken
    accept: "application/json;odata=verbose" or "application/atom+xml"

并通过查询

"?filter=$top=1&$orderby=Created desc"

More information