gpt4 book ai didi

c# - Office 365 Sharepoint 上传文件到文档库

转载 作者:行者123 更新时间:2023-11-30 18:58:51 25 4
gpt4 key购买 nike

我正在尝试使用以下代码通过 Web 服务将文件添加到我在 Sharepoint Office365 上的文档库。

public void SaveFileToSharePoint(string fileName)
{
try
{
var copyService = new Copy { Url = "https://mydomain.com/_vti_bin/copy.asmx", Credentials = new NetworkCredential("username", "password", "domain") };

var destURL = "https://mydomain.com/Shared%20Documents/" + Path.GetFileName(fileName);

string[] destinationUrl = { destURL };

CopyResult[] cResultArray;

var fFiledInfo = new FieldInformation { DisplayName = "Description", Type = FieldType.Text, Value = Path.GetFileName(fileName) };

FieldInformation[] fFiledInfoArray = {fFiledInfo};

var copyresult = copyService.CopyIntoItems(destURL, destinationUrl, fFiledInfoArray, File.ReadAllBytes(fileName), out cResultArray);
var b = copyresult;
}
catch (Exception ex)
{
}
}

我收到错误消息“对象已移动”。尽管 URL 在浏览器中加载了 WSDL。如果有更好的方法从 SharePoint on Office365 在线上传和获取文件,我也会接受。谢谢。

最佳答案

由于 ASMX 网络服务已弃用,您应该查看 Sharepoint 的"new"其余服务。 ON MSDN you find information about it

或者您可以使用客户端对象模型,这是我最喜欢的方式。以下示例显示了基本用法,要连接到 SharePoint online,请查看以下 link

using(ClientContext context = new ClientContext("http://yourURL"))
{
Web web = context.Web;
FileCreationInformation newFile = new FileCreationInformation();
newFile.Content = System.IO.File.ReadAllBytes(@"C:\myfile.txt");
newFile.Url = "file uploaded via client OM.txt";
List docs = web.Lists.GetByTitle("Documents");
Microsoft.SharePoint.Client.File uploadFile = docs.RootFolder.Files.Add(newFile);
context.ExecuteQuery();
}

关于c# - Office 365 Sharepoint 上传文件到文档库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19125529/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com