Skip to content

IOrganizationService Extensions

Artem Grunin edited this page Dec 2, 2022 · 14 revisions

IOrganizationService Extensions

Set of extension methods for IOrganizationService base class. Most of these are simple overrides of existing methods which take EntityReference or Entity instead of separate Id and LogicalName parameters. But you also can take some advantage of alternative keys support or working with large datasets.

Associate & Disassociate

Associate & Disassociate methods override. Take EntityReference (instead of separate Id + LogicalName) input parameter.

Alternative keys are supported since v 1.0.43

public void Associate(EntityReference primaryEntity, Relationship relationship, EntityReferenceCollection relatedEntities);
public void Associate(EntityReference primaryEntity, Relationship relationship, IList<EntityReference> relatedEntities);
public void Associate(EntityReference primaryEntity, String relationshipName, EntityReferenceCollection relatedEntities);
public void Associate(EntityReference primaryEntity, String relationshipName, IList<EntityReference> relatedEntities);

public void Disassociate(EntityReference primaryEntity, Relationship relationship, EntityReferenceCollection relatedEntities);
public void Disassociate(EntityReference primaryEntity, Relationship relationship, IList<EntityReference> relatedEntities);
public void Disassociate(EntityReference primaryEntity, String relationshipName, EntityReferenceCollection relatedEntities);
public void Disassociate(EntityReference primaryEntity, String relationshipName, IList<EntityReference> relatedEntities);

Delete

Delete method override. Take EntityReference (instead of separate Id + LogicalName) input parameter.

Alternative keys are supported since v 1.0.29

public void Delete(EntityReference reference);
public void Delete(Entity entity);

Delete by Alternative key

Delete method override. Deletes using Alternative keys instead of Id. This one use Execute under the hood

public void Delete(string logicalName, KeyAttributeCollection keyAttributeCollection);
public void Delete(string logicalName, string keyName, object keyValue);

Execute<T>

Introduced in version 1.0.38

Generic Execute methods override. Returns response as the specified type. Requires the same amount of typing as any other form of casting. Useless.

public T Execute<T>(OrganizationRequest request) where T : OrganizationResponse;

Execute (Multiple)

Introduced in version 1.5

Execute batch of requests using ExecuteMultipleRequest while taking care of butch size

Warning: NEVER use this extension as well as ExecuteMultipleRequest itself in Plugin code. It can lead to deadlocks and performance issues

In other cases it should significantly improve performance compared to the ordinary for or Parallel.For loops

public IEnumerable<ExecuteMultipleResponse> Execute(IEnumerable<OrganizationRequest> requests, int batchSize = 1000, ExecuteMultipleSettings settings = null);

This is experimental feature:

You can use ExecuteMultipleResponse.GetRequests() or ExecuteMultipleResponse.GetRequest(ExecuteMultipleResponseItem item) to access actual OrganizationRequest objects used to perform the operation. It may be useful to understand status of each particular request

Retrieve

Retrieve method override. Take EntityReference (instead of separate Id + LogicalName) input parameter.

Alternative keys are supported since v 1.0.29

public Entity Retrieve(EntityReference reference, ColumnSet columnSet);
public Entity Retrieve(EntityReference reference, params String[] columns);

Retrieve<T>

Generic version of Retrieve.

public T Retrieve<T>(EntityReference reference, ColumnSet columnSet) where T : Entity;
public T Retrieve<T>(EntityReference reference, params String[] columns) where T : Entity;

Retrieve by Alternative key

Retrieve method override. Take EntityReference instead of Id. This one use Execute under the hood

public Entity Retrieve(string logicalName, KeyAttributeCollection keyAttributeCollection, ColumnSet columnSet);
public Entity Retrieve(string logicalName, string keyName, object keyValue, ColumnSet columnSet);
public Entity Retrieve(string logicalName, string keyName, object keyValue, params string[] columns);

Retrieve<T> by Alternative key

Generic version of by Alternative key. This one use Execute under the hood

public T Retrieve<T>(string logicalName, string keyName, string keyValue, ColumnSet columnSet) where T : Entity;
public T Retrieve<T>(string logicalName, string keyName, string keyValue, params String[] columns) where T : Entity

RetrieveMultiple

Introduced in version 1.0.38

RetrieveMultiple methods override. Returns all pages using callback or 'yield' iterator. This implementation should be very cost effective as new pages are loaded on demand (lazy loadind). You also can use callback function to access original EntityCollection properties. Callback function is executed on each page load.

Warning: Callback is executed only then you use iterator.

public IEnumerable<Entity> RetrieveMultiple(QueryBase query, Action<EntityCollection> callback = null);
public IEnumerable<Entity> RetrieveMultiple(FetchExpression query, Action<EntityCollection> callback = null);

Upsert

Introduced in version 1.0.42

A shortcut for Upsert message

public EntityReference Upsert(Entity entity);

Update

Introduced in version 1.2

Update method override. Take ConcurrencyBehavior enum as second parameter.

public void Update(Entity entity, ConcurrencyBehavior concurrencyBehavior);