Developer forum

Forum » Templates » Get page view by page ID

Get page view by page ID

Jakob Westhausen
Reply

If you are having problems with the GetPageviewByPageID method (Dynamicweb.Frontend.PageView.GetPageviewByPageID( somepageid ).Output() it's because the <head> tag conflics with the excisting <head> on the current page.

So how can we extract the content of only the <body> tag?? No worries the solution is here :)

Use this function instead:

@functions {
  string GetPageContent(int pageId) {
    try {
      var url = string.Format( "http://{0}/Default.aspx?ID={1}", @GetGlobalValue("Global:Request.Host"), pageId );

      System.Net.WebClient client = new System.Net.WebClient();
      Byte[] requestedHTML;
      requestedHTML = client.DownloadData( url );
      System.Text.UTF8Encoding objUTF8 = new System.Text.UTF8Encoding();
      string html = objUTF8.GetString( requestedHTML );

      var content = System.Text.RegularExpressions.Regex.Split( html, @"((?:.(?!<\s*body[^>]*>))+.<\s*body[^>]*>)|(<\s*/\s*body\s*\>.+)" )[0];
      content = content.Trim();
      return content;
    }
    catch { }
    return null;
  }
}

<div>@GetPageContent( pageId )</div>


Replies

 
Mikkel Ricky
Reply

Cool!

You can actually do it without using a WebClient and using the Dynamicweb API:

@functions {
    string GetPageContent(int pageId, bool trim = true)
    {
        try
        {
            var contentPageView = Dynamicweb.Frontend.PageView.GetPageviewByPageID(pageId);
            var content = contentPageView.Content.CreateContent(pageId);
            content = contentPageView.TemplatePage.Output();

            if (trim)
            {
                // remove everything but the stuff between <body> and </body>
                content = System.Text.RegularExpressions.Regex.Replace(content, @"(^.*<body[^>]*>)|(<\/body>.*$)", "", System.Text.RegularExpressions.RegexOptions.IgnoreCase | System.Text.RegularExpressions.RegexOptions.Singleline);
                content = content.Trim();
            }

            return content;
        }
        catch { }
        return null;
    }
}

I have a feeling that we've been working on the same challenge …

Best regards,
Mikkel

 
Jakob Westhausen
Reply

It works fine in Chrome but in Safari it get this output: http://cl.ly/image/182v2k1h1f23

 

Best regards,

Jakob

 
Mikkel Ricky
Reply

You're absolutely right, Jakob!

 
Jakob Westhausen
Reply

Any updates on how to achieved this with the Dynamicweb API?

 
Morten Bengtson
Reply

I once had a similar problem with loading another pageview in a pageview context (page rendering). The reason was that the second pageview also caused a double http compression. Some browsers could handle it, but others would fail.

So, try to disable http compression and see if that makes any difference:

http://manual.dynamicweb-cms.com/Dynamicweb-On-line-Manual/Management-Center/Web-and-HTTP/Http-Compression.aspx

 
Jakob Westhausen
Reply

I don't think disabling the http compression is a solution. My GetPageContent() function works fine, but I would rather use the DW API :)
I would be a nice feature if the page content type could be set to something like "content only" 
http://cl.ly/image/1m222v3g0e1z

But thanks anyways Morten :)

 
Nicolai Høeg Pedersen
Reply

Hi Jakob

I do not think Dynamicweb likes 2 pageviews being created on the same pageview.

The 2nd pageview instance will also set the compression headers, and I think that is what clutters you output on Safari.

So maybe in your second isntance you can remove the extra "Content-Encoding" from the headers collection:

HttpContext.Current.Response.Headers.Remove("Content-Encoding")

 

You must be logged in to post in the forum