如何解决如何在ASP.NET MVC中将文件上传到Sharepoint库?
我已经阅读了很多与此有关的问题,但我仍然感到困惑。 我创建了一个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");
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。