Monday, March 19, 2012

Querying Active Directory from a CRM 2011 Form

Technorati Tags: ,,

How do you query Active Directory from a CRM 2011 form? The typical scenario is this. The user enters the username of a domain user. The system has to validate if the username is a valid AD user and respond appropriately.

My first instinct was to write a javascript web resource that connected to Active Directory and did the necessary checks. But on second thoughts, I recollected that Dynamics CRM did something similar when you add a user. I wanted to re-use as much of the code in CRM as possible and decided to check out how it was done in the New User form.

IE Developer toolbar was my best friend for this. I opened up the new user form in CRM 2011 and found the id of the user name field.

image

The next step was to search in the scripts what function was firing on domain name change.

image

After due validations, the function was instantiating a RemoteCommand object and making a call using that object to get the user properties.

The remote command is an object in the Global.ashx handler and luckily, this handler is loaded on every CRM form. Once I had made that assertion, it was a matter of writing the javascript to pass in the value from the field in my form and doing the same checks as the CRM script.

My code looks something like this

   1:       var oCommand=new RemoteCommand("UserManager","RetrieveADUserProperties");
   2:       if(oCommand!=null)
   3:        {
   4:                oCommand.SetParameter("domainAccountName",userName);
   5:                var oResult=oCommand.Execute();
   6:                if(oResult.Success&&!IsNull(oResult.ReturnValue)&&oResult.ReturnValue.length>0)
   7:                {
   8:                      var firstName = "";
   9:                      var lastName ="";    
  10:                      for(var oUserXmlDoc=loadXmlDocument(oResult.ReturnValue),oNodeList=oUserXmlDoc.documentElement.childNodes,i=0;i<oNodeList.length;i++)
  11:                      {
  12:                           var oNode=oNodeList.item(i);
  13:                           if (oNode.tagName == "firstname")
  14:                           {
  15:                                firstName = oNode.text;
  16:                           }
  17:                          else if(oNode.tagName == "lastname")
  18:                          {
  19:                                lastName = oNode.text;
  20:                          }
  21:              }
  22:                 }
  23:           }    



I use the RemoteCommand I discussed earlier and pass the username in question as a parameter. The return from the function is an XML with the properties of the AD User. The name of the property is the tag of the node. So I check for the properties I want and get the text of the node. As simple as that!!


Why do I like this approach? Because as a developer, I don’t have to be worried about which AD instance to connect and the permissions to connect to the AD from my code. CRM does most of the heavy lifting for me. I don’t have to worry about security or exposing my AD credentials.


This approach is not without its own pitfall. This approach doesn’t work if you want to invoke this from within a HTML web resource. Since a HTML web resource is always contained with an IFrame calling the CRM methods becomes  a challenge.

Friday, March 9, 2012

Windows 8–Day 1

It is probably no news that Windows 8 Consumer Preview is out.I have been equally excited to try this out and I thought it wouldn’t be a bad idea to pen down my thoughts on my experience with Windows 8 as I go along.

If you have just downloaded the bits and are scratching your head on how some of these things work, do check out this blog. It has some useful tips on how to get around Windows 8. I will also share tips & tricks that I have found useful at the end of this post.

The Windows 8 installation by itself was a breeze. I have it on a machine with 2 GB RAM with a 64 bit AMD processor. I also love the fact that I can switch between the desktop view and the metro view. There are two distinct purposes of these views, the way I see it. The desktop is the productive side of Windows 8  and the metro is the fun side. It might be a bit of a hard landing if you are trying to be completely productive with the metro i.e Don’t try and do everything you do on a PC from the metro.

The next thing I did was to configure my email accounts using the Metro Mail app. Below is a snapshot of how the mail app looks. The list of emails is on the left and when you select an email it displays the body on the right and also enables action you can take on the top right.

MailView

You can switch between accounts by right click or from the app bar. Have Skype, Live writer and Lync. and not had any issues so far. There is also a messaging app in metro which I use with my live id. It enables me to chat with my facebook friends too. It displays all the conversations you have with your contacts as individual threads so no more switching tabs. You can also read your facebook updates from the same app!!!

Messaging

I  managed to install VS 2011 and though I haven’t got around to extensively using it , the installation itself did not have any issues. I am hoping I will get to use it more in the coming days. Now to keep up my promise of sharing some tips that I have come across

  • If you want to close an app try using Alt + F4. Personally, I have found that to be the easiest way to close an app.
  • If you want to look at all the windows or apps you have open, hover your mouse on the top left. That will bring up a task bar like the one shown in the snapshot. You can close the window from here too

AppsList

  • Don’t go looking for the run command. The whole desktop now functions as the run command. Switch to the metro view and just type. That serves as search and the run command
  • You don’t need to go into control panel to uninstall an app. Use Windows + Z and click on all apps. Right click and app and you will have the option to uninstall in the app bar.

UnInstall

What do you think about Windows 8? I would love to know what your thoughts are. Leave a comment on what you like or don’t like. Happy metroing Smile