Skip to content

Commit 615a2fd

Browse files
Change/improvements (#16)
* Now works when there is a 'top' attribute in FetchXml.
1 parent d43f469 commit 615a2fd

File tree

3 files changed

+52
-13
lines changed

3 files changed

+52
-13
lines changed

DataMigrationUsingFetchXml/Services/ConfigReader.cs

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ namespace DataMigrationUsingFetchXml.Services
55
{
66
internal sealed class ConfigReader
77
{
8+
public static bool ContainsTopAttribute { get; set; }
9+
public static int PageNumber { get; set; }
10+
public static int PageCount { get; set; }
11+
812
public static List<string> GetPrimaryFields(string fetchXml, out bool idExists)
913
{
1014
idExists = false;
@@ -29,7 +33,30 @@ public static List<string> GetPrimaryFields(string fetchXml, out bool idExists)
2933
return searchAttrs;
3034
}
3135

32-
public static string CreateXml(string xml, string cookie, int page, int count)
36+
public static void SetPaginationAttributes(string fetchXml)
37+
{
38+
PageNumber = 1;
39+
PageCount = 5000;
40+
ContainsTopAttribute = false;
41+
XmlDocument xmlDoc = new XmlDocument();
42+
xmlDoc.LoadXml(fetchXml);
43+
XmlNodeList fetchNodes = xmlDoc.DocumentElement.SelectNodes("/fetch");
44+
45+
if (fetchNodes[0].Attributes["top"] != null)
46+
{
47+
ContainsTopAttribute = true;
48+
}
49+
if (fetchNodes[0].Attributes["count"] != null)
50+
{
51+
PageCount = System.Convert.ToInt32(fetchNodes[0].Attributes["count"].Value);
52+
}
53+
if (fetchNodes[0].Attributes["page"] != null)
54+
{
55+
PageNumber = System.Convert.ToInt32(fetchNodes[0].Attributes["page"].Value);
56+
}
57+
}
58+
59+
public static string CreateXml(string xml, string cookie)
3360
{
3461
// Load document
3562
XmlDocument doc = new XmlDocument();
@@ -45,11 +72,11 @@ public static string CreateXml(string xml, string cookie, int page, int count)
4572
}
4673

4774
XmlAttribute pageAttr = doc.CreateAttribute("page");
48-
pageAttr.Value = System.Convert.ToString(page);
75+
pageAttr.Value = System.Convert.ToString(PageNumber);
4976
attrs.Append(pageAttr);
5077

5178
XmlAttribute countAttr = doc.CreateAttribute("count");
52-
countAttr.Value = System.Convert.ToString(count);
79+
countAttr.Value = System.Convert.ToString(PageCount);
5380
attrs.Append(countAttr);
5481

5582
return doc.OuterXml;

DataMigrationUsingFetchXml/Services/Implementations/DataverseService.cs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,7 @@ internal sealed class DataverseService : IDataverseService
1414
private readonly IOrganizationService _sourceService;
1515
private readonly IOrganizationService _targetService;
1616
private readonly ILogger _logger;
17-
private int pageNumber = 1;
1817
private string pagingCookie = null;
19-
private readonly int fetchCount = 5000;
2018

2119
public DataverseService(IOrganizationService sourceService, IOrganizationService targetService, ILogger logger)
2220
{
@@ -32,12 +30,19 @@ public DataverseService(IOrganizationService service)
3230

3331
public EntityCollection GetAllRecords(string fetchQuery)
3432
{
35-
string xml = ConfigReader.CreateXml(fetchQuery, pagingCookie, pageNumber, fetchCount);
33+
string xml;
34+
if (ConfigReader.ContainsTopAttribute)
35+
{
36+
xml = fetchQuery;
37+
}
38+
else
39+
{
40+
xml = ConfigReader.CreateXml(fetchQuery, pagingCookie);
41+
}
3642
EntityCollection returnCollection = _sourceService.RetrieveMultiple(new FetchExpression(xml));
37-
3843
if (returnCollection.MoreRecords)
3944
{
40-
++pageNumber;
45+
++ConfigReader.PageNumber;
4146
pagingCookie = returnCollection.PagingCookie;
4247
}
4348
return returnCollection;
@@ -114,7 +119,10 @@ private Entity GetRecord(string entitySchemaName, string attributeSchemaName, st
114119
public (string logicalName, string displayName) GetEntityName(string fetchXml)
115120
{
116121
EntityCollection returnCollection = _sourceService.RetrieveMultiple(new FetchExpression(fetchXml));
117-
122+
if (returnCollection.Entities.Count == 0)
123+
{
124+
throw new Exception($"Records not found: '{returnCollection.EntityName}'");
125+
}
118126
RetrieveEntityRequest retrieveEntityRequest = new RetrieveEntityRequest
119127
{
120128
EntityFilters = EntityFilters.All,

DataMigrationUsingFetchXml/Services/Implementations/TransferOperation.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,15 @@ public void Transfer(List<string> fetchXmls, List<int> tableIndexesForTransfer,
6161

6262
foreach (string fetchXml in fetchXmls)
6363
{
64+
ConfigReader.SetPaginationAttributes(fetchXml);
6465
_resultItem = new ResultItem();
6566
_lblTitle.Text = $"Migrating {DisplayNames[tableIndexesForTransfer[index]]} records";
6667
List<string> searchAttrs = ConfigReader.GetPrimaryFields(fetchXml, out bool idExists);
6768
EntityCollection records = _dataverseService.GetAllRecords(fetchXml);
68-
69-
_logger.LogInfo("Getting data of '" + records.Entities[0].LogicalName + "' from source instance");
70-
_resultItem.EntityName = records.Entities[0].LogicalName;
69+
_logger.LogInfo("Getting data of '" + records.EntityName + "' from source instance");
70+
_resultItem.EntityName = records.EntityName;
7171
_logger.LogInfo("Transfering data to: " + _organizationDataServiceUrl);
72+
7273
do
7374
{
7475
_resultItem.SourceRecordCount += records.Entities.Count;
@@ -112,7 +113,6 @@ public void Transfer(List<string> fetchXmls, List<int> tableIndexesForTransfer,
112113
_logger.LogError("Process Stopped. Aborting! ");
113114
break;
114115
}
115-
116116
if (!records.MoreRecords)
117117
{
118118
ResultItems.Add(_resultItem);
@@ -129,6 +129,10 @@ public void Transfer(List<string> fetchXmls, List<int> tableIndexesForTransfer,
129129
}
130130
_lblInfo.Text = $"{_resultItem.SuccessfullyGeneratedRecordCount} of { _resultItem.SourceRecordCountWithSign} {DisplayNames[tableIndexesForTransfer[index]]} is imported";
131131
}
132+
else
133+
{
134+
_lblError.Text = string.Empty;
135+
}
132136
index++;
133137
}
134138
}

0 commit comments

Comments
 (0)