Microsoft Dynamics CRM 2011 Online Integration Getting Started (Late Bound)

This is a log of what I did to create a very simple ASP.NET MVC application that retrieves data from Microsoft Dynamics CRM 2011 Online. I had a custom entity called Elev.

1. Created an ASP.NET MVC 3 Web Application with template Internet Application.

2. Added references to

  • microsoft.crm.sdk.proxy
  • microsoft.xrm.sdk
  • System.ServiceModel
  • System.Runtime.Serialization
  • System.Security.

3. Created a new folder called Helpers and add <location of sdk>\samplecode\cs\helpercode\deviceidmanager.cs to it.

4. Modified AccountController LogOn to create a CRM service proxy and store it in the user’s session:

public ActionResult LogOn(LogOnModel model, string returnUrl)
if (ModelState.IsValid)
//if (Membership.ValidateUser(model.UserName, model.Password))
FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);

// Added for CRM
var clientCredentials = new System.ServiceModel.Description.ClientCredentials();
clientCredentials.UserName.UserName = model.UserName;
clientCredentials.UserName.Password = model.Password;
var serviceProxy = new Microsoft.Xrm.Sdk.Client.OrganizationServiceProxy(
new Uri(“;),
Session.Add(“ServiceProxy”, serviceProxy);
// End added for CRM

if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith(“/”)
&& !returnUrl.StartsWith(“//”) && !returnUrl.StartsWith(“/\\”))
return Redirect(returnUrl);
return RedirectToAction(“Index”, “Home”);
catch (Exception ex)
//ModelState.AddModelError(“”, “The user name or password provided is incorrect.”);
ModelState.AddModelError(“”, ex);

// If we got this far, something failed, redisplay form
return View(model);

5. Modified AccountController LogOff as follows:

public ActionResult LogOff()
var serviceProxy = Session[“ServiceProxy”] as Microsoft.Xrm.Sdk.Client.OrganizationServiceProxy;
if (serviceProxy != null)


return RedirectToAction(“Index”, “Home”);

6. Created a new controller with empty read/write actions called ElevController.


7. Added the following code:

public ActionResult Index()
var service = Session[“ServiceProxy”] as Microsoft.Xrm.Sdk.IOrganizationService;
if (service != null)
var context = new Microsoft.Xrm.Sdk.Client.OrganizationServiceContext(service);
var query = from e in context.CreateQuery(“elev_elev”) select e;
var elever = query.ToList();
return View(elever);
ModelState.AddModelError(“”, “ServiceProxy missing in session state. Did you log in?”);
return RedirectToAction(“LogOn”, “Account”);

8. Added a new folder in Views called Elev.

9. Added a new view called Index:


10. Added the following code:

@model IEnumerable<Microsoft.Xrm.Sdk.Entity>

ViewBag.Title = “Index”;


@Html.ActionLink(“Ny”, “Create”)

@foreach (var item in Model) {
@Html.DisplayFor(modelItem => item[“elev_name”])
@Html.DisplayFor(modelItem => item[“elev_personnummer”])
@Html.ActionLink(“Redigera”, “Edit”, new { id=item.Id }) |
@Html.ActionLink(“Detaljer”, “Details”, new { id = item.Id }) |
@Html.ActionLink(“Ta bort”, “Delete”, new { id = item.Id })


11. Added a link on the Home/Index page:

<p>@Html.ActionLink(“Elever”, “Index”, “Elev”)</p>


10 thoughts on “Microsoft Dynamics CRM 2011 Online Integration Getting Started (Late Bound)”

  1. Hi, I came across your blog post and found exactly what I was looking for…running a MVC framework based custom app within CRM. It will be really great if you can share some sample project code on this blog post or via email ->

  2. Thank you very much! That’s what I am looking for! We would like to create a CRM portal by MVC! Thanks for shaing:)

  3. Thanks! here my search ends with proper results , i really thank you for this smart code snip. And i’ll be very happy if you could add some crude operation base MVC code snip that can solve our real mess.

    Thanks Once again!!

  4. Note that in my code, I create a CRM service proxy in LogOn and store it the user session. That might not be recommended practice. The proxy should probably be created for every request, but that requires the credentials to be stored in the session instead.

  5. When I initially commented I clicked the “Notify me when new comments are added” checkbox and now each time a comment is added I get several e-mails with the same comment.
    Is there any way you can remove people from that service?
    Appreciate it!

  6. Hi, I get a build error on this line: new Uri(“”)
    Is it really supposed to start with a quote and end with a semicolon?

    Thanks for this demo!

    1. No, it should of course be
      new Uri(“”)
      I don’t know why WordPress displays it incorrectly, because when I look at the source for the post it looks exactly as in this reply.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s