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)
🙂
Leave a Reply to ssahanajainSAHANA Cancel reply
Stats
- 1,259,938 hits
Translate
Categories
Top Posts
- PowerApps - Step By Step - Build a simple Calculator App
- Power Apps component framework (PCF) - Beginner guide
- [Code Snippet] Custom Workflow Activity with Input and Output Params
- [Step by Step] Postman tool to test Dynamics 365 online Web API
- Auto generate new GUID for ‘uniqueidentifier’ column in SQL Table
- CRM Entity List Export to Excel – ADX Portals
- C# - Bulk Update Records - Pass Data Table to SQL Stored Procedure
- [Step by Step] Using TypeScript in Dynamics 365 9.x
- Power Apps - Component library
- SqlError: The backup set holds a backup of a database other than the existing database
Recent Posts
- Canvas App Import Failure | CanvasAppEnvironmentMismatch error
- ‘Callback Registration Expander’ System Jobs stuck at ‘Waiting For Resources’
- [Step by Step] Debug Model Driven and Canvas Apps using ‘Monitor’ tool
- Canvas App | Dataverse | Distribute columns to multiple forms and Patch
- [Step by Step] Canvas App | Dataverse | Filter, Patch, For All, Lookup
your note saved me lot of time…
attachment[“objecttypecode”] = EntityLogicalName (i.e., email)
thanks….
Hi Rajeev,
I am rendering a Report using Plugin and emailing it as an attachment.
I face an issue of blank data in Report, though it runs and renders fine from CRM.
And all the required parameters are passed.
Actually the data where Filtered View is not used in the Dataset is coming fine, but for other entities- ACtivity Pointer and a Custom entity, I get no data when using FilteredView, and error while trying to use without Filtered.
I am running the Plugin as an Adminiastrator,
The Credentials for the REport Execution SErvice is- rs.Credentials = System.Net.CredentialCache.DefaultCredentials
Hi,
Since you are authenticating Report server from your Plug-in make sure the Plug-in context user has rights to the Report Server.
You can troubleshoot by explicitly providing credentials of User who has access to Report sever.
reportService.UseDefaultCredentials = false;
reportService.Credentials = new NetworkCredential(UserId,Password,Domain);
You can check the User’s accessibility to your report server using Report services configuration manager in Report Server. You can also browse http://servername/ReportServer.
Thanks,
Rajeev
Thank you very much Rajeev,
Some how It seems there’s problem with taking the running User for Report in case of a Plugin, When I switched to a Workflow, it worked fine!
I uses same code for creating attachment .. and my attachment body is coming from my WCF service in byte[] .. this is ok .. I am able to create attachment on create of email using plugin … but when I send this email (manually click on save) it gives me error “Unexpected error is occurred” .. if I save the attachment and remove the attachment .. again attach the same attachment from saved location.. mail sent without error .. what could be the reason of this….
got the problem … in mimetype I was using @”application\pdf” .. I updated to “text/plain” .. now working fine …. 🙂
Hi Rajeev,
I getting error while creating a new record in annotation error: 0x8004021f The parent’s object type is invalid.\n Platform\n”
I am converting pdf file into ToBase64 and creating a new record in notes which i can use further, i am using plug-in CRM 4.0
Hi,
Hope you read ‘Notes’ section in the article and passing valid ‘objecttypecode’.
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)