Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Unanswered

Copy Attached File From One Entity's Record To Another Of File Data Type In C#

(0) ShareShare
ReportReport
Posted on by 89

Hey There,

Hope you all guys have been doing well, Anyway I need help of yours for New Field (Column) "File Type" when it comes to C# whether it is Plugin or Custom Workflow Activity.

I do have two custom entity as "Document" & "Case Document" which have same data type as File Wherein it has relationship as (1:N).

Requirement :

I want to attach or transfer File which is there on Document record to Case Document records  whenever new record is created.

Issue :

I am trying to write C# wherein i am able to retrieve the file information along with download but when it comes to upload I seem to have done great job but when i try to retrieve the Case Document record i get to see the file information in console application but it not getting shown in Dynamics 365 record. (Please do check my code and let me know perhaps i have forgotten something.)


string fileNM = "doc_template";

var initRequest = new InitializeFileBlocksDownloadRequest()
{ FileAttributeName = fileNM, Target = new EntityReference("SourceEntityName", SourceRecordID) };

var initResponse = (InitializeFileBlocksDownloadResponse)service.Execute(initRequest);


byte[] File;
var increment = 4194304;
var from = 0;
var fileSize = initResponse.FileSizeInBytes;
byte[] downloaded = new byte[fileSize];
var fileContinuationToken = initResponse.FileContinuationToken;

while (from < fileSize)
{
var blockRequest = new DownloadBlockRequest() { Offset = from, BlockLength = increment, FileContinuationToken = fileContinuationToken };
var blockResponse = (DownloadBlockResponse)service.Execute(blockRequest);
blockResponse.Data.CopyTo(downloaded, from);
from += increment;
}

File = downloaded;  

//Till Now I have retrieved as well as downloaded the contents of File type from Source Entity's Record.

//Now I am about to start procedure for uploading the downloaded file into Destination Entity's Record.

string fileNM1 = "Casedoc_template";

var initRequest2 = new InitializeFileBlocksUploadRequest()
{
FileAttributeName = fileNM1,
FileName = initResponse.FileName,
Target = new EntityReference("DestinationEntityName", DestinationRecordGuid)
};
var initResponse2 = (InitializeFileBlocksUploadResponse)service.Execute(initRequest2);

var limit = 4194304;
var fileContinuationToken2 = initResponse2.FileContinuationToken;
var blockIds = new List<string>();


for (int i = 0; i < Math.Ceiling(File.Length / Convert.ToDecimal(limit)); i++)
{
var blockId = Convert.ToBase64String(Encoding.UTF8.GetBytes(Guid.NewGuid().ToString()));
blockIds.Add(blockId);
var blockData = File.Skip(i * limit).Take(limit).ToArray();
var blockRequest = new UploadBlockRequest() { FileContinuationToken = fileContinuationToken, BlockId = blockId, BlockData = blockData };
var blockResponse = (UploadBlockResponse)service.Execute(blockRequest);
}

var commitRequest = new CommitFileBlocksUploadRequest()
{
BlockList = blockIds.ToArray(),
FileContinuationToken = fileContinuationToken,
FileName = initResponse.FileName,
MimeType = @"application/octet-stream"
};

service.Execute(commitRequest);

// Finally it appears that i have been able to upload the downloaded file from source entity's record to destination entity's record. However it is not getting shown in Dynamics 365 record. But when i try to retrieve that destination entity's record in Console Application then it get to see File Name but not the contents at all.


8345.JPG2068.JPG

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Vahid Ghafarpour – Community Spotlight

We are excited to recognize Vahid Ghafarpour as our February 2025 Community…

Tip: Become a User Group leader!

Join the ranks of valued community UG leaders

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,494 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,305 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans