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.