Using the CRM Service via the DataContext

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.
Reader Comments (75)
Thanks for a nice share you have given to us with such an large collection of information.. it's so nicepolice sunglasses official
JVC BN-V11U Camcorder Battery
JVC BN-VF815 Camcorder Battery
JVC BN-VF714 Camcorder Battery
jvc bn v408u battery
JVC BN-V107U Camcorder Battery
JVC BN-VF733U Camcorder Battery
JVC BN-VF714U Camcorder Battery
JVC BN-V20U Camcorder Battery
JVC BN-V507U Camcorder Battery
JVC BN-VF733 Camcorder Battery
JVC BN-VF823U Camcorder Battery
JVC BN-V428U Camcorder Battery
JVC BN-V114U Camcorder Battery
JVC BN-V416U Camcorder Battery
JVC GR-D240 Camcorder Battery
JVC GR-D250 Camcorder Battery
jvc gr dvm70 battery
JVC GR-D270 Camcorder Battery
JVC GR-D250U Camcorder Battery
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++=
jvc gz-mg30u charger
jvc gz-mg21u charger
jvc gz-mg20u charger
jvc gz-mg155 charger
jvc gz-mg37u charger
jvc gz-mg130 charger
jvc gr-dx77us charger
jvc gr-dx100ek charger
jvc gr-dv500u charger
jvc gr-d270u charger
jvc gr-d270 charger
jvc gr-d244us charger
jvc gr-dv800u charger
jvc gr-d750u charger
jvc gr-d396us charger
jvc gr-dv3 charger
jvc gr-d796 charger
jvc gr-d347u charger
jvc gr-d33us charger
jvc gr-d31us charger
jvc gr-d30u charger
jvc gr-d93us charger
jvc gr-d72us charger
jvc gr-d395us charger
jvc gr-d350u charger
jvc gy-hd110u battery
jvc gy-hd111 battery
jvc aa-vf8 charger
jvc bn-vf714u charger
jvc gr-d275us battery
jvc gr-df550us battery
jvc gr-d70u battery
jvc bn-v107us battery
jvc gz-mc500u battery
jvc gz-mc500u battery
jvc gz-mc500ek battery
jvc gz-mc200u battery
jvc bn-vm200u battery
jvc bn-vm200u battery
JVC GR-DVL9800KR Battery Charger
JVC GZ-D270 Battery Charger
JVC GR-D370EX Battery Charger
JVC GR-DF550 Battery Charger
JVC GR-DVL9800U Battery Charger
JVC GR-DVM5U Battery Charger
JVC GR-DVY Battery Charger
JVC GR-DVL167 Battery Charger
JVC DVX70 Battery Charger
JVC GR-DVM1U Battery Charger
JVC GR-D72US Battery Charger
JVC GR-D246 Battery Charger
JVC GR-D63 Camcorder Battery
JVC BN-VF707UE Camcorder Battery
JVC GR-DVF21U Camcorder Battery
JVC GR-D370 Camcorder Battery
The most creative of all up till moncler jacketsnow thefor women is sure to make you the most wonand you can see and moncler women of today. like moncler outlet or and and you also would like were named after the legendary player Michael Jordan and the design and making of th is what makes them different from the others. have a place in the history.as well. varied designs, colors and discounted prices,these shoes can now be easily bought by those who are on a low budget as well.juicy couture handbags is Casualness and Personal Identity.Buying handbags, you can choosejuicy couture outlet.
There is a good news for Jimmy Choo lovers,buy Jimmy Choo Shoes at www.discountjimmychoo-uk.com is very cheap and the quality is very good.All products are on high discount and free shipping,you should not miss.
If you want to buy MBT Sale, www.mbtantishoesstore.com may be your perfect choice,there MBT Shoes are sold at high discount,most MBT products are having promotion now,don’t miss such a good opportunity.
Indonesia Siap Bersaing di Serp
diskon gila disdus.com
Rumah Mungil Yang Sehat
Mobil Keluarga Ideal Terbaik Indonesia
Really trustworthy blog. Please keep updating with great posts like this one. I have booked marked your site and am about to email it to a few friends of mine that I know would enjoy reading..
five fingers
Vibram Five Fingers Performa Womens
Vibram Five Fingers Speed
cheap ugg boots
GHD Hair Straighteners uk
GHD Hair Straightener Discount
nike air max
Best GHD Straighteners sale
chi flat irons
There is a good news for Jimmy Choo lovers,buy Jimmy Choo Shoes at www.discountjimmychoo-uk.com is very cheap and the quality is very good.All products are on high discount and free shipping,you should not miss.
If you want to buy MBT Sale, www.mbtantishoesstore.com may be your perfect choice,there MBT Shoes are sold at high discount,most MBT products are having promotion now,don’t miss such a good opportunity.
Microsoft Office 2010
Office 2010
Office 2007
Microsoft Office 2007
Microsoft Office
Windows 7
Microsoft outlook
Outlook 2010
Microsoft outlook 2010
Acrobat 9
adobe Acrobat
project 2010
microsoft project 2010
visio 2010
microsoft visio 2010
Microsoft Office 2010
Office 2010
Office 2007
Microsoft Office 2007
Microsoft Office
Windows 7
Microsoft outlook
Outlook 2010
Microsoft outlook 2010
Acrobat 9
adobe Acrobat
These are wonderful! Thank you for sharing
Wholesale NFL jerseys
Microsoft Office 2010
Office 2010
Office 2007
Microsoft Office 2007
Microsoft Office
Windows 7
Microsoft outlook
Outlook 2010
Microsoft outlook 2010
Acrobat 9
adobe Acrobat
With summer approaching, we have to begin our work of looking for new clothes, shoes and even hair styles. NBA jerseys are the best choice for wearing in summers. NBA is a magic word that associates with numerous dreams. We are not only interested in the games themselves but the NBA jerseys, shirts and shorts that the players are wearing. Why not to buy a cheap NBA jersey on the lovejerseys.com?
Red Bull HatsThat was a great piece of information., I enjoyed reading it..,
Good new! Jimmy Choo Shoes at www.discountjimmychoo-uk.com are on high discount now! You can find all kinds of Jimmy Choo products here, such as Jimmy Choo Uggs, the products here have cheap price but high quality. The unique design and classic style can show your elegant and noble perfectly, do not hesitate, act now!
replica watches on sale
replica A. Lange & Sohne watches
Audemars Piguet watches replica
Bell & Ross watches replica
Blancpain watches replica
Breguet watches replica
Breitling watches replica
Cartier watches replica
Chopard watches replica
replica watches on sale
replica A. Lange & Sohne watches
Audemars Piguet watches replica
Bell & Ross watches replica
Blancpain watches replica
Breguet watches replica
Breitling watches replica
Cartier watches replica
Chopard watches replica
Ralph Lauren Ralph Lauren Polo Ralph Lauren Polo Ralph Lauren Ralph Lauren Polo Ralph Lauren Polo Cheap Ralph Lauren Polo Cheap Ralph Lauren Polo Ralph Lauren Polo Outlet Ralph Lauren Polo Outlet Ralph Lauren Outlet Store Ralph Lauren Outlet Store Ralph Lauren Polos Ralph Lauren Polos Ralph Lauren Clothing Ralph Lauren Clothing Discount Ralph Lauren Women S Polo Shirts Discount Ralph Lauren Women Polo Ralph Lauren Classic Fit Stripe Sport Shirt Polo Ralph Lauren Classic Mens Ralph Lauren Custom Fit Polo Mens Ralph Lauren Custom Ralph Lauren Black Watch Polo Ralph Lauren Black Watch Big And Tall Ralph Lauren Polo Shirts Big And Tall Ralph Lauren Colorful Polo Ralph Lauren Shirts Colorful Polo Ralph Lauren Ladies Polo Ralph Lauren Shirts Ladies Polo Ralph Lauren Polo Ralph Lauren Models Polo Ralph Lauren Models Big Pony Polo 5x Ralph Lauren Big Pony Polo True Religion True Religion True Religion Jeans True Religion Jeans Cheap True Religion Jeans Cheap True Religion Jeans Discount True Religion Jeans Discount True Religion Jeans Herve Leger Herve Leger Herve Leger Dress Herve Leger Dress Herve Leger Skirt Herve Leger Skirt Herve Leger Outlet Herve Leger Outlet Herve Leger Bandage Herve Leger Bandage Herve Leger Dresses Herve Leger Dresses
Useful blog website, keep me personally through searching it, I am
seriously interested to find out another recommendation of it.