public StringBuilder ExecuteFetchXmlQuery(string entityName, string fetchXml, string[] systemFieldsToExclude, bool forComparison = false) { var queryResultXml = new StringBuilder(); if (string.IsNullOrWhiteSpace(entityName) || string.IsNullOrWhiteSpace(fetchXml)) { return(queryResultXml); } // convert fetchxml to query expression var query = ConvertFetchXmlToQueryExpression(fetchXml); var systemFields = new List <string>(); if (systemFieldsToExclude == null) { // match this with "ColumnsToExcludeToCompareData" in app.config - this is included here in case there is no exclusion in the fetch!!! const string columnsToExclude = "languagecode;createdon;createdby;modifiedon;modifiedby;owningbusinessunit;owninguser;owneridtype;" + "importsequencenumber;overriddencreatedon;timezoneruleversionnumber;operatorparam;utcconversiontimezonecode;versionnumber;" + "customertypecode;matchingentitymatchcodetable;baseentitymatchcodetable;slaidunique;slaitemidunique"; systemFields.AddRange(columnsToExclude.Split(';')); } else { systemFields.AddRange(systemFieldsToExclude); } systemFields.Sort(); var sourceDataLoader = new DataLoader(OrganizationService); var entityMetadata = sourceDataLoader.GetEntityMetaData(query.EntityName, EntityFilters.Attributes); // exclude all fields tha are in the system fields or IsValidForRead=false or isValidForCreate = false if (query.ColumnSet.AllColumns) { query.ColumnSet.AllColumns = false; foreach (var attributeMetadata in entityMetadata.Attributes) { if (systemFields.Contains(attributeMetadata.LogicalName) || attributeMetadata.IsValidForRead == false || attributeMetadata.IsValidForCreate == false) { // ignore system fields or those can't be read or used for created } else { query.ColumnSet.AddColumn(attributeMetadata.LogicalName); } } } var queryResults = OrganizationService.RetrieveMultiple(query); if (queryResults.Entities.Count == 0) { return(queryResultXml); } queryResultXml.AppendLine($"<EntityData Name='{entityName}'>"); foreach (var queryResult in queryResults.Entities) { if (!forComparison) { foreach (var attributeMetadata in entityMetadata.Attributes) { if (systemFields.Contains(attributeMetadata.LogicalName) || attributeMetadata.IsValidForCreate == false) { // these columns should not be included (even if they are included in the query) } else if (query.ColumnSet != null) { // if the fetchXml contains the column but the column is not in the queryResult because it has null value // then we need to add the column in the queryResult so that the column-value is replaced in the target if (query.ColumnSet.Columns.Contains(attributeMetadata.LogicalName) && !queryResult.Attributes.Contains(attributeMetadata.LogicalName)) { queryResult.Attributes.Add(attributeMetadata.LogicalName, null); } } } } if ((queryResult.RowVersion != null) && forComparison) { queryResult.RowVersion = null; } var typeList = new List <Type> { queryResult.GetType() }; queryResultXml.AppendLine(DataContractSerializeObject(queryResult, typeList)); } queryResultXml.AppendLine("</EntityData>"); return(queryResultXml); }