【问题标题】:Office 365 Sharepoint Upload Files to Documents LibraryOffice 365 Sharepoint 将文件上传到文档库
【发布时间】:2013-10-08 03:56:46
【问题描述】:

我正在尝试使用以下代码通过 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。如果有更好的方法可以在线从 Office365 上的 SharePoint 上传和获取文件,我也会接受。谢谢。

【问题讨论】:

    标签: c# web-services sharepoint-2013


    【解决方案1】:

    由于不推荐使用 ASMX 网络服务,您应该查看 sharepoint 的“新”休息服务。 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();
    }
    

    【讨论】:

    • 我喜欢这个解决方案的外观,但是如何处理安全性?谢谢。
    • 查看我答案的第二个链接,有一个完整的示例应用程序,尤其是您应该感兴趣的 Tokenhelper 类
    • 我在哪里可以得到 Microsoft.SharePoint.dll,我需要这个解决方案。我没有安装 SharePoint,也无法在我的 Windows 7 机器上安装 WSS。
    • 谢谢,我会试试这个解决方案,谢谢你的帮助
    【解决方案2】:

    使用上面的 roqz 建议,这是我想出的将文件放在 SharePoint 2013 Office 365 文档库中并按名称检索它们的最终解决方案:

    public void SaveFileToSharePoint(string fileName)
    {
        using (var context = new ClientContext("https://mydomain.com/"))
        {
            var passWord = new SecureString();
            foreach (var c in "MyPassword") passWord.AppendChar(c);
            context.Credentials = new SharePointOnlineCredentials("me@mydomain.com", passWord);
            var web = context.Web;
            var newFile = new FileCreationInformation {Content = File.ReadAllBytes(fileName), Url = Path.GetFileName(fileName)};
            var docs = web.Lists.GetByTitle("Documents");
            docs.RootFolder.Folders.GetByUrl("Test").Files.Add(newFile);
            context.ExecuteQuery();
        }
    }
    
    public void GetFileFromSharePoint(string fileName, string savePath)
    {
        using (var context = new ClientContext("https://mydomain.com/"))
        {
            var passWord = new SecureString();
            foreach (var c in "MyPassword") passWord.AppendChar(c);
            context.Credentials = new SharePointOnlineCredentials("me@mydomain.com", passWord);
            var web = context.Web;
            var myFile = web.Lists.GetByTitle("Documents").RootFolder.Folders.GetByUrl("Test").Files.GetByUrl(fileName);
            context.Load(myFile);
            context.ExecuteQuery();
    
            using (var ffl = Microsoft.SharePoint.Client.File.OpenBinaryDirect(context, myFile.ServerRelativeUrl))
            {
                using (var destFile = File.OpenWrite(savePath + fileName))
                {
                    var buffer = new byte[8*1024];
                    int len;
                    while ((len = ffl.Stream.Read(buffer, 0, buffer.Length)) > 0)
                    {
                        destFile.Write(buffer, 0, len);
                    }
                }
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-07-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-01
      • 2013-12-12
      • 2010-10-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多