Friday, July 17, 2009

Code to connect to CRM Online CRMService

you have to down load service and store it on hard disk and add reference to it in application.


public static CrmService.CrmService GetCrmService()
{
if (CLMservice == null)
{
CLMservice = new CrmService.CrmService();
//CrmAuthenticationToken token1 = new CrmAuthenticationToken();
//token1.AuthenticationType = 0;
//token1.OrganizationName = "CLM";


//CLMservice.CrmAuthenticationTokenValue = token1;
//CLMservice.Credentials = new System.Net.NetworkCredential("administrator", "syspro15*TX");

//
//-------------------
CrmDiscoveryService.CrmDiscoveryService discoveryService = new CrmDiscoveryService.CrmDiscoveryService();
discoveryService.Url = String.Format("https://{0}/MSCRMServices/2007/{1}/CrmDiscoveryService.asmx", "dev.crm.dynamics.com", "Passport");
//"http://clm.crm.dynamics.com/MSCrmServices/2007/Passport/CrmDiscoveryService.asmx?uniquename=clm";//
RetrievePolicyRequest policyRequest = new RetrievePolicyRequest();
RetrievePolicyResponse policyResponse = (RetrievePolicyResponse)discoveryService.Execute(policyRequest);
LogonManager lm = new LogonManager();
//string passportTicket = lm.Logon("syspro101@hotmail.com", "dxt31MS", "crm.dynamics.com", policyResponse.Policy, "Production");
// string passportTicket = lm.Logon("crmadmin@sysprotech.com", "sys35adm", "crm.dynamics.com", policyResponse.Policy, "Production");
string passportTicket = lm.Logon(ConfigurationSettings.AppSettings["CRMUserName"], ConfigurationSettings.AppSettings["CRMPassword"], "crm.dynamics.com", policyResponse.Policy, "Production");

RetrieveCrmTicketRequest crmTicketRequest = new RetrieveCrmTicketRequest();
//crmTicketRequest.OrganizationName = "clm";
crmTicketRequest.OrganizationName = ConfigurationSettings.AppSettings["Organization"];
//crmTicketRequest.OrganizationName = System.Configuration.ConfigurationSettings.AppSettings["Orgname"];
crmTicketRequest.PassportTicket = passportTicket;
RetrieveCrmTicketResponse crmTicketResponse = (RetrieveCrmTicketResponse)discoveryService.Execute(crmTicketRequest);


CrmService.CrmService crmService = new CrmService.CrmService();
crmService.Url = crmTicketResponse.OrganizationDetail.CrmServiceUrl;

CrmService.CrmAuthenticationToken token = new CrmService.CrmAuthenticationToken();
token.AuthenticationType = AuthenticationType.Passport;
token.CrmTicket = crmTicketResponse.CrmTicket;
token.OrganizationName = crmTicketResponse.OrganizationDetail.OrganizationName;
crmService.CrmAuthenticationTokenValue = token;
WhoAmIRequest whoRequest = new WhoAmIRequest();
WhoAmIResponse whoResponse = (WhoAmIResponse)crmService.Execute(whoRequest);
lm.Dispose();
CrmService.CrmService service = crmService;
CLMservice = crmService;
return CLMservice;// service;
}
return CLMservice;
}

No comments: