简体   繁体   English

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

[英]Office 365 Sharepoint Upload Files to Documents Library

I am trying to use the following code to add files to my document library on Sharepoint Office365 using web services. 我正在尝试使用以下代码使用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)
            {
            }
        }

I receive the error "Object Moved". 我收到错误“Object Moved”。 The URL loads the WSDL in the browser though. URL虽然在浏览器中加载了WSDL。 If there is a better way to upload and get files from SharePoint on Office365 online I would entertain that as well. 如果有更好的方法从Office 365上的SharePoint上传和获取文件,我也会对此感到满意。 Thanks. 谢谢。

as the ASMX webservices are deprecated you should check out the "new" rest services of sharepoint. 由于不推荐使用ASMX Web服务,您应该查看sharepoint的“新”休息服务。 ON MSDN you find information about it 在MSDN上,您可以找到有关它的信息

Or you can use the Client object model which would be my favorite way. 或者您可以使用客户端对象模型,这将是我最喜欢的方式。 The following example shows basic usage, to connect to SharePoint online check out the following link 以下示例显示了基本用法,要在线连接到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();
}

Using roqz suggestions above, here is the ultimate solution I came up with to place files in the SharePoint 2013 Office 365 document library and to retrieve them by name: 使用上面的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);
                }
            }
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM