Adding MS CRM to your own windows forms applications

I was asked to create simple demo that demonstrates MS CRM inside custom .NET application. So I created small win app with just couple of controls to make the demo pretty simple.

If user presses Edit customer button then new form is opened and that will host CRM form like this:

And if user then clicks Save and Close this newly created form is automatically closed and user is returned to the previous form.

Let's see the code that is used to make this happen. First we need to retrieve entity and get it's unique id. Here is example of that part for the account entity (I used my helper class and ripped also all the other things that aren't mandatory to understand this example):

 account account = crm.GetAccountByField("telephone1", texPhoneNumber.Text);if (account != null){  url = "http://danubecrm:5555/sfa/accts/edit.aspx?id={" + account.accountid.Value.ToString() + "}";}else{  MessageBox.Show("Account not found!", "Not found", MessageBoxButtons.OK, MessageBoxIcon.Error);}

So in line 4 we have URL for editing that specific account. Now we need to way to host in in our form. This part is actually pretty nice and easy since we can use WebBrowser component to make this happen. But now we need to add code to actually notice that has the user already pressed Save and Close since then we want to close our form. It can be achieved by creating timer that check the URL of the WebBrowser component. We'll just monitor it's value and when it's null we know that user has exited current page. We start the timer when document has been first completed. Here's the code:

     private void browserTimer_Tick(object sender, EventArgs e)    {      if (browser.Url == null)      {        this.Close();      }    }    private void browser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)    {      browserTimer.Enabled = true;    }

This is simple demonstration how to use CRM inside your application. Of course you could do all that stuff through CRM API but if you want to re-use the form from CRM this could be way to achieve that.

Anyways... Happy hacking!