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

 

Per Nergård (MVP)
Nov 17, 2010
  4812
(0 votes)

Custom properties with scripts made easy

If you do custom properties in my experience they often include some java scripting to accomplish what you want.

Doing JavaScript in code behind isn’t so much fun to say the least.

I sure have done my fair share of massive one liners that’s hard to maintain and if you touch them after 11pm you’re going to break them.

Splitting the scripts on several rows is more readable but messes up your code with endless lines of string concatenation.

So what to do? Virtual path providers and embedded resources is the solution.

Allan Thraen has done an excellent job with a single assembly self registrating virtual path provider which you can get here. It’s compiled for CMS5 but it works for CMS6 as well.

With this it’s a breeze to use embedded resources.

In this case add a new JavaScript file to your solution, right click and set it’s build action to embedded resource.

Now we have a nice place where we can place all our scripts nicely formatted and commented.

To read the script file contents you can use the following method. The only thing needed to change is the virtualpath variable which should should point to your resource. If unsure of the path check it out with reflector.

 

   1: protected virtual string LoadJavaScriptFunctions()
   2:         {
   3:             string virtualPath = "/App_Resource/Nergard.EPi.dll/Nergard.EPi.SpecializedPropertis.Scripts.CollapsableCMS6.js";
   4:             string input = string.Empty;
   5:             TextReader reader = null;
   6:             try
   7:             {
   8:                 reader = new StreamReader(VirtualPathProvider.OpenFile(virtualPath));
   9:                 input = reader.ReadToEnd();
  10:                 input = Regex.Replace(input, @"/\*[\w\W]+?\*/", string.Empty);
  11:                 input = "<script type=\"text/javascript\">\n" + input + "\n</script>";
  12:             }
  13:             catch (Exception exception)
  14:             {
  15:                 throw new Exception(string.Format("Error \"{0}\" when editor tried to read file \"{1}\"", exception.Message, virtualPath), exception);
  16:             }
  17:             finally
  18:             {
  19:                 if (reader != null)
  20:                 {
  21:                     reader.Close();
  22:                 }
  23:             }
  24:             return input;
  25:         }

Voila you now have a custom property with JavaScript that’s easy to maintain.

Nov 17, 2010

Comments

Nov 17, 2010 10:31 AM

Did you consider web resources as an alternative to this (http://support.microsoft.com/kb/910442)?

Nov 17, 2010 11:16 AM

No id didn't. My initial thought was single assembly custom properties. So that they are self contained and can be dropped adn used at different customers in any combination with no hassle.

If I'm not mistaken webresources needs some configuration for them to work and would then introduce som hassle wich I was trying to avoid.

Please login to comment.
Latest blogs
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

Introducing AI Assistance for DBLocalizationProvider

The LocalizationProvider for Optimizely has long been a powerful tool for enhancing the localization capabilities of Optimizely CMS. Designed to ma...

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

Order tabs with drag and drop - Blazor

I have started to play around a little with Blazor and the best way to learn is to reimplement some old stuff for CMS12. So I took a look at my old...

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

Product Recommendations - Common Pitfalls

With the added freedom and flexibility that the release of the self-service widgets feature for Product Recommendations provides you as...

Dylan Walker | Jan 14, 2025