Tech
News
Videos
Forums
Jobs
Books
Events
More
Interviews
Live
Learn
Training
Career
Members
Blogs
Challenges
Certification
Contribute
Article
Blog
Video
Ebook
Interview Question
Collapse
Feed
Dashboard
Wallet
Learn
Achievements
Network
Refer
Rewards
SharpGPT
Premium
Contribute
Article
Blog
Video
Ebook
Interview Question
Register
Login
Downloading Note Attachments Using Paging
WhatsApp
Mahender Pal
4y
12.3k
0
2
100
Article
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.
public
void
DownloadNotesAttachments(QueryExpression query) {
//number of records you want to query
int
queryCount = 250;
// Initialize the page number.
int
pageNumber = 1;
// Initialize the number of records.
int
recordCount = 0;
// Assign the pageinfo properties.
query.PageInfo =
new
PagingInfo();
query.PageInfo.Count = queryCount;
query.PageInfo.PageNumber = pageNumber;
query.PageInfo.PagingCookie =
null
;
while
(
true
) {
EntityCollection notes = service.RetrieveMultiple(query);
if
(notes.Entities !=
null
) {
foreach
(Entity note
in
notes.Entities) {
string
name = note.GetAttributeValue <
string
> (
"filename"
);
string
doc_content = note.GetAttributeValue <
string
> (
"documentbody"
);
byte
[] fileContent = Convert.FromBase64String(doc_content);
String Location = @
"Folder Path Where you want to download files"
;
String filename = note.GetAttributeValue < String > (
"filename"
);
String noteBody = note.GetAttributeValue < String > (
"documentbody"
);
string
outputFileName = @
""
+ Location +
"\\"
+ filename;
System.IO.File.WriteAllBytes(outputFileName, fileContent);
}
}
// Check for more records, if it returns true.
if
(notes.MoreRecords) {
query.PageInfo.PageNumber++;
query.PageInfo.PagingCookie = notes.PagingCookie;
}
else
{
break
;
}
}
Console.WriteLine(
"All files downloaded,Press any key to exit..."
);
}
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!!
Download Attachment using SDK
Dynamics CRM
Up Next
Ebook Download
View all
Printing in C# Made Easy
Read by 22.4k people
Download Now!
Learn
View all
HIMBAP
We are expert in Microsoft Power Platform.
Membership not found