显示存储在 SharePoint 文档文件夹中的 HTML 页

Display HTML page stored in SharePoint documents folder

我正在开发一个 SharePoint Web 部件,它在页面的不同 div 中显示许多不同的报告。在其中一个 div 中,我需要显示存储在 SharePoint 内 'Documents' 容器中的页面中的 HTML。 HTML 页面中的信息是从应用程序的几个不同部分检索的,并且显示方式不同,所以基本上我们将其用作源数据。我正在尝试弄清楚如何从应用程序内访问该页面,并希望将 link 作为可配置设置存储到文件中,以便我可以为我们的 dev/test/prod 环境进行设置。

我已将 HTML 文件加载到 'Documents' 文件夹中,如果我手动浏览它,它显示正常,但如果我使用以下内容:

SPSecurity.RunWithElevatedPrivileges(delegate
{
    using (System.Net.WebClient client = new System.Net.WebClient())
    {
    string htmlCode = client.DownloadString(url);
    }
}

我收到 403 错误,在响应 header 消息中,"Before opening files from this location you must first browse to the website and select the option to login automatically"。

我认为 RunWithElevatedPriveleges 会传递凭据,但我对 SharePoint 还很陌生。不确定我使用的方法是否正确,如有任何帮助,我们将不胜感激。

将页面放入标准文档库,然后使用页面查看器 Web 部件。站点资产库用于其他自定义目的。您甚至不需要 SharePoint Designer。页面查看器应设置为 "Web Page",因为网页查看器本质上变成了 IFRAME。

如果仍然有问题...可能是 Web 应用程序级别的设置导致非 Microsoft 文件出现问题

转到管理中心 > 管理 Web 应用程序。然后我选择我的 Web 应用程序并单击 "General Settings" 按钮。然后我将浏览器文件处理从 "Strict" 更改为 "Permissive" 并解决了我的问题。我已经包含了该设置的附件,因此您可以阅读与之相关的文本。

想通了。存在许多权限问题,但一旦这些问题得到解决,这段代码就可以工作了:

using (SPSite site = new SPSite(SPContext.Current.Site.ID))
{
    using (SPWeb web = site.OpenWeb())
    {
      SPFolder folder = web.GetFolder("MainFolder/Lists/MainFolderDocs");

      if (folder.Exists)
      {
         SPFile file = web.GetFile("/MainFolder/Lists/MainFolderDocs/Mainlist.html");
         if (file.Exists)
         {
             using (System.IO.StreamReader reader = new System.IO.StreamReader(file.OpenBinaryStream()))
                 {
                    string htmlCode = reader.ReadToEnd();
                    lChecklist.Text = htmlCode;
                 }
         }
       }
   }

}