Using the Bulk API to Create Records

I wanted to build a data populator for Dynamics that created a bunch of custom records and did some associations between them.

(The reasoning is so I could be sure I was always validating the same thing).

To do this, I wanted to use the Bulk API.  The Bulk API has a limit of 1000 requests per message so in addition to bulk loading my requests, I needed to ensure that multiple requests could be queued up and handled by the server.

Here is some of the sample code I wrote for this interaction.

public void BulkDispatchCreateRequests<T>(List<T> requests)
 int MaxBatchSize = 999;
 bool CreateBatchRequest = true;

ExecuteMultipleRequest requestWithResults = null;

for (int i = 0; i < requests.Count(); i++)
 if (CreateBatchRequest)
 requestWithResults = new ExecuteMultipleRequest()
 // Assign settings that define execution behavior: continue on error, return responses. 
 Settings = new ExecuteMultipleSettings()
 ContinueOnError = false,
 ReturnResponses = true
 // Create an empty organization request collection.
 Requests = new OrganizationRequestCollection()
 requestWithResults.Requests.Add(requests[i] as OrganizationRequest);

if (requestWithResults.Requests.Count() == MaxBatchSize || requestWithResults.Requests.Count() == requests.Count())
 ExecuteMultipleResponse responseWithResults = (ExecuteMultipleResponse)_service.Execute(requestWithResults);
 CreateBatchRequest = true;
 System.Diagnostics.Debug.WriteLine("Bulk Accessor Request Sent.");
 catch (Exception ex)
 System.Diagnostics.Debug.WriteLine("Could not bulk create Accessor - error: " + ex.ToString());
 CreateBatchRequest = false;

I used 999 as my max batch size to proactively stay under the 1,000 item limit.

The big piece of code here is that all my record Create requests are batched and associated to a ExecuteMultipleRequest. Once this is all done you bundle into the Execute request and send them off.

I got a bit fancy in this implementation and created a generic function called BulkDispatchCreateRequests which allows me drop in Associate, Delete, Disassociate requests that can all be bulk handled and loaded without having to rewrite the function.

The implementation then becomes pretty simple in other areas of my code.



Dynamics SDK Paging Records

I never thought I’d have a case when I’d need to work with over 5,000 records from the Dynamics SDK.

The scenario here is that I was trying to reproduce some production issues and validate out some code changes with a higher threshold of objects.  However, as I started testing, I noticed I wasn’t bringing back everything and had hit the that magical 5,000 limit.

Without wanting to change it, I modified by query to support the retrieval of 5,000+ items that would return to my service to be worked on.

bool KeepSearching = true;
 EntityCollection results = null;
 Dictionary <Guid, Entity> Accessors = new Dictionary<Guid, Entity>();

QueryExpression query = new QueryExpression();
 query.EntityName = "contact";
 query.Criteria.AddCondition(new ConditionExpression("contactid", ConditionOperator.Equal, ContactId));
 query.ColumnSet = new ColumnSet(new string[] { "contactid", "fullname"});
 query.PageInfo = new PagingInfo();
  query.PageInfo.PageNumber = 1;

results = _service.RetrieveMultiple(query);

while (KeepSearching)
 AppendContactResults(Accessors , results);

if (results.MoreRecords)
 query.PageInfo.PagingCookie = results.PagingCookie;
 results = _service.RetrieveMultiple(query);
 KeepSearching = false;

The code itself is pretty simple – I append the paging info and request for the first page to come back (note even if you do not exceed the first page, you don’t need to change this code).

When the results are returned, I check if there are moreresults.  If there are no more results, I process what I have, kill the while loop and move on.  If there ARE more results, I queue up my paging information, set the watermark (the PagingCookie) and make another call to see what I missed.

The loop takes over and will close itself when there are no more results.


Retrieving Configuration Values from the USD

In the USD you can create Options that you can use within your Actions/Events/etc in the Unified Service Desk Configuration.

You can also access these values from within your own control via C# relatively easily.


To access the value within my USD Custom Hosted Control via C# all that is needed is the following code.


And now I can use this value within the USD without having to create my own configuration files et al in C#.