Simple loading message to entertain user during background processes

There are a lot of ways this can be done, including using Ajax, Telerik controls or Update Panels, but with ASP.Net and Javascript, it can be very simple.  All you need is a server-side label, a simple css class and some simple jQuery.  First, your label with the loading text:

The css class is simple enough.  It just hides the label when we don’t want to see it.

Now, we can run some simple jQuery when the event occurs where we want to show the loading label (in this case,  a button click) to remove the css class from the element that is causing it to be hidden:

All we have to do now is hide the loading label by adding the appropriate css class after our work is finished on the server-side:

Easy, right?  I love easy!

Advertisements

Javascript and Dynamics CRM 2011

I was asked to develop some CRM customizations using only Javascript.  As anyone who has worked with Javascript knows, it can be a bit of a hassle to work with, so I have compiled a list of hurdles that I had to leap over to reach my goals for this project in the hopes that it will help someone else (or if nothing else, give me a place to refer to when I have to do it again some day)

I installed CRMFetchKit, which makes it very simple to execute FetchXML queries and act on the results.  It’s easy to use the CRM “Advanced Find” feature to build the list of data you need and export the XML.  From here, you just need to paste it into code and insert your variables.

After the results are returned, the id field is extracted using the same field name that is used in the attribute elements of the XML query.  In this case, the name and id are used to populate a lookup, which gets populated differently than a standard CRM text field.  Brackets are required for setting the id fields of lookups.  The relevant code is below:

 

 

 

 

 

 

I was tasked to create a custom lookup on the Address entity, which doesn’t allow creation of lookups.  I had to create an HTML web resource and spend days messing with CSS and Javascript to get it to display and act like a CRM lookup.  Typically, Xrm.Page.getServerUrl() would give you the root of your CRM site, but in my case it cut off the server our CRM instance was running on, so I replaced it with the following:

This was used to link to the record when the name of the selected lookup value is clicked, as is the behavior of a standard CRM lookup.

For calling the actual lookup dialogue, I created a function to encapsulate the method call so that I could get intellisense when determining what fields I would fill in when I calling it.

 

Here is the code I used to call the lookup using the above function.  The State lookup should be filtered to show only the states that belong to the selected Country.  If it’s just a standard lookup, only the lookupStyle and lookupTypes parameters are required.

For another requirement, the client wanted “cloning” functionality for an entity, to be called from a ribbon button click.  When the ribbon button is clicked, a new window should open for creating a new entity and the relevant fields should be populated.   Installing the CRM Visaul Ribbon Editor makes it a snap to add ribbon buttons.  I don’t know any other way to do it, but this seems to be very easy.  So there are basically three steps to the process.

1. Collecting the field values from the source record. The fields need to be treated differently depending on the field type, as usual

2. Define the values that will be populated into the destination record

3. Open the new form, passing the values to be populated (AGREEMENT is the entity type name (in our case, ‘iis_agreement’)

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

One last note – I used the trial version of JetBrains WebStorm for most of my Javascript development here, as it checks your variable names on the fly, whereas Visual Studio doesn’t.  So if you misspelled projectid in the code above (I did), you would have to wait until you run your test and use alerts or console.log writes to find out what went wrong. In WebStorm, there are a number of error checks you can enable, but you would notice it was the wrong color and fix your typo before it got that far.  Just trying to minimize your headaches, people.

Happy coding

Windows 8 Developer Camp

I spent most of this Saturday at the Windows 8 Developer Camp in midtown Manhattan, which seemed to have an endless supply of food and beverages to prevent people from feeling the need to leave the room.  I was glad to see some of the friendly faces from Infinity already there when I arrived.

The presenters spoke about the improvements in Windows 8, such as the live tiles that basically act as large shortcuts that can cycle through information (pictures or RSS feeds, for example) or be pushed information so you get notifications similar to how you would on a mobile device without having an actual pop-up.  A lot of new features have been added in terms of the new look of the start menu/start page, but anything that you currently run on Windows 7 will work on Windows 8 which is a big plus.

They talked about the app store and the requirements and best practices to have a successful app in the store, which seems to work similar to the app store works on an Android or iPhone.  At the minimum, they said a metro app in the app store needs to have section in the settings for rating and reviewing the app, as well as for permissions to control of the device.  They mentioned that if an app takes more than 10 seconds to load, the runtime will immediately close it, so performance optimization seems to actually be enforced in Windows 8.   Settings are accessed from the same place regardless of the application, similar to how it would be on iOS or a linux machine.

The search feature looked great, as you can enter a search term and it is content-centric, rather than being application-centric, as they put it.   You can enter a search term and then scroll through different contexts including settings, files, apps or the web.  Content can easily be shared between applications, whether it be text, an image, a file or otherwise and the application will be aware of how it was activated with ActivationKind enumeration, whether it be launched, searched or shared.

The “Metro” UI is the new look and feel of windows desktops and tablets alike.  They emphasized that “Windows 8 is a touch-first OS”.  “Code for touch and you get the pen and mouse for free”, they said.  Their point was that you can develop as if the user is touching the screen on a tablet and the mouse and stylus functionality should just work.  However, someone asked a good question about how the “hover” event would be treated if the user was on a tablet and not a desktop and it seemed apparent that there will be some need to differentiate between the input type, (whether it be touch, mouse or stylus) and act accordingly.

Metro apps that are not actively focused on the screen are set to the “suspended” state and will consume minimal battery, cpu cycles or other resources.   The “SuspensionManager” works similar Session state on web sites and web applications, where objects can be stored for later use in the runtime.  There are some exceptions to what a suspended app can do, such as playing sound or completing file transfers in the background.  They seem to be trying to get away from a program just sitting in the background consuming resources.

Apps are written in XAML and Javascript.  The XAML editor in Visual Studio 2012 was improved, so they say but I didn’t notice any major differences.  I’m sure they improved the intellisense support, if nothing else.  Javascript code will now show intellisense for methods that it can call from the code behind.  In the example, the class that was being called from Javascript was sealed.  WinRT will be used for Windows 8 applications and will form a layer between the code and the Windows core OS services.  P-Invoke external calls will no longer be made and instead calls from externally assemblies will be handled by WinRT.

The “async” method type has been added to encapsulate the code required to run an operation asynchronously and is used in conjunction with the new “await” keyword.  This way, it will be very easy to write code where you don’t lose time waiting for an operation to complete entirely before a second operation begins to run.  Behind the scenes, a state machine is created (like IEnumerable) and basically an operation can be called recursively for each object in a collection as they are being loaded (or otherwise acted upon).  

Microsoft seems to want to bridge the gap between a “desktop” app and a “mobile” app with Windows 8.  From the perspective of a spoiled user that doesn’t want to wait for anything no matter what device I am using, I’m all for this approach.  As much as an improvement Windows 7 was on Vista, Windows 8 is taking the way we interact with our UI in a new direction – seemingly in the direction of a smartphone UI buzzing with fresh information and quick ways to jump around to new tasks fluidly.  I’m looking forward to developing for this new platform.

All slides, labs and code for the camp can be downloaded from here