Using the CRM Service via the DataContext
Thursday, May 13, 2010 at 10:54PM
TheCRMBook.com

Last week Microsoft announced the latest update to the MSCRM SDK that included a new LINQ provider. If you are new to the provider then you should read my quick start blog post here first.

In this blog post we are going to look at how you can use the CRM Service when you have a DataContext instance. As you probably recall the DataContext is generated when you run crmsvcutil against your CRM organization. DataContext is a wrapper on top of the standard CRM Service. Sometimes you need to drop down to make calls against the lower level CRM Service. For example, calling Who Am I, working with metadata and any other interaction that requires calling Execute. As it turns out if you have an instance of DataContext it provides an easy way to interact with the standard CRM Service for these types of operations. In the rest of the blog post we are going to look at calling WhoAmI using the DataContext helper methods.

The first approach we are going to look at is using the CreateService method on the DataContext. This method gives back an object instance that implements IOrganizationService. The IOrganizationService interface is a new abstraction as part of the updated SDK that provides methods to interact with both the transactional data in CRM and the metadata. Using the object instance returned from CreateService you can do calls to Execute just like you would from the CRM Service or Metadata Service directly.

Let’s get started making a Who Am I call to the CRM Service using the IOrganizationService instance. The first thing we need to do is add a using for the SdkTypeProxy namespace where the WhoAmIRequest/Response classes reside. Notice we are using an alias of SdkTypeProxy on the using. The reason for that is that name space also contains the “account” entity class and it conflicts with the generated account class created by CrmSvcUtil code generation. By using an alias we can work around this pretty easily.

using SdkTypeProxy = Microsoft.Crm.SdkTypeProxy;

Next, create an instance of the DataContext, remember this class is generated when you ran CrmSvcUtil and might have a different name if you chose something different on the options.

DataContext ctx = new DataContext("mycrm");
Next, define a Guid variable to hold the current user ID we get back.

Guid currentUserID = Guid.Empty;
Next, call the CreateService method on the context to get an instance of IOrganizationService. You will notice a couple of things in the following example. First, we are using “var service” without defining the type. The var keyword causes the type to be inferred from the right side of the statement. In this case it will be inferred to be IOrganizationService since that is what CreateService returns. We are using this approach to keep the code simply and concise without any redundant specification. You will also notice we wrap the code block with a “using”. When coded like below the using ensures that the dispose is properly called on the IOrganizationService instance when it falls out of scope either due to normal completion or an exception. We then proceed to build up a WhoAmIRequest object and call the execute method to get the current user ID.

using (var service = ctx.CreateService())

{

var userRequest = new SdkTypeProxy.WhoAmIRequest();

var user = service.Execute(userRequest) as SdkTypeProxy.WhoAmIResponse;

Console.Write("You are running as user " + user.UserId);

currentUserID = user.UserId;

}

The new SDK also offers another approach that you can use to accomplish the same thing. Instead of calling CreateService you can call the UsingService method which accomplishes the same thing as the “using” statement and the CreateService method call. The following is the revised code adjusted to call the UseService method.

ctx.UsingService(service =>

{

var userRequest = new SdkTypeProxy.WhoAmIRequest();

var user = service.Execute(userRequest) as SdkTypeProxy.WhoAmIResponse;

Console.Write("You are running as user " + user.UserId);

currentUserID = user.UserId;

});

Using either approach you can make calls to the standard CRM Service without having to re-create a connection to the CRM server using the lower level API. Stay tuned as we continue to explore how to use the new LINQ provider that is part of the new SDK.

Article originally appeared on CRM as a Rapid Development Platform (http://blog.thecrmbook.com/).
See website for complete article licensing information.