I am working in Umbraco for last few months and want to acheive following.
I want to get html (images and css) for all the pages created in Umbraco content section. In simple words, can I do something with xslt that all created pages in contents section are saved as html (as these appear in browser) with their css etc?
Yes, it can be done and obviously requries some coding...
Assuming you want this html generated for each change (publish), I suggest writing a event handler that subscribes to the AfterPublish event for documents and use RenderTemplate() method (from umbraco.library class) to get the html content and save that to disk.
More info on event handling can be found on the wiki.
Thanks Dirk for the help. Can I have a reference for sample code to get this? I am finding it to resolve it at my end anyway. Thanks again for your help.
using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using umbraco.BusinessLogic; using umbraco.cms.businesslogic; using umbraco.cms.businesslogic.web;
namespace mynamespace {
public class renderhtml : umbraco.BusinessLogic.ApplicationBase { public renderhtml() { Document.AfterPublish += new Document.PublishEventHandler(Document_AfterPublish);
string html = umbraco.library.RenderTemplate(sender.Id, sender.Template);
//string html = "<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'><html xmlns='http://www.w3.org/1999/xhtml'><head><meta http-equiv='Content-Type' content='text/html; charset=utf-8' /><title>Blackbird</title></head><body><h1>Welcome to bb</h1><p>This is Bb site, a project by ibt.</p></body></html>";
string path = @"c:\test\MyTest.html"; if (!File.Exists(path)) { // Create a file to write to. using (StreamWriter sw = File.CreateText(path)) { sw.Write(html); } }
}
This code works fine with my commented string html, but when I provide it "string html = umbraco.library.RenderTemplate(sender.Id, sender.Template);" to write in the file, the file MyTest.html has the following contents
==========================================
<!-- Error generating macroContent: 'System.Web.HttpException: Error executing child request for /default.aspx. ---> System.Web.HttpRequestValidationException: A potentially dangerous Request.Form value was detected from the client (ctl00$body$content="<p>This is Bb..."). at System.Web.HttpRequest.ValidateString(String s, String valueName, String collectionName) at System.Web.HttpRequest.ValidateNameValueCollection(NameValueCollection nvc, String collectionName) at System.Web.HttpRequest.get_Form() at System.Web.HttpRequest.get_Item(String key) at umbraco.UmbracoDefault.Page_Load(Object sender, EventArgs e) at System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) at System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) at System.Web.UI.Control.OnLoad(EventArgs e) at System.Web.UI.Control.LoadRecursive() at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) at System.Web.UI.Page.ProcessRequest(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) at System.Web.UI.Page.ProcessRequest() at System.Web.UI.Page.ProcessRequestWithNoAssert(HttpContext context) at System.Web.UI.Page.ProcessRequest(HttpContext context) at ASP.default_aspx.ProcessRequest(HttpContext context) in c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\root\bcf59f34\210f5834\App_Web_default.aspx.cdcab7d2.glr6iqyh.0.cs:line 0 at System.Web.HttpServerUtility.ExecuteInternal(IHttpHandler handler, TextWriter writer, Boolean preserveForm, Boolean setPreviousPage, VirtualPath path, VirtualPath filePath, String physPath, Exception error, String queryStringOverride) --- End of inner exception stack trace --- at System.Web.HttpServerUtility.ExecuteInternal(IHttpHandler handler, TextWriter writer, Boolean preserveForm, Boolean setPreviousPage, VirtualPath path, VirtualPath filePath, String physPath, Exception error, String queryStringOverride) at System.Web.HttpServerUtility.Execute(String path, TextWriter writer, Boolean preserveForm) at System.Web.HttpServerUtility.Execute(String path, TextWriter writer) at umbraco.library.RenderTemplate(Int32 PageId, Int32 TemplateId)' -->
But with that said, I am currently looking at doing a similar thing with generating HTML files from the Publish event... so good luck with that too! Let us know how you get on with it!
If I needed such a functionality I would use the WebClientClass of the Microsoft Framework to download the file. Look at the documentation of the WebClientClass how to use that one then build the url in the afterpublish event
Lee, thank you for the help but I am looking for a solution where we can generate a fresh copy of html of the site when any page is published. thanks for the help.
Richard, I will try your suggested solution. Thanks
get all the pages in contents as html
Hi
I am working in Umbraco for last few months and want to acheive following.
I want to get html (images and css) for all the pages created in Umbraco content section. In simple words, can I do something with xslt that all created pages in contents section are saved as html (as these appear in browser) with their css etc?
Regards
Nauman
Nauman,
Yes, it can be done and obviously requries some coding...
Assuming you want this html generated for each change (publish), I suggest writing a event handler that subscribes to the AfterPublish event for documents and use RenderTemplate() method (from umbraco.library class) to get the html content and save that to disk.
More info on event handling can be found on the wiki.
Hope this helps.
Regards,
/Dirk
Thanks Dirk for the help. Can I have a reference for sample code to get this? I am finding it to resolve it at my end anyway. Thanks again for your help.
The subpages in the wiki are lised onthe right hand side, the samples are available there. http://our.umbraco.org/wiki/reference/api-cheatsheet/using-applicationbase-to-register-events/event-examples
Chris
Hi Chris
For the purpose I mention above, I started to write the cs code as following, can you please help me in writing the code for save the pages as html?
=======================================================
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using umbraco.BusinessLogic;
using umbraco.cms.businesslogic;
using umbraco.cms.businesslogic.web;
namespace mynamespace
{
public class renderhtml : umbraco.BusinessLogic.ApplicationBase
{
public renderhtml()
{
Document.AfterPublish += new Document.PublishEventHandler(Document_AfterPublish);
}
void Document_AfterPublish(Document sender, PublishEventHandler e)
{
Log.Add(LogTypes.Debug, sender.Id, "the document " + sender.Text + " is about to be published");
//cancel the publishing
e.Cancel = true;
}
}
}
===============================================
Regards
Nauman
Hi Nauman,
inject this piece of code in the Document_AfterPublish event handler:
(msdn ref)
Hope this helps.
Regards,
/Dirk
Thank you very much Dirk. This is very helpful. I will implement the solution and if something went wrong, I will get back to you. thanks again.
Hi Dirk, Its me again, please review the following code.
void Document_AfterPublish(Document sender, PublishEventArgs e)
{
string html = umbraco.library.RenderTemplate(sender.Id, sender.Template);
//string html = "<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'><html xmlns='http://www.w3.org/1999/xhtml'><head><meta http-equiv='Content-Type' content='text/html; charset=utf-8' /><title>Blackbird</title></head><body><h1>Welcome to bb</h1><p>This is Bb site, a project by ibt.</p></body></html>";
string path = @"c:\test\MyTest.html";
if (!File.Exists(path))
{
// Create a file to write to.
using (StreamWriter sw = File.CreateText(path))
{
sw.Write(html);
}
}
}
This code works fine with my commented string html, but when I provide it "string html = umbraco.library.RenderTemplate(sender.Id, sender.Template);" to write in the file, the file MyTest.html has the following contents
==========================================
<!-- Error generating macroContent: 'System.Web.HttpException: Error executing child request for /default.aspx. ---> System.Web.HttpRequestValidationException: A potentially dangerous Request.Form value was detected from the client (ctl00$body$content="<p>This is Bb...").
at System.Web.HttpRequest.ValidateString(String s, String valueName, String collectionName)
at System.Web.HttpRequest.ValidateNameValueCollection(NameValueCollection nvc, String collectionName)
at System.Web.HttpRequest.get_Form()
at System.Web.HttpRequest.get_Item(String key)
at umbraco.UmbracoDefault.Page_Load(Object sender, EventArgs e)
at System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e)
at System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e)
at System.Web.UI.Control.OnLoad(EventArgs e)
at System.Web.UI.Control.LoadRecursive()
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
at System.Web.UI.Page.ProcessRequest(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
at System.Web.UI.Page.ProcessRequest()
at System.Web.UI.Page.ProcessRequestWithNoAssert(HttpContext context)
at System.Web.UI.Page.ProcessRequest(HttpContext context)
at ASP.default_aspx.ProcessRequest(HttpContext context) in c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\root\bcf59f34\210f5834\App_Web_default.aspx.cdcab7d2.glr6iqyh.0.cs:line 0
at System.Web.HttpServerUtility.ExecuteInternal(IHttpHandler handler, TextWriter writer, Boolean preserveForm, Boolean setPreviousPage, VirtualPath path, VirtualPath filePath, String physPath, Exception error, String queryStringOverride)
--- End of inner exception stack trace ---
at System.Web.HttpServerUtility.ExecuteInternal(IHttpHandler handler, TextWriter writer, Boolean preserveForm, Boolean setPreviousPage, VirtualPath path, VirtualPath filePath, String physPath, Exception error, String queryStringOverride)
at System.Web.HttpServerUtility.Execute(String path, TextWriter writer, Boolean preserveForm)
at System.Web.HttpServerUtility.Execute(String path, TextWriter writer)
at umbraco.library.RenderTemplate(Int32 PageId, Int32 TemplateId)' -->
==========================================
can you please advise where I am wrong?
Nauman
Just to throw an alternative idea out there... Nauman, if you just need a static HTML version of your website, then take a look at WinHTTrack
http://www.httrack.com/
It might be a quicker/easier solution?
But with that said, I am currently looking at doing a similar thing with generating HTML files from the Publish event... so good luck with that too! Let us know how you get on with it!
Cheers, Lee.
Hi ,
If I needed such a functionality I would use the WebClientClass of the Microsoft Framework to download the file. Look at the documentation of the WebClientClass how to use that one then build the url in the afterpublish event
string url = "http://www.mysite.com/default.aspx?id="+ sender.id;
Then you can use the url in the webclient class to download the file to your local disk.
Cheers,
Richard
Lee, thank you for the help but I am looking for a solution where we can generate a fresh copy of html of the site when any page is published. thanks for the help.
Richard, I will try your suggested solution. Thanks
Cheers
Nauman
Richard, your solution is good but it is not downloading the related css, images or js files. Thanks for sharing idea anyway.
Dirk, are you out there to help?
is working on a reply...