【问题标题】:Get Attachments from Sharepoint Online ListItem从 Sharepoint Online ListItem 获取附件
【发布时间】:2017-06-30 15:08:23
【问题描述】:

我正在尝试获取 Sharepoint 在线列表项或其网址的附件。到目前为止,我只找到了解决方案 like these

但问题是,当我浏览附件所在的文件夹时,它是空的。 这很令人困惑,因为在 SharepointClientBrowser 中,文件夹不为空。如果不在带有 url https://scundp.sharepoint.com/sites/list_overview_test/f0/Lists/Testlist2/Attachments/2 的文件夹中,还应该将 https://scundp.sharepoint.com/sites/list_overview_test/f0/Lists/Testlist2/Attachments/2/Dokument1.png 存储在哪里?

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

我正在开发的 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)
                        {

                        }
                    }
                }

【问题讨论】:

    标签: c# sharepoint


    【解决方案1】:

    您可以使用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,这里有一些很好的例子: How to get a file using SharePoint Client Object Model with only an absolute url at hand?

    【讨论】:

    • 是的,我只能使用 CSOM。谢谢,我会尝试的,但是如果我没有完整的路径,它会起作用吗?我的意思是,在我的代码中,我只得到文件名的一部分。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-04
    • 1970-01-01
    • 2014-01-17
    • 2011-06-07
    • 1970-01-01
    • 2022-08-19
    相关资源
    最近更新 更多