Sample code to retrieve more than 5000 records using FetchXML in CRM


Sharing a sample code to retrieve more than 5000 records using the Fetch XML.

Version 1 :

private List<Entity> GetTotalRecordsfromFetch(string fetchXML, IOrganizationService orgService)
List<Entity> lstEntity = new List<Entity>();

int fetchCount = 5000;
int pageNumber = 1;
string pagingCookie = null;

while (true)
// Build fetchXml string with the placeholders.
string xml = this.CreateXml(fetchXML, pagingCookie, pageNumber, fetchCount);
RetrieveMultipleRequest fetchRequest = new RetrieveMultipleRequest
Query = new FetchExpression(xml)

var returnCollections = ((RetrieveMultipleResponse)orgService.Execute(fetchRequest)).EntityCollection;

if (returnCollections.Entities.Count >= 1)

if (returnCollections.MoreRecords)

results.pagingCookie = returnCollections.PagingCookie;
// If no more records in the result nodes, exit the loop.

return lstEntity;

public string CreateXml(string xml, string cookie, int page, int count)
StringReader stringReader = new StringReader(xml);
XmlTextReader reader = new XmlTextReader(stringReader);

XmlDocument doc = new XmlDocument();

XmlAttributeCollection attrs = doc.DocumentElement.Attributes;

if (cookie != null)
XmlAttribute pagingAttr = doc.CreateAttribute("paging-cookie");
pagingAttr.Value = cookie;

XmlAttribute pageAttr = doc.CreateAttribute("page");
pageAttr.Value = System.Convert.ToString(page);

XmlAttribute countAttr = doc.CreateAttribute("count");
countAttr.Value = System.Convert.ToString(count);

StringBuilder sb = new StringBuilder(1024);
StringWriter stringWriter = new StringWriter(sb);

XmlTextWriter writer = new XmlTextWriter(stringWriter);

return sb.ToString();

Version 2 :

private static List<Entity> GetTotalRecordsFetchXML(OrganizationServiceProxy orgProxy, string fetchXML)
XDocument xDocument = XDocument.Parse(fetchXML);
var fetchXmlEntity = xDocument.Root.Element("entity").ToString();

EntityCollection entityColl = new EntityCollection();
List<Entity> lstEntity = new List<Entity>();
int page = 1;

entityColl = orgProxy.RetrieveMultiple(new FetchExpression(
string.Format("<fetch version='1.0' page='{1}' paging-cookie='{0}'>" + fetchXmlEntity + "</fetch>",
SecurityElement.Escape(entityColl.PagingCookie), page++)));

while (entityColl.MoreRecords);

return lstEntity;

Hope it helps..

List is Locked. Cannot perform this action error while trying to remove marketing list member using RemoveMemberListRequest in Dynamics 365 (CRM)

Recently we got the below error while trying to remove the marketing list member from a marketing list.

This occurs if the marketing list is locked. The marketing list gets locked if it is Dynamic Marketing List. However, we are getting this issue on our Static marketing lists. Later we realized that those static marketing lists were created using Copy To Static ribbon button. When a Dynamic Marketing List is created the locked field value is false. However it gets set as true when we convert it to Static using Copy To Static functionality.

Changing the Locked field value as No fixed the issue.

Hope this helps..

Key not valid for use in specified state while using Configuration Manager in Dynamics 365

While trying to login in Configuration Migration (DataMigrationUtility.exe), we were getting the below error in the log file.


The tool was working fine from one machine but giving an error in other. As suggested in some forum, deleting the LiveDeviceId.xml also didn’t fix the issue.

The way we got it working was to download the latest version of SDK and running the tool from there.

Missing RemoveListMembersListRequest in CRM.

In one of our recent implementations, we were working with Marketing List which could be in millions.

So we had the recent request wherein we split the marketing list into groups.

For adding members to a marketing list we have the following request


which is quite efficient as we can add multiple members in a single request.

However, when it comes to removing a member from marketing, we do not have a corresponding Remove request like RemoveListMembersListRequest.

We only have RemoveMemberListRequest, which only removes 1 member at a time, which is highly inefficient when we are taking in consideration large number of records.

Kindly vote up for this idea !!

More details

Using Shared Mailbox and Queue in CRM for implementing generic email address in Dynamics 365 and Exchange Online.

Recently, we had a requirement to send email to customers using a generic email address like from Dynamics CRM (& Exchange Online).

The way we can implement this is through Shared Mailbox and Queue in CRM Online.

Log in to the Administration Portal

Select Shared mailboxes

Members can be added to the Shared Mailbox

Now back in CRM assign the email id to a particular queue.

Now sending an email activity with From as Queue and to as one of the user inside CRM.

Open the user’s mail box.

Make sure we have done Test and Enabled Mailbox for the Shared Email Box and have set Incoming and Outgoing Email as Server-Side Synchronization.

Hope it helps..

App not accessible to certain users in Dynamics 365 (CRM)

Recently we created a new App for one our clients and gave permission to appropriate security roles to access those App.

But still some users were not able to access the App.

Eventually we realized the Security Roles (custom one) assigned was not having Read permission on App to access it.

This fixed the issue.

Hope it helps..