web
You’re offline. This is a read only version of the page.
close
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

I have the same question (0)

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Tom_Gioielli Profile Picture

Tom_Gioielli 137 Super User 2025 Season 2

#2
#ManoVerse Profile Picture

#ManoVerse 57

#3
Jimmy Passeti Profile Picture

Jimmy Passeti 50 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans