从 Sharepoint Online ListItem 获取附件

问题描述 投票:0回答:1

我正在尝试从 SharePoint Online ListItem 或其 URL 获取附件。到目前为止,我只找到了解决方案喜欢这些

但问题是,当我浏览附件所在的文件夹时,它是空的。

这非常令人困惑,因为在 SharePoint 客户端浏览器中,该文件夹不为空。

还有应该存储在哪里

https://scundp.sharepoint.com/sites/list_overview_test/f0/Lists/Testlist2/Attachments/2/Dokument1.png
如果不在带有url的文件夹中
https://scundp.sharepoint.com/sites/list_overview_test/f0/Lists/Testlist2/Attachments/2

在我的代码中,我尝试将 ListItem 的附件 URL 写入

newListItem["AFiles"]
listItem["Attachments"].ToString() == "True"
工作正常,因此该块仅在有任何附件时运行。 但是
bool h
设置为
false
并且该文件夹也被解释为空。

我正在使用的 SharePoint 加载项具有以下权限:

  • 网络 - 管理,
  • 网站集 - 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)
    {
    }
}
c# sharepoint
1个回答
0
投票

您可以使用

WebRequest
来完成此任务,以便从共享点站点下载文件:

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
,这里有一些很好的例子: 如何使用 SharePoint 客户端对象模型获取只有绝对 URL 的文件?

© www.soinside.com 2019 - 2024. All rights reserved.