Microsoft CRM 2011 web services

Microsoft CRM 2011 provides rich WCF service support. Microsoft CRM 2011 provides the two following web services.

Organization web service

The Organizaiton web service is used to retrieve data and metadata from Microsoft CRM 2011. It has the following methods.

Create

The Create method is used to create entity records. It takes the entity object as a parameter and returns the GUID of the newly created record. We can use the following example to create the Account entity record:

        Entity Account = new Entity("account");
        Account.Attributes.Add("name", "TestAccount");
        Account.Attributes.Add("address1_city","Gurgaon");
        service.Create(Account);

Update

The Update method is used to update existing entity data based on key values. It takes the entity object as a parameter. For example, if we want to update city information for a particular account, we can write the following function:

private void UpdateAccount(IOrganizationService service,Guid AccountID)
    {
        Entity Account = new Entity("account");
        Account["address1_city"] = "CA";
        Account["accountid"] = AccountID;
        service.Update(Account);
    }

Delete

This method is used to delete entity records. It takes the entity name and GUID of the record that we want to delete. The following code snippet is an example of the Delete method:

service.Delete("account", AccountID);

Retrieve

The Retrieve method is used to retrieve data from CRM entities based on the key value. It takes the entity name, the GUID of the record, and the column that we want to fetch. For example, we can use the following code to fetch account records:

Entity _Account = service.Retrieve("account",AccountID, new ColumnSet(new string[] { "name", "accountcategorycode"}));

RetrieveMultiple

The RetrieveMultiple method is used to fetch data from CRM entities based on the condition available in the query object passed to this method. This method returns entity collection.

QueryExpression _Query = new QueryExpression("account");
        ColumnSet _Cols = new ColumnSet(new string[] { "name", "accountcategorycode", "parentaccountid" });
        ConditionExpression _Condition = new ConditionExpression();
        _Condition.AttributeName = "address1_city";
        _Condition.Operator = ConditionOperator.Equal;
        _Condition.Values.Add("CA");
        _Query.ColumnSet = _Cols;
        _Query.Criteria.AddCondition(_Condition);
        EntityCollection Entity = service.RetrieveMultiple(_Query);

Execute

The Execute method can be used to perform those operations that are not exposed as methods in the CRM service. It takes Request as a parameter and returns Response as output. For example, we can use the following request to assign an account to a Microsoft CRM 2011 user and pass the account to the Execute method:

AssignRequest Request = new AssignRequest
            {
             Assignee = new EntityReference("systemuser", UserID),
                Target = new EntityReference("account", AccountID)
            };
AssignResponse Response = (AssignResponse)service.Execute(Request);

Associate

The Associate method is used create links between two entity records. For example, we can use the following code to relate account IDs to contacts:

//Create a collection of the entity ids that will be associated to the contact.
        EntityReferenceCollection relatedEntities = new EntityReferenceCollection();
        relatedEntities.Add(new EntityReference("account", account1Id));
        relatedEntities.Add(new EntityReference("account", account2Id));
        relatedEntities.Add(new EntityReference("account", account3Id));
        Relationship relationship = new Relationship("account_primary_contact");
        service.Associate("contact", contactId, relationship, relatedEntities);

Disassociate

The Disassociate method is used to remove links between two entity records. The following code snippet is an example of disassociating entity records associated in the previous method:

        service.Disassociate("contact", _contactId, relationship, relatedEntities);

In order to access metadata, we need to use metadata classes. You can refer to http://technet.microsoft.com/en-us/library/gg307336.aspx to get information on metadata classes for Microsoft CRM 2011.

Discovery web service

DiscoveryService is used to get lists of organizations for a CRM user. This web service only contains one method, the Execute method. The following code snippet is an example of fetching all organization information:

RetrieveOrganizationsRequest orgsRequest =new RetrieveOrganizationsRequest()
             { AccessType = EndpointAccessType.Default,
               Release = OrganizationRelease.Current
              };
        RetrieveOrganizationsResponse organizations =
RetrieveOrganizationsResponse)service.Execute(orgsRequest);
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.223.170.223