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.