Search code examples
htmlsharepointsharepoint-2010

Display HTML page stored in SharePoint documents folder


I'm working on a SharePoint web part that displays a number of different reports in different divs on the page. In one of these divs, I need to display the HTML from a page we have stored in the 'Documents' container within SharePoint. The info in the HTML page is retrieved from several different parts of the application, and is displayed differently, so basically we're using it as the source data. I'm trying to figure out how to access the page from within the app and hopefully store the link to the file as a configurable setting so I can set it up for our dev/test/prod environments.

I've loaded the HTML file into the 'Documents' folder, and if I browse to it manually it displays fine but if I use the following:

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

I get a 403 error and in the response header the message, "Before opening files from this location you must first browse to the website and select the option to login automatically".

I thought the RunWithElevatedPriveleges would pass the credentials through but I'm pretty new to SharePoint. Not sure if I'm using the right approach, any help is appreciated.


Solution

  • Figured it out. There were a number of permissions problems but once those were sorted this code worked:

    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;
                     }
             }
           }
       }
    

    }