簡體   English   中英

Office 365 Sharepoint將文件上載到文檔庫

[英]Office 365 Sharepoint Upload Files to Documents Library

我正在嘗試使用以下代碼使用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)
            {
            }
        }

我收到錯誤“Object Moved”。 URL雖然在瀏覽器中加載了WSDL。 如果有更好的方法從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);
                }
            }
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM