Office 365 SharePoint 将文件上载到文档库

本文关键字:文档 上载 文件 SharePoint Office | 更新日期: 2023-09-27 18:33:27

我正在尝试使用以下代码通过 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在线上传和获取文件,我也会接受。谢谢。

Office 365 SharePoint 将文件上载到文档库

由于ASMX Web服务已被弃用,您应该查看SharePoint 的"新"休息服务。在 MSDN 上,您可以找到有关它的信息

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

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();
}

使用上面的 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);
                }
            }
        }
    }
}