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

This is a follow up to my previous post Microsoft Dynamics CRM 2011 Online Integration Getting Started (Late Bound). Here I list the steps that I had to do different to work with early binding.

After creating the project, I ran CrmSvcUtil.exe:

<location of sdk>\bin\CrmSvcUtil.exe /url:https://xxx.api.crm4.dynamics.com/XRMServices/2011/Organization.svc /out:”C:\Users\henols02\Documents\Visual Studio 2010\Projects\<project name>\Models\CrmModel.cs” /namespace:<project name>.Models /serviceContextName:CrmServiceContext /username:”<Windows Live ID” /password:”<password>”

and added the resulting CrmModel.cs to the project in the Models folder.

After creating the service proxy (in LogOn method), early bound support had to be added using the following statement:

serviceProxy.ServiceConfiguration.CurrentServiceEndpoint.Behaviors.Add(new Microsoft.Xrm.Sdk.Client.ProxyTypesBehavior());

In the created controller, getting the context and retrieving the data is done in a slightly different (simpler) way:

var context = new Elevregister1.Models.CrmServiceContext(service);
var elever = context.elev_elevSet.ToList();

I created the view as a strongly-type view:

image

Then, I removed the unwanted columns.

Advertisements

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>

PowerShell Script for Web Request

I wanted to schedule a “ping” web request to a web application for “warm up” every morning. It turned out to be relatively simple using PowerShell. I created this script and called it Ping.ps1:

$url = “https://…&#8221;
$wc = new-object net.WebClient
$probe = $wc.downloadData($url)
$s = [text.encoding]::ascii.getString($probe)
$s | Out-File -filepath “D:\Logfiles\PingResult.htm”

Then I created a new task in Task Scheduler with the following parameters:

  • Action: Start a program
  • Program/script: powershell
  • Arguments: C:\scripts\Ping.ps1

image