Archive for August 2014

Generic Handler to Unpublish Past Events in Umbraco

The following Generic Handler (C#) is used to unpublish the past/old Events automatically after it's expiration date in Umbraco.

Source Code:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Configuration;
  6. using umbraco.MacroEngines;
  7. using umbraco.cms.businesslogic.web;
  8. namespace KPMGAudit.Web.GenericHandlers
  9. {
  10.     /// <summary>
  11.     /// Summary description for UnpublishExpiredEvents
  12.     /// </summary>
  13.     public class UnpublishExpiredEvents : IHttpHandler
  14.     {
  15.         public void ProcessRequest(HttpContext context)
  16.         {
  17.             int EventsNodeId = Convert.ToInt32(ConfigurationManager.AppSettings["EventsNodeId"]);
  18.             string strEventStartDate = string.Empty, strEventEndDate = string.Empty;
  19.             context.Response.ContentType = "text/plain";
  20.             dynamic EventsNode = new DynamicNode().NodeById(EventsNodeId);
  21.             List<DynamicNode> eList = EventsNode.Children.Items;
  22.             foreach (var Event in eList)
  23.             {
  24.                 int CurrentEventNodeId = Convert.ToInt32(Event.Id);
  25.                 try
  26.                 {
  27.                     DateTime EventStartDate = Convert.ToDateTime(Event.GetProperty("eventStartDate").Value);
  28.                     strEventStartDate = EventStartDate.ToString("MM/dd/yyyy");
  29.                 }
  30.                 catch { strEventStartDate = string.Empty; }
  31.                 try
  32.                 {
  33.                     DateTime EventEndDate = Convert.ToDateTime(Event.GetProperty("eventEndDate").Value);
  34.                     strEventEndDate = EventEndDate.ToString("MM/dd/yyyy");
  35.                 }
  36.                 catch { strEventEndDate = string.Empty; }
  37.                 Document CurrentEventNode = new Document(CurrentEventNodeId);
  38.                 if (!string.IsNullOrEmpty(strEventEndDate))
  39.                 {
  40.                     if (Convert.ToDateTime(strEventEndDate) < DateTime.Now)
  41.                     {
  42.                         CurrentEventNode.UnPublish();
  43.                     }
  44.                 }
  45.                 else
  46.                 {
  47.                     if (Convert.ToDateTime(strEventStartDate) < DateTime.Now)
  48.                     {
  49.                         CurrentEventNode.UnPublish();
  50.                     }
  51.                 }
  52.             }
  53.         }
  54.         public bool IsReusable
  55.         {
  56.             get
  57.             {
  58.                 return false;
  59.             }
  60.         }
  61.     }
  62. }
Or
  1. public void UnpublishNode(Document CurrentContentNode, DateTime ContentExpiryDate)
  2.         {
  3.             if (ContentExpiryDate != DateTime.MinValue)
  4.             {
  5.                 if (Convert.ToDateTime(ContentExpiryDate) < DateTime.Now)
  6.                 {
  7.                     CurrentContentNode.UnPublish();
  8.                 }
  9.             }
  10.         }

Configuration: (umbracoSettings.config)

  1.   <scheduledTasks>
  2.     <!-- add tasks that should be called with an interval (seconds) -->
  3.     <!--    <task log="true" alias="test60" interval="60" url="http://localhost/umbraco/test.aspx"/>-->
  4.     <task log="false" alias="ExpiredEventsUnpublishScheduler" interval="60" url="http://local.kpmg.com/GenericHandlers/UnpublishExpiredEvents.ashx"/>
  5.   </scheduledTasks>
Thursday 7 August 2014
Posted by Jebastin

How to Use CDATA in XML?

All text in an XML document will be parsed by the parser. But text inside a CDATA section will be ignored by the parser.

XML parsers normally parse all the text in an XML document.
When an XML element is parsed, the text between the XML tags is also parsed:
<message>This text is also parsed</message>
 The parser does this because XML elements can contain other elements, as in this example, where the <name> element contains two other elements (first and last):
<name><first>Bill</first><last>Gates</last></name>
and the parser will break it up into sub-elements like this:
<name>
  <first>Bill</first>
  <last>Gates</last>
</name>
 Parsed Character Data (PCDATA) is a term used about text data that will be parsed by the XML parser.

CDATA - (Unparsed) Character Data

The term CDATA is used about text data that should not be parsed by the XML parser.
Characters like "<" and "&" are illegal in XML elements.
"<" will generate an error because the parser interprets it as the start of a new element.
"&" will generate an error because the parser interprets it as the start of an character entity.
Some text, like JavaScript code, contains a lot of "<" or "&" characters. To avoid errors script code can be defined as CDATA.
Everything inside a CDATA section is ignored by the parser.
A CDATA section starts with "<![CDATA[" and ends with "]]>":

<script>
<![CDATA[
function matchwo(a,b)
{
if (a < b && a < 0) then
  {
  return 1;
  }
else
  {
  return 0;
  }
}
]]>
</script>
In the example above, everything inside the CDATA section is ignored by the parser.

Code:
  1.  string CDATAStart = "<![CDATA[", CDATAEnd = "]]>";
  2.  LongDescription = CDATAStart + mcLongDescription.Trim() + CDATAEnd;
Notes on CDATA sections:
A CDATA section cannot contain the string "]]>". Nested CDATA sections are not allowed.
The "]]>" that marks the end of the CDATA section cannot contain spaces or line breaks. 

Reference: W3Schools, StackOverflow
Posted by Jebastin
Tag : ,

C# Code to implement Google Analytics Tracking Script dynamically

In general we track a page using JavaScript statically. This C# code is used to implement Google Analytics Tracking Script dynamically for every page we want to do it.

Source Code:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Configuration;
  4. using System.Linq;
  5. using System.Web;
  6. namespace KPMGAudit.Web.GoogleAnalytics
  7. {
  8.     public static class Tracking
  9.     {
  10.         public static string TrackingScript(string url)
  11.         {
  12.             var jsTestString = string.Format(@"
  13.                     <script type=""text/javascript"">
  14.                         var _gaq = _gaq || [];
  15.                         _gaq.push(['_setAccount', '{0}']);
  16.                         _gaq.push(['_trackPageview','{1}']);
  17.                         (function() {{
  18.                         var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
  19.                         ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
  20.                         var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  21.                         }})();
  22.                     </script>", ConfigurationManager.AppSettings["GoogleAnalyticsAccountCode"], url);
  23.             return jsTestString;
  24.         }
  25.     }
  26. }
  1. // Inject javascript into Long Description
  2. var url = string.Format("/{0}/{1}", mcContentCategory.AsUrl(), mcTitle.AsUrl());
  3. mcLongDescription = mcLongDescription + GoogleAnalytics.Tracking.TrackingScript(url); 

Another way: Click here.
Posted by Jebastin

How to remove the trailing directory slash from the URL?

It's possible to configure Umbraco to remove the final trailing slash on the URL. If you want to do this, locate the 'umbracoSettings.config' file. This can be found in the 'webroot\config\' directory of your site.
Open this file and locate the 'addTrailingSlash' setting in the 'requestHandler' section of the file.
<requestHandler>
<addTrailingSlash>false</addTrailingSlash>
</requestHandler>
Then all you need to do is to change the addTrailingSlash value to 'false'. Don't forget to save your changes.
Wednesday 6 August 2014
Posted by Jebastin

How to remove the .ASPX extension from the URL?

First of all, to configure Umbraco to show URL's without the .aspx extension, you're going to need to locate the 'umbracoUseDirectoryUrls' setting in the ' appSettings' section of the 'web.config' file for your site.
<appSettings>
<add key="umbracoUseDirectoryUrls" value="true" />
</appSettings>
Once you've opened this file, all you need to do is change the 'umbracoUseDirectoryUrls' value to 'true'.
Posted by Jebastin

Link To This Post/Page

Spread The Word

Add this button to your blog:
JJ Technology Solutions

Blog Archive

Trackers

eXTReMe Tracker
facebook

- Copyright © JJ Technology Solutions - Powered by Source Code Solutions -