Monday, June 14, 2010

My First F# App

I wanted to learn the basics of F# so i wrote a little app to organize my movie folder.  My movie folder was a folder that contained a bunch of movies.  I wanted to organize the movies alphabetically into folders with the first letter being the name of the folder.  After getting used to the syntax of F#, i found writing this little app to be just a easy as C#.
Here is the code:
Code Snippet
  1. #light
  3. open System
  4. open System.IO
  6. let title : string = "My Movie Organizer"
  8. Console.WriteLine(title)
  10. Console.WriteLine("This Application will move all files in a folder to subfolders with the first letter as the folder name.")
  12. Console.WriteLine("Please enter the path of the folder you want to organize")
  13. let folderPath : string = Console.ReadLine()
  15. if folderPath <> String.Empty then
  17.     let dirInfo : DirectoryInfo = new DirectoryInfo(folderPath)
  19.     for x : FileInfo in dirInfo.GetFiles() do
  20.         let firstLetter : char = x.Name.Chars 0
  22.         let currentDirectory : DirectoryInfo = new DirectoryInfo(String.Format("{0}\\{1}\\", folderPath, firstLetter.ToString()))
  24.         //If the current folder exists, move the file there else create the folder then move th file
  25.         if currentDirectory.Exists then
  26.             let xFilePath : string = currentDirectory.FullName
  27.             x.MoveTo(xFilePath + x.Name)
  28.         else
  29.             currentDirectory.Create()
  30.             let xFilePath : string = currentDirectory.FullName
  31.             x.MoveTo(xFilePath + x.Name)        
  32.         //Write out the file name and path    
  33.         Console.WriteLine(String.Format("Moving File {0} to Folder {1} ", x.FullName, currentDirectory.FullName))
  34. else
  35.     Console.WriteLine("Please specify a base path")
  36. //Wait for key press to end
  37. let endValue : string = Console.ReadLine()
Being a C# developer, i couldn’t help but explicitly type my variables (although unnecessary).  The first couple of lines are just writing out some text and waiting for the user to enter a path to a folder the want organized.  Next, the base folder path is opened as a directory info object.  Then we loop through all of the files in the directory and grab the first letter of the file name.  After checking if the folder exists, we either move the file or create the new folder and move the file.  The readline at the end just waits for the user to enter a key to end the program.
This is a very simple app and by no means enterprise level with error checking ,etc.  Just a basic app to move some files around in F#.

Sunday, June 6, 2010

How to get the CrmService URL without using the registry

Yes, the Crm Service URL is available in registry but what if the client does not want your custom web page/site to have access the registry? Well here is a simple way to build the SDK URL from the Request object in a web page.


  • Your page lives in the CrmWeb/ISV folder
    • Which means you custom page will have the same URL as the SDK
  • If you are using an ISV page in a multi org deployment, be sure to use the prependOrgName function or manually append the org name to ISV URL
    • var sUrl = prependOrgName("/ISV/Ascentium/mypage.aspx”)
    • prependOrgName is an unsupported Crm Function.

First the URL Template and the URI Property:

private const string CRM_SDK_URL = "{0}{1}{2}:{3}/mscrmservices/2007/crmservice.asmx";

private Uri _currentUri;

/// <summary>
/// Gets the current URI.
/// </summary>
/// <value>The current URI.</value>
private Uri CurrentUri
if (_currentUri == null)
_currentUri = this.Request.Url;
return _currentUri;

Now we use string.Format to create the SDK URL from the URI and the Template:

private CrmService _service;
/// <summary>
/// Gets the current service.
/// </summary>
/// <value>The current service.</value>
private CrmService CurrentService
if(_service == null)
_service = new CrmService();
_service.CrmAuthenticationTokenValue = CrmAuthenticationToken.ExtractCrmAuthenticationToken(this.Context, "MY_ORG");
_service.Url = string.Format(CRM_SDK_URL, CurrentUri.Scheme, Uri.SchemeDelimiter, CurrentUri.Host, CurrentUri.Port);

return _service;

And that is it.  Now the SDK Url can be built dynamically from the Request. This method could be adapted to fit other scenarios as well.

Thursday, May 27, 2010

Creating a Lookup with Javascript in Crm 4

This is one on those things that i always forget how to do it so i am posting it. 

From the SDK:

//Create an array to set as the DataValue for the lookup control.
var lookupData = new Array();
//Create an Object add to the array.
var lookupItem= new Object();
//Set the id, typename, and name properties to the object. = '{1AAC1363-01A1-DB11-8432-0003FF9CE217}';
lookupItem.typename = 'account'; = 'A Bike Store';
// Add the object to the array.
lookupData[0] = lookupItem;
// Set the value of the lookup field to the value of the array.
crmForm.all.parentaccountid.DataValue = lookupData;

Friday, April 2, 2010

How to Create a New Guid with Javascript in Crm 4

Ever needed to create a new Guid on the fly with Javascript in Crm 4? There is actually a Javascript function that does just that.

  • Add a reference to CRMWeb\_static\Tools\FormEditor\Scripts\util.js (If necessary)
  • The function name is GetGuid()

The Code from util.js:

function GetGuid()
var sGuid = null;

var oCommand = new RemoteCommand("SystemCustomization", "GetGuid");
var oResult = oCommand.Execute();
if (oResult.Success)
sGuid = oResult.ReturnValue;
catch (e)
sGuid = null;

if (!sGuid)


return sGuid;

The function is used in Crm for creating a new import job and probably other things as well.  It is completely unsupported to use this function in your code, but imho better than using a third party JavaScript library to create the Guid.

Wednesday, March 17, 2010

Adding Multi processor support to a 32 bit Windows 2003 VirtualBox Image

Say you have an existing Windows 2003 R2 Virtual Machine with all of your favorite software installed.  When you installed the machine only had a single processor and now you are using a Virtual Environment that supports multiple processors.  Both processors will show up in the device manager but the OS is not using them (See task manager).  To remedy this issue, you need to replace the HAL.  After you complete Windows setup, there is not an out of the box way to replace the HAL.  This has only been tested on Windows 2003 R2 Sp2 but should work for all XP/Win2003 flavors. 

Disclaimer: I am not responsible for broken machines or nuclear war.  Information is provided as is.

Note: Requires the existing HAL be ACPI based or this will blow up your machine.

Retrieve the following HAL libraries from the Service pack folder (C:\WINDOWS\ServicePackFiles\i386) and rename them accordingly:

ACPI Multiprocessor PC
- halmacpi.dll (renamed to hal.dll)
- ntkrpamp.exe (renamed to ntkrnlpa.exe)
- ntkrnlmp.exe (renamed to ntoskrnl.exe)

· Drop the renamed files into the system32 folder and shutdown. 

· In VirtualBox’s Machine Settings, I enabled  IO APIC in the System > motherboard tab.

· On the Processor tab I changed the number of processors to 2

· Start the Virtual Machine

· Prompted me for a reboot after first log in

· After reboot, both CPU’s can be seen in the task manager.