I am really proud that my first .NET Magazine article is publiced.
I wrote this article with my co-worker Dennis van de Laar. The topic is AJAX and Javascript.


Dutch .NET Magazine #22


 Ajax is niet meer weg te denken

It is a small download, it''s PDF and it is Dutch 

Henry Cordes
My thougts exactly...


External js file and CRM

Published 5/27/2008 by Henry in AJAX | CRM | Javascript

While doing a project where MS Dynamics CRM  is used a lot of customizations are performed by JavaScript.
Usually the way to it is to perform some JavaScript actions in the OnLoad of the Page.
MS Dynamics CRM has a extention point, where you can control the OnLoad of Detail Forms by entering JavaScript.

Now when you need to deploy your CRM configuration to more than one system (like we do at my project, it is sold as part of a product), you want to use a centralized Javascript file so you can change your url's etc. all in one place.
To do this (unsupported by Microsoft!) I learnt the following technique from CRM Specialists:

First technique

   1:  var script = document.createElement('script');
   2:  script.language = 'javascript';
   3:  script.src = '/_customscript/customscript.js';
   4:  script.onreadystatechange = OnScriptReadyState;
   5:  document.getElementsByTagName('head')[0].appendChild(script);
   6:   
   7:  function OnScriptReadyState()
   8:  {
   9:      if (event.srcElement.readyState == 'complete')
  10:     {
  11:          // Perform onload script
  12:          //Doit();
  13:      }
  14:  }

Listing 1

The drawback with this technique is that the first time CRM loads (and every time the cache is empty) the script is not executed. Leaving the user to think the application does not work. After some time it really annoyed me, so I started to ask uncle Google again for a solution. I found the following on http://blog.odynia.org/archives/1-Javascript-Includes.html.

What this guy does is doing an AJAX call, to get the js file.
Next he loads the javascript as a string, eval() it, and imports all functions found into the current namespace, so you can access them.

It needs functionnames a-z, it cannot handle numeric values in the name of the function, but i will fix this before I will use it.
Otherwise I think it rocks! Async technique (no first time drawback)

   1:  function load_script (url) 
   2:  { 
   3:      var x = new ActiveXObject("Msxml2.XMLHTTP"); 
   4:      x.open('GET', url, false); 
   5:      x.send(''); 
   6:      eval(x.responseText); 
   7:      var s = x.responseText.split(/\n/); 
   8:      var r = /^function\s*([a-z_]+)/i; 
   9:      for (var i = 0; i < s.length; i++) 
  10:      { 
  11:          var m = r.exec(s[i]); 
  12:          if (m != null) 
  13:          {
  14:              window[m[1]] = eval(m[1]); 
  15:          }
  16:      } 
  17:  } 
  18:   
  19:  load_script("/_customscript/customscript.js"); 
  20:   
  21:  //perform onload scripts
  22:  //DoIt();

Listing 2

Addition:

As I mentioned numbers in the functionname caused the code to fail. So I changed the regex pattern in line 8 from listing 2 into:

   1:  var r = /^function\s*([a-zA-Z_0-9]+)/i; 

Listing 3

With this regex pattern functions with numbers in the name also are added to the namespace. I added the uppercase A-Z not because functions with uppercase characters in the name where not added, but as a best practice. also you can never be sure browsers keep on using IgnoreCase as default setting.

As you can read in the comments, Marc-Andre uses the following pattern:

   1:  var r = /^(?:function|var)\s*([a-zA-Z_]+)/i; 

He wants some vars (which he uses as constants) to be added to the namespace also, maybe I would add the 0-9 here also. anyway, I think it is a good suggestion to mention here.

Addition 2:

Steve Le Mon made a very good suggestion and tried out a few things, he found a way around the parsing of the functions and/or vars and adding them to the current namespace.
I tweaked his code a little bit and ended up with the following:

   1:  function InjectScript(scriptFile)
   2:  {
   3:      var netRequest = new ActiveXObject("Msxml2.XMLHTTP"); 
   4:      netRequest.open("GET", scriptFile, false); 
   5:      netRequest.send(null); 
   6:      eval(netRequest.responseText); 
   7:  }
   8:   
   9:  InjectScript('/_customscript/customscript.js');
  10:   
  11:  //CallFunctionInExternalFile(); 

Listing 4

This technique removes the overhead of the parsing of the functions and vars so will perform faster.

Henry Cordes
My thoughts exactly...


My fellow-avanade dude Dennis pointed me out the following site:
Lost in Tangent

On this site there are lots links to of tutorials, walkthroughs of the ADO.NET Data Services (formerly "Astoria"). Astoria was a project from Microsoft's 'Data team'. A team that works on all data related stuff in and for the .NET Framework, like the Entity Framework. The ADO.NET Data Services is the name that is now used for project 'Astoria', after a few CTP's now the ASP.NET 3.5 Extensions preview release contains the first production release. With these Extensions Microsofts paves the way for more 'in-between' releases. We do not always have to wait for a major .NEt Framework release that contains new functionality. The ASP.NET AJAX Framework had the premiere with this in-between release methodology. It was a success and with the .NET Framework 3.5 just released, ADO.NET Data services is here with some more goodies.
These services are a combination of patterns and libraries that enables any data store to be exposed as a data service, naturally integrating with the Web. Also these services can be consumed by Web clients. It is built making heavy use of the Entity Framework. 

I quote: "ADO.NET Data Services uses URIs to point to pieces of data and simple, well-known formats to repirst production release.resent that data, such as JSON and ATOM/APP. This results in data being exposed to Web clients as a REST-style resource collection, addressable with URIs that agents can interact with using standard HTTP verbs such as GET, POST, or DELETE."

I played with it and it was remarkebly easy to get data on the client using Javascript and because JSON is a supported format, it is easy to get these objects directly from the server tier. I think this could be a really helpfull if you want to create Client-centric AJAX Webapps. The use of formats like RSS, JSON and Atom is brilliant in simplicity. The ADO.NET Data Services are released in the ASP.NET 3.5 Extensions preview release.

If you are interested in what it is all about, take a look here

Henry Cordes
My thoughts exactly


Microsoft Press have just released an e-book on Visual Studio 2008 technologies and are giving it away for free. The e-book includes excerpts from three recent book releases and provides a wealth of information and insights from top experts:

Look here

Henry Cordes
My thoughts exactly...


This edition of the DevDays was totally in the light of XAML, WPF (Windows Presentation Foundation), WCP (Windows Communication Foundation) and last but not least: Silverlight. The last few editions where always about new technology, this year Microsoft tried to listen to the community.  With the result that they also put in some sessions on todays technology and how to make good use of it. Think AJAX, Asynchronous ASP.Net Pages and Web Services.

In the Keynote we saw Scott Guthrie who showed us Silverlight and Microsoft Expression.
We saw an application called: 'Top Banana', which is a video editing application in the browser. It is impressive technology, I know we already have Flash, but Silverlight integrates the .NET framework and supports ASP.NET AJAX. I don't know if you ever worked with Flash and .NET (through remoting and actionscript), than I think you can appreciate this integration.

I saw a lot of WPF and WCF. I see the advantages a lot can be daone declarative through XAML.
In WPF the UI (User Interface) can be made in XAML and a lot can be nested (a button can contain a movie, or a listbox etc.). Your own objects can be accessed in XAML if you addd your own namespace in the root element of the XAML Document:
xmlns:u="clr-namespace:NAMESPACE"
If you want to try you can use 'Visual Studio ORCAS', which in the future will be 'Visual studio 2008', but if you want to use a more 'drag and drop' approach building a UI, use 'Blend'. If you really want to know more go here: http://wpf.netfx3.com/. Animations are supported in XAML, this means declaritive animations! Databinding support, WPF supports databinding.


XBAP
I heard about XAML Browser Applications(XBAPs), are WPF applications you can run in the browser, these apps are deployed using the 'Click once' deployment technique.
The advantages are:

  • Are not installed  
     – No Start Menu or Add/Remove Programs presence  
     – Use ClickOnce for deployment  
  • Run in the browser  
     – Familiarity of web browsing  
  • Are “online only” apps  
     – Require access to the  
     deployment location  
     – Always latest version  
  • Run in a security sandbox  
     – Have no security or installation prompt  


These Devdays, I became a great fan of Mark miller, he works with Devexpress and I think Coderush is an app he worked on.
He is a great performer with something to tell, this guy keeps your interest the whole session. The first day I attended his 'Rockin' & Rollin' with WCF and WPF' session, he built an Airhockey client-server App with WPF and WCF. He showed a lot about how this app is built and he also entertained the audience real well. The second day the last session I attended was Mark Miller's 'The Science of a Great UI' session. He really had a big audience, a lot of people wanted to see this session. Unless the blue screen of death it was a interesting and entertaining session.
I had a great time.


Henry Cordes
My thoughts exactly...