如何在 ASP.NET MVC 中将文件上传到 Sharepoint 库?

How can i upload a file into Sharepoint library, in ASP.NET MVC?

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

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

您可以使用 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");
            }