使用C#将文件上传到sharepoint中的文件夹

时间:2017-04-27 18:56:51

标签: c# sharepoint upload directory

我尝试将文件上传到sharepoint中的文件夹(Not list)。但是,什么时候做" executeQuery()"显示错误"找不到文件" :■

我尝试了很多东西,最后一个是:

string siteUrl = "https://xxx.sharepoint.com/sites/some/some2";
        //Insert Credentials
        ClientContext context = new ClientContext(siteUrl);

        SecureString passWord = new SecureString();
        foreach (var c in "MySecurityPassword.") passWord.AppendChar(c);
        context.Credentials = new SharePointOnlineCredentials("name@domain.com", passWord);
        Web site = context.Web;

        //Get the required RootFolder
        string barRootFolderRelativeUrl = "2017"; //<- existent folder
        Folder barFolder = site.GetFolderByServerRelativeUrl(barRootFolderRelativeUrl);

        //Create new subFolder to load files into
        string newFolderName = "pro" + DateTime.Now.ToString("yyyyMMddHHmm");
        barFolder.Folders.Add(newFolderName);
        barFolder.Update();

        //Add file to new Folder
        Folder currentRunFolder = site.GetFolderByServerRelativeUrl(barRootFolderRelativeUrl + "/" + newFolderName);
        FileCreationInformation newFile = new FileCreationInformation { Content = System.IO.File.ReadAllBytes(@p), Url = Path.GetFileName(@p), Overwrite = true };
        currentRunFolder.Files.Add(newFile);
        currentRunFolder.Update();

        context.ExecuteQuery();

        //Return the URL of the new uploaded file
        newUrl = siteUrl + barRootFolderRelativeUrl + "/" + newFolderName + "/" + Path.GetFileName(@p);

帮助:C

1 个答案:

答案 0 :(得分:0)

好吧,我回应自己:

问题是

中的“url”参数
FileCreationInformation newFile = new FileCreationInformation { Content = System.IO.File.ReadAllBytes(@p), Url = Path.GetFileName(@p), Overwrite = true };

需要是网站的相对网址。虽然使用“https://xxx.sharepoint.com/sites/some/some2”定义了完整的网址,但必须将“/ sites / some / some2”定义为

中的相对网址
FileCreationInformation newFile = new FileCreationInformation { Content = System.IO.File.ReadAllBytes(@p), Url = "/sites/some/some2/"+fileName, Overwrite = true };

并且工作。 (干得好,微软¬¬)。