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:

[HttpPost]
public ActionResult LogOn(LogOnModel model, string returnUrl)
{
if (ModelState.IsValid)
{
//if (Membership.ValidateUser(model.UserName, model.Password))
try
{
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(“https://xxx.api.crm4.dynamics.com/XRMServices/2011/Organization.svc&#8221;),
null,
clientCredentials,
Microsoft.Crm.Services.Utility.DeviceIdManager.LoadOrRegisterDevice());
Session.Add(“ServiceProxy”, serviceProxy);
// End added for CRM

if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith(“/”)
&& !returnUrl.StartsWith(“//”) && !returnUrl.StartsWith(“/\\”))
{
return Redirect(returnUrl);
}
else
{
return RedirectToAction(“Index”, “Home”);
}
}
//else
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)
{
Session.Remove(“ServiceProxy”);
serviceProxy.Dispose();
}

FormsAuthentication.SignOut();

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

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

image

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);
}
else
{
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:

image

10. Added the following code:

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

@{
ViewBag.Title = “Index”;
}

<h2>Index</h2>

<p>
@Html.ActionLink(“Ny”, “Create”)
</p>
<table>
<tr>
<th>
Namn
</th>
<th>
Personnummer
</th>
</tr>

@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item[“elev_name”])
</td>
<td>
@Html.DisplayFor(modelItem => item[“elev_personnummer”])
</td>
<td>
@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 })
</td>
</tr>
}

</table>

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

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

Advertisements

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 -> sp22403@yahoo.com?

  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(“https://xxx.api.crm4.dynamics.com/XRMServices/2011/Organization.svc”)
    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(“https://xxx.api.crm4.dynamics.com/XRMServices/2011/Organization.svc”)
      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:

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s