从网站删除sharepoint中的文件

时间:2017-11-21 14:11:53

标签: c# asp.net sharepoint

我正在尝试上传并稍后从sharepoint网站删除文件。上传工作正常但我收到错误删除。

这是我的代码:

> library('proto')
> sessionInfo()
R version 3.3.3 (2017-03-06)
Platform: x86_64-redhat-linux-gnu (64-bit)
Running under: CentOS release 6.8 (Final)

locale:
 [1] LC_CTYPE=en_GB.utf8       LC_NUMERIC=C
 [3] LC_TIME=en_GB.utf8        LC_COLLATE=en_GB.utf8
 [5] LC_MONETARY=en_GB.utf8    LC_MESSAGES=en_GB.utf8
 [7] LC_PAPER=en_GB.utf8       LC_NAME=C
 [9] LC_ADDRESS=C              LC_TELEPHONE=C
[11] LC_MEASUREMENT=en_GB.utf8 LC_IDENTIFICATION=C

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base

other attached packages:
[1] proto_1.0.0

loaded via a namespace (and not attached):
[1] tools_3.3.3 tcltk_3.3.3
> .libPaths()
[1] "/cluster/gjb_lab/username/R/x86_64-redhat-linux-gnu-library/3.3"
[2] "/cluster/gjb_lab/software/centos6.3/opt/R/3.3.3/lib64/R/library"
[3] "/usr/lib64/R/library"

错误讯息:

  

{“网址'https://sites.company.com/sites/333333'的网站上不存在列表'mydoc'。”}

因此,删除 private void UploadSharePoint(string siteNumber, string fileName) { try { using (ClientContext client = new ClientContext("https://sites.company.com/sites/" + siteNumber + "/")) { client.Credentials = new NetworkCredential("user", "pw", "domain"); var formLib = client.Web.Lists.GetByTitle("Documents"); client.Load(formLib.RootFolder); client.ExecuteQuery(); //File Upload var fileCreationInfo = new FileCreationInformation { Content = System.IO.File.ReadAllBytes(fileName), Overwrite = true, Url = Path.Combine("Documents/99_Upload", Path.GetFileName(fileName)) }; var uploadFile = formLib.RootFolder.Files.Add(fileCreationInfo); client.Load(uploadFile); client.ExecuteQuery(); //File Delete Web web = client.Web; List list = web.Lists.GetByTitle("mydoc"); client.Load(list); --> here I get nothing back Folder folder = list.RootFolder; client.Load(folder); FileCollection files = folder.Files; client.Load(files); client.ExecuteQuery(); Microsoft.SharePoint.Client.File file = files[1]; file.DeleteObject(); client.ExecuteQuery(); } } catch (System.Exception ex) { } }

的输入必定存在问题

Sharepoint中显示的标题是mydoc。文件名是mydoc.txt。我试过两个但没什么作用。

感谢您的帮助

1 个答案:

答案 0 :(得分:2)

抛出的错误消息是正确的:

  

{" List' mydoc'在包含网址' https://sites.company.com/sites/333333'。"}

的网站上不存在

问题是您正在使用标题" mydoc"创建 SharePoint列表项,但之后您尝试使用标题访问 SharePoint列表 " mydoc"哪个不存在。请参阅下面的更新代码:

            //File Delete
            Web web = client.Web;
            List list = web.Lists.GetByTitle("Documents");
            client.Load(list);  --> here you will get reference to the list

            Microsoft.SharePoint.Client.File file = web.GetFileByServerRelativeUrl("/Documents/mydoc.txt");
            client.Load(file);
            file.DeleteObject();
            client.ExecuteQuery();