ParallelOrganizationServiceProxy.Create() Method

The Create method accepts a list of entities representing the targets of each create request. Method overloads accept an instance of OrganizationServiceProxyOptions to control proxy channel behaviors. Each Create method returns a list of the target entities submitted for create with the unique identifier generated by the CRM platform assigned to each entity.

Parallel Create request can also be performed using a keyed collection of target Entity instances. These methods return a keyed collection of unique identifiers generated from each Create request. The key can be used to correlate the originating Entity to the generated unique identifier. This approach should be used if you need to insert new CRM data and then correlate back to a source system without physically storing an external identifier in CRM.
Method Overloads
public IEnumerable<Entity> Create(IEnumerable<Entity> targets)
public IEnumerable<Entity> Create(IEnumerable<Entity> targets, OrganizationServiceProxyOptions options)
public IDictionary<string, Guid> Create(IDictionary<string, Entity> targets)
public IDictionary<string, Guid> Create(IDictionary<string, Entity> targets, OrganizationServiceProxyOptions options)

Each of the above methods also provides an optional exception handling parameter of type Action<TRequest, FaultException<OrganizationServiceFault>>.
Usage Examples
Each example represents a method of a sample class that contains the following property

OrganizationServiceManager Manager { get; set; }

1. How to Create entities in parallel using keyed collection
Demonstrates parallelized submission of multiple create requests. This example uses the IDictionary<TKey, TValue> method to demonstrate how an external key can be used to correlate the request (Entity) with the CRM generated unique identifier.

public IDictionary<string, Guid> ParallelCreate(IDictionary<string, Entity> targets)
{
    var responses = new Dictionary<string, Guid>();
            
    try
    {
        responses = this.Manager.ParallelProxy.Create(targets)
            .ToDictionary(t => t.Key, t => t.Value);
    }
    catch (AggregateException ae)
    {
        // Handle exceptions
    }

    foreach(var response in responses)
    {
        Console.WriteLine("Created {0} with id={1} for key={2}", 
            targets[response.Key].LogicalName,
            response.Value, 
            response.Key);
    }

    return responses;
}

2. How to Create entities in parallel using collection of entities
Demonstrates a parallelized submission of multiple create requests using a basic collection of entities. This example uses the List<Entity> method which returns the instance of the created entity hydrated with the unique identifier generated by CRM.

public List<Entity> ParallelCreateWithEntityList(List<Entity> targets)
{
    try
    {
        targets = this.Manager.ParallelProxy.Create(targets).ToList();
    }
    catch (AggregateException ae)
    {
        // Handle exceptions
    }

    targets.ForEach(t =>
    {
        Console.WriteLine("Created {0} with id={1}", t.LogicalName, t.Id);
    });

    return targets;
}

3. How to use the optional exception handler
Demonstrates parallelized submission of multiple create requests with an optional delegate exception handling function. The delegate is provided the original request and the fault exception encountered. It is executed on the calling thread after all parallel operations are complete.

public IDictionary<string, Guid> ParallelCreateWithExceptionHandler(IDictionary<string, Entity> targets)
{
    int errorCount = 0;
    var responses = new Dictionary<string, Guid>();

    try
    {                
        responses = this.Manager.ParallelProxy.Create(targets,
            (target, ex) =>
            {
                System.Diagnostics.Debug.WriteLine("Error encountered during create of entity with key={0}: {1}", target.Key, ex.Detail.Message);
                errorCount++;
            })
            .ToDictionary(t => t.Key, t => t.Value);
    }
    catch (AggregateException ae)
    {
        // Handle exceptions
    }

    Console.WriteLine("{0} errors encountered during parallel create.", errorCount);

    return responses;
}

Last edited Feb 24, 2015 at 3:09 PM by maustinjones, version 37