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 Cancel reply
Stats
- 1,581,533 hits
Tweets
- RT @MikeFactorial: Big things are coming to the #CoEStarterKit. So, @ManuelaPichler_ and team are building the tension for the April releas… 3 weeks ago
- New blog post : Boost conversations using GPT in Power Virtual Agent (PVA) rajeevpentyala.com/2023/03/08/pow… 3 weeks ago
- RT @PPDevWeekly: 🔥 Going Live: Power Platform Dev Weekly 155 bit.ly/PPDevWeekly155 This week's cover story by @RajeevPentyala With gre… 3 weeks ago
- RT @caseyburke21: Pipelines is now generally available! Huge congratulations to the team and all who have supported reaching this milestone… 1 month ago
- New blog post : Call Dataverse actions directly in power-fx rajeevpentyala.com/2023/03/01/ste… 1 month ago
Top Posts
- [Code Snippet] Custom Workflow Activity with Input and Output Params
- [Step by Step] Power Apps | Show pop ups in Canvas App
- Power Apps component framework (PCF) - Beginner guide
- Power Automate Cloud Flow | 'Correct to include a valid reference' error
- God Mode - Level Up - Dynamics 365 Chrome Extension
- Power Apps | Business Rule | Set Field Value vs Set Default Value
- Set “Created On”,” Created By”, “Modified On”, “Modified By” fields using SDK/Data Import/Plug-in – Dynamics 365
- Useful JScript syntax's – ADX/Dynamics 365 Portals
- The server was unable to process the request due to an internal error - plugin registration tool
- Auto generate new GUID for ‘uniqueidentifier’ column in SQL Table
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)