Downloading Note Attachments Using Paging

Introduction


This article is about downloading note attachments from Dynamics 365. If you have a large number of records from where you want to download an attachment, you can face different issues such as a timeout, slow downloading, etc. In this article, I am going to provide sample code that you can use to download attachments from a small set of data.
 

Details


To download an attachment from Dynamics, we can query the notes entity. We can control data retrieval based on the query, but sometimes, our source dataset is more than 5000 so we need to use paging cookie in our retrieve multiple query. While query our entity data if we have used paging cookie feature, the result contains a value for the paging cookie which we can use to fetch the next pages.
 
However, even while fetching default 5000 records, depending on the notes attachment size, it can talk lot of time to query. Recently, while working a similar requirement, I faced an issue while fetching records. I used the following code to fetch a small number of records using paging.
  1. public void DownloadNotesAttachments(QueryExpression query) {  
  2.  //number of records you want to query  
  3.  int queryCount = 250;  
  4.  // Initialize the page number.  
  5.  int pageNumber = 1;  
  6.  // Initialize the number of records.  
  7.  int recordCount = 0;  
  8.    
  9.  // Assign the pageinfo properties.  
  10.  query.PageInfo = new PagingInfo();  
  11.  query.PageInfo.Count = queryCount;  
  12.  query.PageInfo.PageNumber = pageNumber;  
  13.  query.PageInfo.PagingCookie = null;  
  14.    
  15.  while (true) {  
  16.   EntityCollection notes = service.RetrieveMultiple(query);  
  17.   if (notes.Entities != null) {  
  18.    foreach(Entity note in notes.Entities) {  
  19.     string name = note.GetAttributeValue < string > ("filename");  
  20.     string doc_content = note.GetAttributeValue < string > ("documentbody");  
  21.     byte[] fileContent = Convert.FromBase64String(doc_content);  
  22.     String Location = @ "Folder Path Where you want to download files";  
  23.     String filename = note.GetAttributeValue < String > ("filename");  
  24.     String noteBody = note.GetAttributeValue < String > ("documentbody");  
  25.    
  26.     string outputFileName = @ "" + Location + "\\" + filename;  
  27.     System.IO.File.WriteAllBytes(outputFileName, fileContent);  
  28.    }  
  29.   }  
  30.   // Check for more records, if it returns true.  
  31.   if (notes.MoreRecords) {  
  32.    query.PageInfo.PageNumber++;  
  33.    query.PageInfo.PagingCookie = notes.PagingCookie;  
  34.   } else {  
  35.    break;  
  36.   }  
  37.  }  
  38.  Console.WriteLine("All files downloaded,Press any key to exit...");  
  39.    
  40. }  
In the above code, I am downloading the notes attachment based on the query parameter, as I am retrieving a small number of records (250) per page so it will start downloading the files quickly.
 
Hope this will help someone!!


Similar Articles
HIMBAP
We are expert in Microsoft Power Platform.