Mastering the Technique of Extracting File Content from Notes and Updating Another Table: A Developer's Guide
- Sunil Thomas
- Aug 6, 2024
- 2 min read
The process consists of two steps: first, we extract the file content, and then we upload it into another table.
Extracting the file content
We retrieve the file content and convert it into a byte[]. This byte[] is then passed to a method that uploads it into another table. In this scenario, we have an attachment in a note associated with a table named AddNotes. We aim to extract this attachment from the notes and load it into a different column within the AddNotes table. It is assumed that there is only one attachment. Please see the image below.
Uploading the file content
In this case the entity reference will be AddNotes entity reference and the attributename will be the column in AddNotes where this file will be stored. So the attributename will be of the type File. The last parameter is the name of the file that we are uploading.
You may use the code below
var notesQry = new QueryExpression("annotation");
notesQry.ColumnSet = new ColumnSet("createdon", "documentbody","filename");
notesQry.Criteria.FilterOperator = LogicalOperator.And;
notesQry.Criteria.AddCondition("objectid", ConditionOperator.Equal, AddNotes.Id);
EntityCollection reissueNotesRecords = service.RetrieveMultiple(notesQry);
foreach (var reissueNotesRecord in reissueNotesRecords.Entities)
{
string notesDoc = (string)reissueNotesRecord["documentbody"];// getting the file content
byte[] fileContent = Convert.FromBase64String(notesDoc); // Converting to byte[]
UploadFile(service, AddNotes, "AttributeName", fileContent,(string) reissueNotesRecord["filename"]);
}
public void UploadFile(IOrganizationService service, EntityReference recordReference,string attributeName, byte[] fileBytes, string fileName)
{
string fileExtension = Path.GetExtension(fileName);
// Determine MIME type based on file extension
string mimeType;
switch (fileExtension?.ToLowerInvariant())
{
case ".pdf":
mimeType = "application/pdf";
break;
case ".jpeg":
case ".jpg":
mimeType = "image/jpeg";
break;
case ".png":
mimeType = "image/png";
break;
case ".txt":
mimeType = "text/plain";
break;
default:
mimeType = "application/pdf"; // Default to PDF if not PDF, JPEG, PNG, or TXT
break;
}
// Upload file in blocks
var blockSize = 4194304; // Block size for uploading 4194304 ~ 4MB
var blockIds = new List<string>();
// Initialize file upload request
var initializeFileUploadRequest = new InitializeFileBlocksUploadRequest
{
FileAttributeName = attributeName,
Target = recordReference,
FileName = fileName
};
// Execute the initialization request
var fileUploadResponse = (InitializeFileBlocksUploadResponse)service.Execute(initializeFileUploadRequest);
// Upload file in blocks
for (int i = 0; i < Math.Ceiling(fileBytes.Length / Convert.ToDecimal(blockSize)); i++)
{
var blockId = Convert.ToBase64String(Encoding.UTF8.GetBytes(Guid.NewGuid().ToString()));
blockIds.Add(blockId);
var blockData = fileBytes.Skip(i * blockSize).Take(blockSize).ToArray();
var blockRequest = new UploadBlockRequest()
{
FileContinuationToken = fileUploadResponse.FileContinuationToken,
BlockId = blockId,
BlockData = blockData
};
// Execute block upload request
var blockResponse = (UploadBlockResponse)service.Execute(blockRequest);
}
// Commit the file upload
var commitRequest = new CommitFileBlocksUploadRequest()
{
BlockList = blockIds.ToArray(),
FileContinuationToken = fileUploadResponse.FileContinuationToken,
FileName = fileName,
MimeType = mimeType
};
// Execute the commit request
service.Execute(commitRequest);
}
Stay tuned for more development insights and guides to empower your coding journey!
Follow along for more developer tips and tricks as we navigate the intricacies of the digital landscape. Let's elevate our skills together!
Comments