【问题标题】:Upload file to folder in sharepoint with C#使用 C# 将文件上传到共享点中的文件夹
【发布时间】:2017-04-27 18:56:50
【问题描述】:

我尝试将文件上传到共享点中的文件夹(未列出)。但是,“executeQuery()”何时显示错误“找不到文件”:s

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

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

【问题讨论】:

    标签: c# sharepoint upload directory


    【解决方案1】:

    好吧,我自己回答:

    问题出在中的“url”参数

    FileCreationInformation newFile = new FileCreationInformation { Content = System.IO.File.ReadAllBytes(@p), Url = Path.GetFileName(@p), Overwrite = true };
    

    需要是网站的相对 URL。虽然用“https://xxx.sharepoint.com/sites/some/some2”定义了完整的 url,但还是需要在

    中将“/sites/some/some2”定义为相对 url
    FileCreationInformation newFile = new FileCreationInformation { Content = System.IO.File.ReadAllBytes(@p), Url = "/sites/some/some2/"+fileName, Overwrite = true };
    

    然后工作。 (干得好,微软 ¬¬)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-04-26
      • 1970-01-01
      • 2019-11-14
      • 2012-12-25
      • 2011-01-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多