Monday, May 21, 2012

OrganizationServiceContext Overview

Overview




The OrganizationServiceContext class provides an alternative method of connecting to the underlying MS CRM Organization Service, providng the ability to easily track CRM objects and their related object, perform actions on those objects and commit changes back into the CRM database. The following link provides a list of all supported methods of OrganizationServiceContext



OrganizationServiceContext can be generated using the CrmSvcutil.exe code generation tool located in the <deployed location>SDK\bin folder of the CRM SDK. The syntax for generating the OrganizationServiceContext is outlined below:



CrmSvcUtil.exe /url:http:////XRMServices/2011/Organization.svc /out:.cs /username: /password: /domain:/namespace: /serviceContextName


Under the covers OrganizationServiceContext wraps the OrganizationServiceProxy class and uses an IOrgnaizationService as it's underlying connection. In the sample below we have generated an OrganizationServiceContext called OrgServiceContext which is ready for use.

Note: The name of your OrgnanizationSerivceContext object may be different based on what name you specified in the /serviceContextName parameter during the creation using CrmSvcutil.exe:



//Generate the Serivce Proxt _serivceProxy
...

//Generate IOrgnaizationService from the _serviceProxy
IOrganizationService _service = (IOrganizationService)_serviceProxy;

//Generate OrganizationServiceContext from the IOrganizationService
OrgServiceContext _orgContext = new OrgServiceContext(_service);



You can use both Early and Late Bound Types with the OrganizationServiceContext. To use Early Bound Types you have to enable Proxy Types on the IOrganizationServiceProxy prior to the OrganizationServiceContext being created:



// Enable Proxy Types for Early Bound classes
_serviceProxy.EnableProxyTypes();

// Create a new contact and commit to CRM using _orgContext.AddObject.
var contact = new Contact()
{
    FirstName = "Joe",
    LastName = "Bloggs",
    Address1_Line1 = "116 Joe Bloggs St.",
    Address1_City = "Leeds",
    Address1_StateOrProvince = "North Yorkshire",
    Address1_PostalCode = "99999",
    Telephone1 = "0111222332",
    EMailAddress1 = "joe.bloggs@joebloggs.com",
    Id = Guid.NewGuid()
};

_contactId = contact.Id;
_orgContext.AddObject(contact);



The following provides an example of how to use Late Bound Types with the generated OrganizationServiceContext:



// Create an organization service context object
OrgServiceContext _orgContext = new OrgServiceContext(_serviceProxy);

// Instantiate an account object using the Entity class.
Entity sampleAccount= new Entity("account");

sampleAccount["name"] = "Sample account 1";
testaccount["emailaddress1"] = sampleAccount@samplecompany.com;

// Save the entity using the organization service context object.
_orgContext.AddToAccountSet(sampleAccount);
context.SaveChanges();


LINQ Queries


OrganizationServiceContext implementes the IQueryable interface allowing .Net LINQ queries to be written and executed against CRM data via the OrganizationServiceContext. The code sample below assumes an OrganizationServiceContext object has been instantiated and retrieves a list of all accounts with a name beggining with the word "Sample"


      
var accounts = (from a in _orgContext.CreateQuery("account")
where ((string)a["name"]).StartsWith("Sample")
select new
{
   Id = (Guid)a["accountid"],
   Name = (string)a["name"]
}).ToList();




The CreateQuery method takes either an Entity type name or a String representing the entity logical name and returns a collection of IQueryable. The following link provides a good overview of how to construct LINQ queries using the OrganizationServiceContext.

No comments:

Post a Comment