Showing posts with label CrmService. Show all posts
Showing posts with label CrmService. Show all posts

Friday, April 19, 2013

Microsoft CRM Services



What is a web service in Microsoft CRM?
A web service is a programming interface exposed by an application. It follows industry standards that allow it to be consumed ( used ) by any software technology that understands the web service standards defined by W3C.
CRM Live exposes two web services: CrmService & CrmMetadataService
CrmService is used to fetch data, set data, make programmatic configuration changes, etc.
CrmMetadataService is used to query about the metadata. The metadata describes the entities, attributes, relationships, and so on, of the organization.
The WSDL, which stands for web service description language, can be fetched from your CRM system via the Settings-Download WSDL. You use this to create your web reference’s.
The endpoint URL for each of these in an internet facing deployment of CRM can be discovered thru the CrmDiscoveryService Web Service. You’ll also use the CrmDiscoveryService  for policy information during the authentication process. 
The reference for the discovery service is at
https://MyOrgName/MSCRMServices/2007/Passport/CrmDiscoveryService.asmx
Then in your code set the URL for the CrmService and CrmMetadataService using the endpoint returned from the discovery services. The URLs can be retrieved from your ticket response object.

Example :
// STEP 1: Retrieve a policy from the Discovery Web service.
CrmDiscoveryService discoveryService = new CrmDiscoveryService();
discoveryService.Url = String.Format("https://{0}/MSCRMServices/2007/Passport/CrmDiscoveryService.asmx",_hostname);
RetrievePolicyRequest policyRequest = new RetrievePolicyRequest();
RetrievePolicyResponse policyResponse = (RetrievePolicyResponse)discoveryService.Execute(policyRequest);
// STEP 2: Retrieve a Passport ticket from the Passport service.
LogonManager lm = new LogonManager();
string passportTicket = lm.Logon(UserID, Password, _partner, policyResponse.Policy, _environment);
// STEP 3: Retrieve a CrmTicket from the Discovery Web service.
RetrieveCrmTicketRequest crmTicketRequest = new RetrieveCrmTicketRequest();
crmTicketRequest.OrganizationName = _organization;
crmTicketRequest.PassportTicket = passportTicket;
RetrieveCrmTicketResponse crmTicketResponse = (RetrieveCrmTicketResponse)discoveryService.Execute(crmTicketRequest);
// STEP 4: Create and configure an instance of the CrmService Web service.
CrmAuthenticationToken token = new CrmAuthenticationToken();
token.AuthenticationType = AuthenticationType.Passport;
token.CrmTicket = crmTicketResponse.CrmTicket;
token.OrganizationName = crmTicketResponse.OrganizationDetail.OrganizationName;
CrmService crmService = new CrmService();
crmService.Url = crmService.CrmAuthenticationTokenValue = token;
// STEP 5: Invoke the desired CrmService Web service methods.
lead newlead = new lead();
newlead.description = "Lead Test";
newlead.subject   = "My Lead";
newlead.firstname ="Sai";
newlead.lastname  = "Krishna";


// Call the Create method to create an lead
Guid leadID = crmService.Create(newlead);
When using the CRM Web Services, there are times when you need to work with custom entities that aren't described in the current WSDL that you have. The DynamicEntity lets you program against types that you don't have the full description in the WSDL. This can be very helpful when you don't have access to fetch the systems current WSDL files.
Let's look at a code snippet to do this.

Here at the steps .
1. Create all the attribute values
2. Create Dynamic Entity
3. Set the attribute properties
4. Create the Target
5. Execute the Request
I'll show how to create a Customer entity and associate an existing account id to the new entity. The CRM SDK support a variety of property type objects. Be sure to use the correct object type with the definition of the attribute. Mismatching this is a common error. I'll save you some time with this tip: Use LookUpProperty instead of Guid type when relating entities together. Even though the value you set will be a GUID, it only works with the LookupProperty object.
// Create Properties
LookupProperty accountid = new LookupProperty();
accountid.Name = "new_accountid";
accountid.Value = new Lookup();
accountid.Value.Value = new Guid("260FB27F-25E6-DC11-BEDA-0013210AC0BE");
accountid.Value.type = EntityName.account.ToString();

StringProperty name = new StringProperty();
name.Name = "new_CustomerName";
name.Value = "Krishna";
DateTime userTime = new DateTime();
userTime = dtExpDate;

CrmService.CrmDateTime LaunchDate = new CrmService.CrmDateTime();
LaunchDate.Value = string.Format(System.Globalization.CultureInfo.InvariantCulture, "{0:s}", userTime);
CrmDateTimeProperty LaunchDate = new CrmDateTimeProperty();
LaunchDate.Name = "new_LaunchDate";
LaunchDate.Value = LaunchDate;

// Create the DynamicEntity object.
DynamicEntity AcmeEntity = new DynamicEntity();

// Set the name of the entity type.
AcmeEntity.Name = "new_Customer";

// Set the properties
KeyEntity.Properties = new Property[] { name, accountid, LaunchDate };

// Create the target.
TargetCreateDynamic targetCreate = new TargetCreateDynamic();
targetCreate.Entity = KeyEntity;

// Create the request object.
CreateRequest create = new CreateRequest();

// Set the properties of the request object.
create.Target = targetCreate;

// Execute the request.
CreateResponse created = (CreateResponse)crmService.Execute(create); 

That's it. Now we have an instance of the entity type
new_Customer that is related to the account id. Don't forget to check out the SDK for complete samples and descriptions of all the property objects.  Also, if you get errors, the first to check is that the attribute type matches the property object that you are using.