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

标签 c# web-services sharepoint-2013

我正在尝试使用以下代码通过 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/

相关文章:

java - Android - 使用 ksoap2 使用 wsdl webservice 进行登录身份验证

php - PHP 中的 Sharepoint GetListItemChangesSinceToken CURL 请求

rest - SharePoint 2013 REST 日期时间字段问题

javascript - 如何使用 JavaScript/JQuery 在面向公众的 ASP.NET 网站上使用 SP2013 REST API?

c# - 如何在 dll 中创建可以通过运行它的应用程序更改的条件

c# - LINQ:根据属性值从 XML 中删除元素?

c# - WCF 数据服务,不要公开所有列

c# regex - 组和嵌套组

c# - 一个 WCF 服务的两个 throttle

c# - 将 C++ App Server 与 C# Web 服务(客户端+服务器)集成的好方法