Skip to content

Commit

Permalink
Added possibility to add parameters for RetrieveMultiple requests
Browse files Browse the repository at this point in the history
  • Loading branch information
rappen committed Oct 4, 2024
1 parent f369db2 commit ae98cf6
Showing 1 changed file with 51 additions and 5 deletions.
56 changes: 51 additions & 5 deletions Rappen.XRM.Helpers/Extensions/ServiceExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Microsoft.Crm.Sdk.Messages;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Messages;
using Microsoft.Xrm.Sdk.Metadata;
using Microsoft.Xrm.Sdk.Organization;
using Microsoft.Xrm.Sdk.Query;
Expand Down Expand Up @@ -126,9 +127,34 @@ public static string GetEntityFormUrl(this IOrganizationService service, EntityR
/// {records} - retrieved records until now
/// {timeperrecord} - avarage of time to retrieve each record
/// </param>
/// <param name="showMessageOnFirstPage"></param>
/// <returns></returns>
/// <exception cref="ArgumentException"></exception>
public static EntityCollection RetrieveMultipleAll(this IOrganizationService service, QueryBase query, BackgroundWorker worker, DoWorkEventArgs eventargs, string message, bool showMessageOnFirstPage)
public static EntityCollection RetrieveMultipleAll(this IOrganizationService service, QueryBase query, BackgroundWorker worker, DoWorkEventArgs eventargs, string message, bool showMessageOnFirstPage) => RetrieveMultiple(service, query, worker, eventargs, message, showMessageOnFirstPage, true);

/// <summary>
/// Retrieving records from Dataverse
/// </summary>
/// <param name="service"></param>
/// <param name="query"></param>
/// <param name="worker"></param>
/// <param name="eventargs"></param>
/// <param name="message">
/// Progress message send before each page retrieving.
/// Possible tokens:
/// {retrieving} - which records we are now retrieving
/// {page} - which page with are retrieving
/// {pagesize} - the size of the page to retrieve
/// {time} - how much time it has taken
/// {records} - retrieved records until now
/// {timeperrecord} - avarage of time to retrieve each record
/// </param>
/// <param name="showMessageOnFirstPage"></param>
/// <param name="allpages"></param>
/// <param name="parameters"></param>
/// <returns></returns>
/// <exception cref="ArgumentException"></exception>
public static EntityCollection RetrieveMultiple(this IOrganizationService service, QueryBase query, BackgroundWorker worker, DoWorkEventArgs eventargs, string message, bool showMessageOnFirstPage, bool allpages, params KeyValuePair<string, object>[] parameters)
{
if (!(query is FetchExpression || query is QueryExpression))
{
Expand All @@ -138,9 +164,11 @@ public static EntityCollection RetrieveMultipleAll(this IOrganizationService ser
EntityCollection tmpResult = null;
if (string.IsNullOrEmpty(message))
{
message = "Retrieving records {retrieving} on page {page}\nRetrieved {records} in {time}";
message = allpages ?
"Retrieving records {retrieving} on page {page}\nRetrieved {records} in {time}" :
"Retrieving records {retrieving}";
}
if (query is QueryExpression queryex && queryex.PageInfo.PageNumber == 0 && queryex.TopCount == null)
if (allpages && query is QueryExpression queryex && queryex.PageInfo.PageNumber == 0 && queryex.TopCount == null)
{
queryex.PageInfo.PageNumber = 1;
}
Expand All @@ -163,7 +191,7 @@ public static EntityCollection RetrieveMultipleAll(this IOrganizationService ser
{
query.NavigatePage(tmpResult.PagingCookie);
}
tmpResult = service.RetrieveMultiple(query);
tmpResult = RetrieveMultiple(service, query, parameters);
if (resultCollection == null)
{
resultCollection = tmpResult;
Expand All @@ -177,10 +205,28 @@ public static EntityCollection RetrieveMultipleAll(this IOrganizationService ser
resultCollection.TotalRecordCountLimitExceeded = tmpResult.TotalRecordCountLimitExceeded;
}
}
while (tmpResult.MoreRecords && eventargs?.Cancel != true);
while (allpages && tmpResult.MoreRecords && eventargs?.Cancel != true);
return resultCollection;
}

public static EntityCollection RetrieveMultiple(this IOrganizationService service, QueryBase query, KeyValuePair<string, object>[] parameters)
{
if (parameters != null && parameters.Length > 0)
{
var req = new RetrieveMultipleRequest
{
Query = query
};
req.Parameters.AddRange(parameters);
var resp = (RetrieveMultipleResponse)service.Execute(req);
return resp.EntityCollection;
}
else
{
return service.RetrieveMultiple(query);
}
}

private static string GetProgress(string message, int retrievedrecords, int pagesize, int page, Stopwatch sw)
{
return message.Contains("{0}") ?
Expand Down

0 comments on commit ae98cf6

Please sign in to comment.