从Sharepoint Online ListItem获取附件

时间:2017-06-30 15:08:23

标签: c# sharepoint

我正在尝试获取Sharepoint的在线listItem或他们的Urls。到目前为止,我只找到了解决方案like these

但问题是,当我浏览附件所在的文件夹时,它是空的。 这非常令人困惑,因为在SharepointClientBrowser中,Folder不为空。如果不在包含网址https://scundp.sharepoint.com/sites/list_overview_test/f0/Lists/Testlist2/Attachments/2/Dokument1.png

的文件夹中,也应该存储在哪里https://scundp.sharepoint.com/sites/list_overview_test/f0/Lists/Testlist2/Attachments/2

在我的代码中,我尝试将listItem的Urles of Attachments写入newListItem["AFiles"]. listItem["Attachments"].ToString() == "True"正常工作,因此只有在有任何附件时才会运行Block。 但是bool h设置为false,Folder也被解释为空。

我正在处理的Sharepoint加载项具有权限:Web - 管理,网站集 - fullControl。整个网站集的设置“客户端应用程序中的打开文档”处于活动状态。

                    if (listItem["Attachments"].ToString() == "True")
                    {
                        bool h = listItem.AttachmentFiles.AreItemsAvailable;


                        try
                        {
                            Web oWeb = context.Web;
                            Folder folder = oWeb.GetFolderByServerRelativeUrl(siteName + "/Lists/" + listName + "/Attachments/" + listItem["ID"]);
                            context.Load(folder);
                            context.ExecuteQuery();
                            FileCollection attachments = folder.Files;
                            context.Load(attachments);
                            context.ExecuteQuery();
                            newListItem["AFiles"] = "";

                            foreach (Microsoft.SharePoint.Client.File oFile in folder.Files)
                            {
                                newListItem["AFiles"] = newListItem["AFiles"] + "  " + oFile.ServerRelativeUrl;
                            }
                        }
                        catch (ServerException ex)
                        {

                        }
                    }
                }

1 个答案:

答案 0 :(得分:0)

您可以使用WebRequest来完成此任务,以便从sharepoint网站下载文件:

public void DownloadFile(string serverFilePath, string destPath)
        {
            var url = string.Format("{0}/{1}", ServerURL, serverFilePath);
            createDirectiory(Path.GetDirectoryName(destPath)); // this method creates your directory
            var request = System.Net.HttpWebRequest.Create(url);
            request.Credentials = System.Net.CredentialCache.DefaultCredentials;
            using (var sReader = new StreamReader(request.GetResponse().GetResponseStream()))
            {
                using (var sWriter = new StreamWriter(destPath))
                {
                    sWriter.Write(sReader.ReadToEnd());
                }
            }
        }

如果你想使用Client-object-model这里有一些很好的例子: How to get a file using SharePoint Client Object Model with only an absolute url at hand?