Calling all developers! We invite you to provide your input on Feature Experimentation by completing this brief survey.

 

Anders Hattestad
Jan 26, 2011
  5655
(0 votes)

Just for fun

Deane Barker (@gadgetopia) tweeted about some code he had done just for fun to extend the PropertyDataGet handler. The concept is cool, and you should take a look @ http://pastie.org/1493264 

But the problem is of course that its not strong typed, so we have problems with magic strings. But sometimes you can live with that, and Deane code is a nice extension if you can find a use :)

You can extend properties with extension methods. Those will be strong typed from the backend code. So I made myself a method that found every extension methods that worked on PropertyData or PageData and returned PropertyData or string that exists in the dll’s and makes them available for use thou the PropertyDataCollection.GetHandler.

The trick Deane used to exclude assembly's that started with System work great.

Code Snippet
  1. public static void Initialize(int optionFlags)
  2. {
  3.     extendProperties = new Hashtable();
  4.     extendPageData = new Hashtable();
  5.     HelpText = new StringBuilder();
  6.     AddText(HelpText, "<ul>");
  7.     foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies())
  8.     {
  9.         // This cuts the number of assemblies to be iterated by about 75%.  If your assembly starts with "System" for some reason (really bad idea, for the record), you need to remove this.
  10.         if (assembly.FullName.StartsWith("System"))
  11.             continue;
  12.         foreach (Type type in assembly.GetTypes())
  13.         {
  14.             foreach (MethodInfo method in type.GetMethods())
  15.             {
  16.                 bool isExtension = method.IsDefined(typeof(ExtensionAttribute), true);
  17.                 if (isExtension)
  18.                 {
  19.                     var parameters = method.GetParameters();
  20.                     Add<PropertyData, PropertyData>("PropertyData [PropertyData]:", HelpText, method, parameters, extendProperties);
  21.                     Add<PropertyData, string>("string [PropertyData]:", HelpText, method, parameters, extendProperties);
  22.                     Add<PageData, PropertyData>("PropertyData [PageData]:", HelpText, method, parameters, extendPageData);
  23.                     Add<PageData, string>("string [PageData]:", HelpText, method, parameters, extendPageData);
  24.                 }
  25.             }
  26.         }
  27.     }
  28.     AddText(HelpText, "</ul>");
  29.     PropertyDataCollection.GetHandler = DiscoverExtensionMethods.GetProperty;
  30. }
for instance this extension methods
Code Snippet
  1. public static string TimeAgo(this PropertyData prop)
  2. {
  3.     TimeSpan dif = DateTime.Now - ((DateTime)prop.Value);
  4.     if (dif.Days > 0)
  5.         return string.Format("{0} day{1} ago ", dif.Days, dif.Days == 1 ? "" : "s");
  6.     else if (dif.Hours > 0)
  7.         return string.Format("{0} hour{1} ago ", dif.Hours, dif.Hours == 1 ? "" : "s");
  8.     else if (dif.Minutes > 0)
  9.         return string.Format("{0} minute{1} ago ", dif.Minutes, dif.Minutes == 1 ? "" : "s");
  10.     return string.Format("{0} second{1} ago ", dif.Seconds, dif.Seconds == 1 ? "" : "s");
  11. }

can be used with PageCreated like this

Code Snippet
  1. CurrentPage.Property["PageCreated"].TimeAgo();

or

Code Snippet
  1. PageCreated=<EPiServer:Property ID="PageCreated" PropertyName="PageCreated:TimeAgo" EnableViewState="false" runat="server" />

and will result in code like this

image

The extension method that exists in the demo template

image

can also be used like this

Code Snippet
  1. GetPreviewText=<EPiServer:Property ID="Property2" PropertyName="GetPreviewText(100)" EnableViewState="false" runat="server" />

and will render like this

image

I even made myself a help function, so if you use propertyname !help! you will get an overview of methods that exists

Code Snippet
  1. Help=<EPiServer:Property ID="Property1" PropertyName="!help!" EnableViewState="false" runat="server" />

 

image

Code is available in the code section

and any ideas where this can be used would be appreciated :)

Jan 26, 2011

Comments

Please login to comment.
Latest blogs
Decimal numbers in Optimizely Graph

Storing prices as decimal numbers on a commerce website and planning to expose them through Optimizely Graph? It might not be as straightforward as...

Damian Smutek | Jan 23, 2025 | Syndicated blog

Find and delete non used media and blocks

On my new quest to play around with Blazor and MudBlazor I'm going back memory lane and porting some previously plugins. So this time up is my plug...

Per Nergård (MVP) | Jan 21, 2025

Optimizely Content Graph on mobile application

CG everywhere! I pull schema from our default index https://cg.optimizely.com/app/graphiql?auth=eBrGunULiC5TziTCtiOLEmov2LijBf30obh0KmhcBlyTktGZ in...

Cuong Nguyen Dinh | Jan 20, 2025

Image Analyzer with AI Assistant for Optimizely

The Smart Image Analyzer is a new feature in the Epicweb AI Assistant for Optimizely CMS that automates the management of image metadata, such as...

Luc Gosso (MVP) | Jan 16, 2025 | Syndicated blog

How to: create Decimal metafield with custom precision

If you are using catalog system, the way of creating metafields are easy – in fact, you can forget about “metafields”, all you should be using is t...

Quan Mai | Jan 16, 2025 | Syndicated blog

Level Up with Optimizely's Newly Relaunched Certifications!

We're thrilled to announce the relaunch of our Optimizely Certifications—designed to help partners, customers, and developers redefine what it mean...

Satata Satez | Jan 14, 2025