Organization service methods


Applies To: Dynamics 365 (online), Dynamics 365 (on-premises), Dynamics CRM 2016, Dynamics CRM Online

The IOrganizationService web service provides a set of methods used to perform the most common operations on system and custom entities and on the metadata for your organization. These operations can also be performed by using the IOrganizationService.Execute method and the corresponding message.


Use the IOrganizationService.Create method to create an instance (record) of any entity that supports the Create message, including custom entities.


Use the IOrganizationService.Retrieve method to retrieve an instance (record) of an entity.


Use the IOrganizationService.RetrieveMultiple method to retrieve a collection records. The query can be specified using a query expression or Fetch XML query.


Use the IOrganizationService.Update method to update an existing record.


Use the IOrganizationService.Delete method to delete an existing record.


Use the IOrganizationService.Associate method to create a link between two records that participate in a relationship.


Use the IOrganizationService.Disassociate method to delete the link between two records.


Use the IOrganizationService.Execute method to execute a message. This includes common processing like create and delete of data records and metadata, or it can be specialized processing such as import or detect duplicates.


The following code shows how to use the Create, Retrieve, and Update methods using the strong types.

//Define the account for which we will add letters                
Account accountToCreate = new Account
    Name = "Example Account"

//Define the IDs of the related letters we will create
_letterIds = new[] { Guid.NewGuid(), Guid.NewGuid(), Guid.NewGuid() };

//This acts as a container for each letter we create. Note that we haven't
//define the relationship between the letter and account yet.
EntityCollection relatedLettersToCreate = new EntityCollection
    EntityName = Letter.EntityLogicalName,
    Entities =
        new Letter{Subject = "Letter 1", ActivityId = _letterIds[0]},
        new Letter{Subject = "Letter 2", ActivityId = _letterIds[1]},
        new Letter{Subject = "Letter 3", ActivityId = _letterIds[2]}

//Creates the reference between which relationship between Letter and
//Account we would like to use.
Relationship letterRelationship = new Relationship("Account_Letters");

//Adds the letters to the account under the specified relationship
accountToCreate.RelatedEntities.Add(letterRelationship, relatedLettersToCreate);

//Passes the Account (which contains the letters)
_accountId = _service.Create(accountToCreate);

Console.WriteLine("An account and {0} letters were created.", _letterIds.Length);

//Now we run through many of the same steps as the above "Create" example
Account accountToUpdate = new Account
    Name = "Example Account - Updated",
    AccountId = _accountId

EntityCollection relatedLettersToUpdate = new EntityCollection
    EntityName = Letter.EntityLogicalName,
    Entities =
        new Letter{Subject = "Letter 1 - Updated", ActivityId = _letterIds[0]},
        new Letter{Subject = "Letter 2 - Updated", ActivityId = _letterIds[1]},
        new Letter{Subject = "Letter 3 - Updated", ActivityId = _letterIds[2]}

accountToUpdate.RelatedEntities.Add(letterRelationship, relatedLettersToUpdate);

//This will update the account as well as all of the related letters
Console.WriteLine("An account and {0} letters were updated.", _letterIds.Length);

See Also

Use the Organization Service to read and write data or metadata
Use messages (request and response classes) with the Execute method
Use ExecuteMultiple to improve performance for bulk data load
xRM messages in the Organization service
Dynamics 365 messages in the organization service
Authenticate users in Microsoft Dynamics 365
Sample: Create, retrieve, update, and delete records (early bound)
Sample: Create, retrieve, update, and delete (late bound)

Microsoft Dynamics 365

© 2016 Microsoft. All rights reserved. Copyright