2013-10-01 101 views
2

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

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) 
      { 
      } 
     } 

我收到错误“Object Moved”。 URL通过浏览器加载WSDL。如果有更好的方式在Office365上传并从SharePoint获取文件,我也会这么做。谢谢。

回答

7

由于ASMX web服务已被弃用,您应该检查sharepoint的“新”rest服务。 ON MSDN you find information about it

或者您可以使用客户端对象模型,这将是我最喜欢的方式。下面的示例演示基本用法,连接到SharePoint在线查看以下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(); 
} 
+0

我喜欢这个解决方案的外观,但安全性如何处理?谢谢。 – Cyberdrew

+0

查看我的答案的第二个链接,有一个完整的示例应用程序,尤其是类Tokenhelper应该是您感兴趣的 – roqz

+0

我在哪里可以获得我需要的Microsoft.SharePoint.dll解决方案。我没有安装SharePoint,无法在我的Windows 7计算机上安装WSS以获取它。 – Cyberdrew

6

使用上述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("[email protected]", 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("[email protected]", 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); 
       } 
      } 
     } 
    } 
} 
相关问题