Tag Archives: CRM

Using an ERD tool to create CRM entities

I get asked this now and then:

“I want to use Erwin [or Visio or Visual Studio or…] to graphically design the CRM entity model, then have the actual CRM entities generated off of the tool I used to design the entities.”

While I am not aware of a direct way to do this with any ERD tools, there is a multi-step process you can use as long as your ERD designer can generate a SQL or Access database:

  • Use your ERD designer to model your entities, fields and relationships
  • Generate a SQL or Access database from the designer
  • Use https://xrmspeedy.codeplex.com/ to generate the CRM entities from the database

Of course, you’ll need to make sure you don’t pick data types in your diagram that the xrmspeedy tool can’t translate to CRM types, but this approach can be used as a productivity enhancement to those who prefer to diagram out their entities and generated them.  Since this is an open source tool, you might consider contributing feedback if you have ideas for improvement.  After a little trial and error, I’ve found I get value out of using Visual Studio’s Entity Framework Designer plus this tool when brainstorming data models and building POCs.  Your Mileage May Vary (YMMV).

@devkeydet

Batch updating fields with crmsvcutil generated code

Scenario:

“I love early bound code because of compile time checking, LINQ query enablement, etc.  I want to batch update a bunch of records, but OrganizationServiceContext.SaveChanges() executes under the hood as one web service call per entity.  How do I batch update?  Also, how do I make sure that only the fields I change are updated?”

The answer is that you basically have to combine your crmsvcutil.exe generated code with ExecuteMultipleResults.  Here’s an example:

var conn = CrmConnection.Parse("USE A VALID connection string using appropriate format documented at https://msdn.microsoft.com/en-us/library/jj602970.aspx");
var ctx = new marcsctest1Context(new OrganizationService(conn));

// NOTE: The query below uses LINQ projection.  The CRM LINQ provider will translate this to a query that only returns ID & Name
var query = 
    from a in ctx.AccountSet
    select new Account
    {
        Id = a.Id,
        Name = a.Name
    };

var executeMultipleRequest = new ExecuteMultipleRequest()
{
    Settings = new ExecuteMultipleSettings(){
        ContinueOnError = false,
        ReturnResponses = true
    },
    Requests = new OrganizationRequestCollection()
};

foreach (var account in query)
{
    account.Name += " - Updated";
    //UpdateObject is required to set the EntityState to Changed because it is readonly through crmsvcutil generated entities
    ctx.UpdateObject(account); 

    executeMultipleRequest.Requests.Add(new UpdateRequest()
    {
        Target = account
    });
}

//Don't forget to check response.IsFaulted and handle appropriately
var response = ctx.Execute(executeMultipleRequest) as ExecuteMultipleResponse;

@devkeydet

Using LINQPad with the CRM Online OData service

I’ve been a longtime fan of LINQPad (www.linqpad.net) as a general purpose tool for executing LINQ queries with various LINQ enabled technologies as well as a general purpose scratchpad for testing out .NET code without having to fire up Visual Studio.  I’ve already blogged about how to use LINQPad to work with the CRM Online SOAP service:

http://blogs.msdn.com/b/devkeydet/archive/2012/12/04/linqpad-crm-2011-and-using-office-365-accounts.aspx

One thing that may not be obvious is how to use LINQPad to query the OData service that comes with Dynamics CRM Online.  Using the same basic concept that I showed with the SOAP service, I created a video walkthrough of how to interact with the OData service from LINQPad:

 

Here’s the LINQPad starter code I showed in the video:

var clientId = "[YOUR_CLIENT_ID]";
var resource = "https://[YOUR_INSTANCE].crm.dynamics.com";
var redirectUri = new Uri("http://linqpad"); //replace with your redirect uri

var authContext = new AuthenticationContext("https://login.windows.net/common/oauth2/authorize", false);
var authResult = authContext.AcquireToken(resource, clientId, redirectUri, PromptBehavior.Auto);
Util.ClearResults();

var ctx = new marcsctest1Context(new Uri(resource + "/XRMServices/2011/OrganizationData.svc/"));

ctx.SendingRequest2 +=
 (o, eventArgs) => eventArgs.RequestMessage.SetHeader("Authorization", "Bearer " + authResult.AccessToken);

var query = 
     from a in ctx.AccountSet
     where a.Name.Contains("sample")
     select new
     {
          a.AccountId,
          a.Name,
          a.Address1_City,
          a.Address1_StateOrProvince
     };

query.ToString().Dump("OData Query Url");
query.Dump("Query Results");

One of the things I did, after creating the video, was to write a reusable helper method in the LINQPad MyExtensions class so I can reuse the code to get the access token across multiple LINQPad query sessions.  Here’s the code for the extension:

void Main()
{
     // Write code to test your extensions here. Press F5 to compile and run.
}

public static class MyExtensions
{
     // Write custom extension methods here. They will be available to all queries.
     static string LINQPadAccessToken = null;
     
     public static string GetAccessToken()
     {
          if (LINQPadAccessToken != null)
          {
               return LINQPadAccessToken;
          }
          
          var clientId = "[YOUR_CLIENT_ID]";
          var resource = "https://[YOUR_INSTANCE].crm.dynamics.com";
          var redirectUri = new Uri("http://linqpad"); //your redirect uri

          var authContext = new AuthenticationContext("https://login.windows.net/common/oauth2/authorize", false);
          var authResult = authContext.AcquireToken(resource, clientId, redirectUri, PromptBehavior.Auto);
          Util.ClearResults();
          
          LINQPadAccessToken = authResult.AccessToken;
          
          return LINQPadAccessToken;
     }     
}

Here’s a version of the code in my query tab that is simplified, after writing and leveraging the helper method:

var ctx = new marcsctest1Context(
     new Uri("https://[YOUR_INSTANCE].crm.dynamics.com/XRMServices/2011/OrganizationData.svc/")
);
ctx.SendingRequest2 += 
     (o, eventArgs) => eventArgs.RequestMessage.SetHeader("Authorization", "Bearer " + MyExtensions.GetAccessToken());

var query =      
     from a in ctx.AccountSet
        where a.Name.Contains("sample")
     select new
     {
          a.AccountId,
          a.Name,
          a.Address1_City,
          a.Address1_StateOrProvince
     };

query.ToString().Dump("OData Query Url");
query.Dump("Query Results");

Note the call to MyExtensions.GetAccessToken() above.  You could obviously refactor the GetAccessToken() method more to make it even more reusable, but I will leave that as an exercise for you.

This approach will become even more powerful once Dynamics CRM introduces the new OData v4 service endpoint:

https://msdn.microsoft.com/en-us/dynamics/crm/webapipreview

As I mention in the video, once that happens, you’ll want to perform the codgen with the OData Client Code Generator instead of “Add Service Reference”:

http://blogs.msdn.com/b/odatateam/archive/2014/03/11/how-to-use-odata-client-code-generator-to-generate-client-side-proxy-class.aspx

@devkeydet

Setting up a Microsoft Cloud trial

CRM Online based solutions often run across the broader Microsoft Cloud, not just CRM Online alone.  As they say, a picture is worth a thousand words.  So here’s a picture that hits home this point at a high level.

image

To help people get started trying this environment out, I’ve created a video that walks you through setting up a trial for CRM Online, Office 365, and Microsoft Azure where all the services are provisioned for Single Sign On (SSO) under a single Azure Active Directory tenant and provide shared administration. 

@devkeydet

Microsoft Dynamics Technical Conference

February 2-4, 2015, Seattle WA

Hear from Jujhar Singh, General Manager of Program Management for Microsoft Dynamics CRM, about why you should be a part of the Microsoft Dynamics Technical Conference.
clip_image004
Equal parts education, networking, and fun, the Microsoft Dynamics Technical Conference brings together Microsoft Dynamics CRM professional and developer communities in a technical readiness experience designed to inspire you and help you take your business further. This is our first-ever such event for Microsoft Dynamics CRM partners. Don’t miss it!
We want to see you there!

Register now!

clip_image006

Time is running out: Register today for the Microsoft Dynamics Technical Conference!
Your conference pass—priced at $1,595—gives you access to keynotes, general and breakout sessions, and evening functions, where you’ll get in on knowledge and conversations that can only happen in a live event atmosphere.
Check out the Session Catalog now.
All of our Breakout Sessions and Instructor-led Labs are focused on Microsoft Dynamics CRM 2015. Don’t miss your chance to meet the members of the R&D team and speak directly to the experts!
Looking for more hands-on training?
Check out the Deep Dive Workshops. Scheduled around the Microsoft Dynamics Technical Conference and offered at half the usual price (use code HaLF007), these intensive workshops help you maximize your learning and make the most of your trip to Seattle.

Register now!

clip_image007

See you in Seattle!
The Microsoft Dynamics Technical Conference Team

© Microsoft Corporation. All Rights Reserved.