Tuesday, December 04, 2007

CRM 4.0 Code Sample

Dear Readers,

While working in MS CRM 4.0 (Titan), there is one more webservice introduced for the purpose of handling multi-tenacy.

Here is code sample, how you will use this webservice to authenticate.

//Create Windows Network Credential
NetworkCredential cred = new NetworkCredential();
cred.Domain = textBoxDomain.Text;
cred.UserName = textBoxUserName.Text;
cred.Password = textBoxPassword.Text;

//Create a Discovery Service
CrmDiscoveryService disco = new CrmDiscoveryService();
disco.Credentials = cred;
disco.Url = textBoxServer.Text + "/MSCRMServices/2007/AD/CrmDiscoveryService.asmx";

//Retrieve a list of Organizations which this user has access to
RetrieveOrganizationsRequest orgRequest = new RetrieveOrganizationsRequest();
RetrieveOrganizationsResponse orgResponse = (RetrieveOrganizationsResponse)disco.Execute(orgRequest);


//Create a CRM Authentication token
CrmAuthenticationToken token = new CrmAuthenticationToken();
token.AuthenticationType = 0;
token.OrganizationName = formorg.Org.OrganizationName;

//Create a CRM Service
CrmService service = new CrmService();
service.CrmAuthenticationTokenValue = token;
service.Credentials = cred;
service.Url = formorg.Org.CrmServiceUrl;

//Run a basic service operation to test the connection
WhoAmIRequest whoRequest = new WhoAmIRequest();
WhoAmIResponse whoResponse = (WhoAmIResponse)service.Execute(whoRequest);


Hopefull code snippet will help to developers.

Regards,
Imran

msn : mscrmexpert@gmail.com