c# - 使用 C#.net 从 Sharepoint Online 下载文件?

标签 c# c#-4.0 sharepoint sharepoint-online

我想将文件从 SharePoint Online 下载到本地,但我正在努力处理代码示例。我在谷歌上搜索过,但没有得到任何有值(value)的信息。

最佳答案

尝试下面的代码片段,使用 SharePoint Online CSOM 将文件从库下载到本地:

using Microsoft.SharePoint.Client;
using System.IO;
using System.Linq;
using System.Security;

class Program
{
    static void Main(string[] args)
    {
        using (ClientContext ctx = new ClientContext(
            "https://tenantname.sharepoint.com/sites/sitename/"))
        {
            string password = "********";
            string account = "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="41343224332f202c240135242f202f352f202c246f2e2f2c2822332e322e27356f222e2c" rel="noreferrer noopener nofollow">[email protected]</a>";
            var secret = new SecureString();
            foreach (char c in password)
            {
                secret.AppendChar(c);
            }
            ctx.Credentials = new SharePointOnlineCredentials(account, secret);
            ctx.Load(ctx.Web);
            ctx.ExecuteQuery();
        
            List list = ctx.Web.Lists.GetByTitle("libraryTitle");

            FileCollection files = list.RootFolder.Folders
                .GetByUrl("/sites/sitename/shared documents/foldername").Files;

            ctx.Load(files);
            ctx.ExecuteQuery();

            foreach (Microsoft.SharePoint.Client.File file in files)
            {
                FileInformation fileinfo = Microsoft.SharePoint.Client.File
                    .OpenBinaryDirect(ctx, file.ServerRelativeUrl);

                ctx.ExecuteQuery();

                using (FileStream filestream =
                    new FileStream("C:" + "\\" + file.Name, FileMode.Create))
                {
                    fileinfo.Stream.CopyTo(filestream);
                }
            }
        };
    }
}

关于c# - 使用 C#.net 从 Sharepoint Online 下载文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58034425/

相关文章:

c# - 使用验证覆盖列表中的 .Add 方法

c# - OData 集合参数绑定(bind)......它们真的有效吗?

c#-4.0 - 从 ASP.NET MVC 3 升级到 MVC 4,参数被替换为路由

.net - COM 互操作 : indexed property signature issues

sharepoint - CAML 仅返回 10 个结果

sharepoint list Vs library-developer 的观点

c# - 我可以设置一些东西来让一个类在卸载时做一些事情吗

c# - 如果 Controller 与 View 强绑定(bind),为什么 Controller 需要模型参数?

asp.net - 使用 "!important"覆盖 SharePoint 2010 母版页的 css 属性

c# - global.asax 中的 Application_Start() 事件