Archive
File name ABC.pdf is invalid – error while creating attachment programmatically
The other day I was getting “Invalid file name” error when I try to create an attachment with .pdf file and associating to Email, in my plug-in. (link)
Reason
- Reason was “pdf” was added as blocked file extension for attachments in my CRM organization.
Fix :-
- Go to “Settings -> Administration -> System Settings”
- Remove pdf from “Blocked file extension for attachment”
- Click ‘OK’
🙂
Adding file as “Attachment” to activities programmatically in plug-in
Hi,
We can add file as attachment and associate to the CRM objects i.e., Email, Task etc…
The entity that store attachment information is “activitymimeattachment”
Below is the code snippet to add “.pdf” file as attachment to the Email activity
Entity attachment = new Entity(“activitymimeattachment”);
attachment[“subject”] = “My Subject”;
string fileName = “Sample.pdf”;
attachment[“filename”] = fileName;
byte[] fileStream= = new byte[] { }; //Set file stream bytes
attachment[“body”] = Convert.ToBase64String(fileStream);
attachment[“mimetype”] = “text/plain”;
attachment[“attachmentnumber”] = 1;
Guid emailId; //GUID of Email activity
attachment[“objectid”] = new EntityReference(“email”, emailId);
attachment[“objecttypecode”] = “email”;
IOrganizationService service;
service.Create(attachment);
Note :-
- In the above code the property “objecttypecode” is slight misleading, as it expects “EntityLogicalName” rather than “EntityTypeCode”
- So make sure to set attachment[“objecttypecode”] = EntityLogicalName (i.e., email)
🙂
Stats
- 1,242,678 hits
Translate
Categories
Top Posts
- [Step by Step] Postman tool to test Dynamics 365 online Web API
- Power Apps component framework (PCF) - Beginner guide
- Auto generate new GUID for ‘uniqueidentifier’ column in SQL Table
- Useful JScript syntax's – ADX/Dynamics 365 Portals
- [Step by Step] Restore a Database from Azure Blob to Azure SQL Server
- [Code Snippet] Custom Workflow Activity with Input and Output Params
- [MS Word] The Linked file isn't available
- [Step by Step] Data Export Service - Replicate CDS data to Azure SQL Server Database
- Aggregate Query Record Limit Exceeded – Exception in CRM
- C# - Bulk Update Records - Pass Data Table to SQL Stored Procedure