【问题标题】:How can i upload a file into Sharepoint library, in ASP.NET MVC?如何在 ASP.NET MVC 中将文件上传到 Sharepoint 库?
【发布时间】:2020-12-22 15:17:29
【问题描述】:

我已经阅读了很多关于此的问题,但我仍然感到困惑.. 我做了一个powerapps项目,我将一个文件上传到sharepoint,上传会创建一个文件夹,其中包含一个文本字段,该字段将在该表单中填写。如果名称已经是一个文件夹,他会在字段内部上传只会上传到图书馆!

现在我想要做的是完全相同的事情,但是使用 C# ASP.NET,我想创建一个表单,我发送 2 个值、一个用于创建文件夹的文本和要上传到该文件夹​​的文件!有人可以帮忙吗? :) 谢谢你的帮助 ! :)

【问题讨论】:

    标签: c# asp.net sharepoint file-upload


    【解决方案1】:

    您可以使用 SharePoint CSOM 创建文件夹:

                string userName = "user@Tenant.onmicrosoft.com";
                string Password = "*********";
                var securePassword = new SecureString();
                foreach (char c in Password)
                {
                    securePassword.AppendChar(c);
                }
                using (var ctx = new ClientContext("https://tenant.sharepoint.com/sites/sitename"))
                {
                    ctx.Credentials = new Microsoft.SharePoint.Client.SharePointOnlineCredentials(userName, securePassword);
                    Web web = ctx.Web;
                    ctx.Load(web);
                    ctx.ExecuteQuery();
                    List byTitle = ctx.Web.Lists.GetByTitle("LibraryName");
    
                    // New object of "ListItemCreationInformation" class
                    ListItemCreationInformation listItemCreationInformation = new ListItemCreationInformation();
    
                    // Below are options.
                    // (1) File - This will create a file in the list or document library
                    // (2) Folder - This will create a foder in list(if folder creation is enabled) or documnt library
                    listItemCreationInformation.UnderlyingObjectType = FileSystemObjectType.Folder;
    
                    // This will et the internal name/path of the file/folder
                    listItemCreationInformation.LeafName = "NewFolderFromCSOM";
    
                    ListItem listItem = byTitle.AddItem(listItemCreationInformation);
    
                    // Set folder Name
                    listItem["Title"] = "NewFolderFromCSOM";
    
                    listItem.Update();
                    ctx.ExecuteQuery();
                }
    

    Create Folder in SharePoint using CSOM

    然后将文件上传到新创建的文件夹:

                string userName = "user@Tenant.onmicrosoft.com";
                string Password = "*******";
                var securePassword = new SecureString();
                foreach (char c in Password)
                {
                    securePassword.AppendChar(c);
                }
                using (var ctx = new ClientContext("https://tenant.sharepoint.com/"))
                {
                    ctx.Credentials = new Microsoft.SharePoint.Client.SharePointOnlineCredentials(userName, securePassword);
                    Web web = ctx.Web;
                    ctx.Load(web);
                    ctx.ExecuteQuery();
                    FileCreationInformation newFile = new FileCreationInformation();
                    newFile.Content = System.IO.File.ReadAllBytes("D:\\document.pdf");
                    newFile.Url = @"document.pdf";
                    List byTitle = ctx.Web.Lists.GetByTitle("Documents");
                    Folder folder = byTitle.RootFolder.Folders.GetByUrl("NewFolderFromCSOM");
                    ctx.Load(folder);
                    ctx.ExecuteQuery();
                    Microsoft.SharePoint.Client.File uploadFile = folder.Files.Add(newFile);
                    uploadFile.CheckIn("checkin", CheckinType.MajorCheckIn);
                    ctx.Load(byTitle);
                    ctx.Load(uploadFile);
                    ctx.ExecuteQuery();
                    Console.WriteLine("done");
                }
    

    【讨论】:

    • 你是个传奇!谢谢 ! :D
    • 嗨,Jerry,代码运行良好,但现在在不同的项目中无法运行。标准 .net 核心中不允许 SharepointOnlineCredentials...在这种情况下,解决方法是什么?
    • 嗨,Ricardo,在你的另一个帖子中回答了这个问题:stackoverflow.com/questions/64026245/… 请检查。
    • 嗨,杰瑞,我一直在尝试使用我的输入而不是硬编码的 URL 之类的东西来使这段代码动态化,我一直在努力解决的一件事是重复的文件夹。它正在尝试创建重复的文件夹女巫会导致错误,我想做的是适应你所拥有的,但我没有看到我能做些什么来实现这一点!顺便说一句,我没有按照您的方式使用连接,而是关注ypcode.io/posts/2020/07/use-csom-aspnetcore,以便我可以在.Net Core 上执行此操作。感谢您的帮助,我真的很感激! :)
    猜你喜欢
    • 2010-09-21
    • 2010-10-02
    • 2021-08-29
    • 1970-01-01
    • 2010-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-08
    相关资源
    最近更新 更多