I am writing a web resource to save an image as an attachment to an email.
What I am having trouble with is how to save a dataImage URL as an attachment to a bullets email which is currently opened.
I was hoping there would be a way using Xrm to add the image as an attachment if not then how would I add it using the web api.
Below is an image of the form I am working on. I want to use XRM to add an image as an attachment through javascript. If this is not possible how could I do it through the CRM rest api?
Thanks in advance,
Steven.
Hi Steven,
Good to know that its working now. Is the entity name "email" case sensitivity the reason? Please share us how you fixed your error.
Also mark this as answer to close the loop.
gist.github.com/.../61150022552056a4c3ed8b5fd52675a7
Regards,
Praveen T
D365 CE Support
Microsoft Corporation
I got it working thanks for your help!
Hi Praveen,
I am not sure how to send images on this forum. But I can explain what I did and what happened.
At the bottom is the response message I got for creating the attachment. I will also provide the code I wrote.
Essentially what I did was I got an ID where I had a data url stored. I received the data url with a get request then I created a new attachment using the activity id and the data url. The response below is from the post request when creating the attachment.
Function ---------------------------------------------------------------------
function testEmail() {
var caseguid = Xrm.Page.data.entity.getId();
caseguid = Xrm.Page.getAttribute("new_incidentid").getValue();
caseguid = caseguid.replace("{", "");
caseguid = caseguid.replace("}", "");
(Imported library).Utilities.Retrieve("incidents(" + caseguid + ")?$select=incidentid,new_temp_image", function (imageData) {
if (imageData) {
// TODO: Get request to retrieve the activitiy id of the email.
var activityId = Xrm.Page.data.getEntity().getId().replace(/[{|}]/g, '');;
var activityType = "Email"; //or any other entity type
var entity = {};
entity["objectid_activitypointer@odata.bind"] = "/activitypointers(" + activityId + ")";
entity.body = imageData.new_temp_image;
entity.filename = "test";
entity.subject = "test";
entity.objecttypecode = activityType;
var req = new XMLHttpRequest();
req.open("POST", Xrm.Page.context.getClientUrl() + "/api/data/v8.2/activitymimeattachments", true);
req.setRequestHeader("OData-MaxVersion", "4.0");
req.setRequestHeader("OData-Version", "4.0");
req.setRequestHeader("Accept", "application/json");
req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
req.onreadystatechange = function () {
if (this.readyState === 4) {
req.onreadystatechange = null;
if (this.status === 204) {
var uri = this.getResponseHeader("OData-EntityId");
var regExp = /\(([^)]+)\)/;
var matches = regExp.exec(uri);
var newEntityId = matches[1];
} else {
Xrm.Utility.alertDialog(this.statusText);
}
}
};
req.send(JSON.stringify(entity));
}
});
}
PS. I have checked and can confirm that the ID's are grabbed and populated. Also the body is a data url.
I will take a look at the server to see if it tells me anything.
Error from creating attachment post request-------------------------------
{
"error":{
"code":"","message":"Email","innererror":{
"message":"Email","type":"System.ServiceModel.FaultException`1[[Microsoft.Xrm.Sdk.OrganizationServiceFault, Microsoft.Xrm.Sdk, Version=8.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]]","stacktrace":" at Microsoft.Crm.Extensibility.OrganizationSdkServiceInternal.Create(Entity entity, CorrelationToken correlationToken, CallerOriginToken callerOriginToken, WebServiceType serviceType, Boolean checkAdminMode, Dictionary`2 optionalParameters)\r\n at Microsoft.Crm.Extensibility.OData.CrmODataExecutionContext.Create(Entity entity)\r\n at Microsoft.Crm.Extensibility.OData.CrmODataServiceDataProvider.CreateEdmEntity(CrmODataExecutionContext context, String edmEntityName, EdmEntityObject entityObject, Boolean isUpsert)\r\n at Microsoft.Crm.Extensibility.OData.EntityController.PostEntitySet(String entitySetName, EdmEntityObject entityObject)\r\n at lambda_method(Closure , Object , Object[] )\r\n at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.<>c__DisplayClass10.<GetExecutor>b__9(Object instance, Object[] methodParameters)\r\n at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ExecuteAsync(HttpControllerContext controllerContext, IDictionary`2 arguments, CancellationToken cancellationToken)\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Web.Http.Controllers.ApiControllerActionInvoker.<InvokeActionAsyncCore>d__0.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Web.Http.Controllers.ActionFilterResult.<ExecuteAsync>d__2.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__1.MoveNext()"
}
}
}
Hi Steven,
Need to see the full response from fiddler to understand what could be the reason. We may also want to see the server CRM platform traces in case of On Premise or in case of Online we have to check our telemetry against the requested.
Regards,
Praveen T
D365 Support
Microsoft Corporation
Hi,
I discovered the reason there was no ID was because the email had to be saved before the ID was generated. My bad. However I am getting a 500 internal error when I used the provided snippet with the activity ID of the email. Any ideas??
Thank you for your time,
Steven.
Hi Steven,
Email itself is an activity hence am thinking Xrm.Page.data.entity.getId() should be the activityid that you want, Can you please verify if it isn’t the case.
Regards,
Praveen T
D365 CE Support Engineer
Microsoft Corporation
Hi,
Thank you for your timely reply. I am trying to do this but the activityid is not in the email form. I can see the activityid when I do a get request for all emails. But I am not sure how I can do a get request for my current email if the activityid is the primary key but it is not in the front end. Any ideas?
PS. Please let me know if you would like me to clarify anything I am trying to say.
Thanks,
Steven.
Hi Steven,
You should be able to achieve the same by using this bloggers sample. You should need to change activity type to email.
gist.github.com/.../61150022552056a4c3ed8b5fd52675a7
Regards,
Praveen T
D365 CE Support Engineer
Microsoft Corporation
Stay up to date on forum activity by subscribing. You can also customize your in-app and email Notification settings across all subscriptions.
André Arnaud de Cal... 291,253 Super User 2024 Season 2
Martin Dráb 230,188 Most Valuable Professional
nmaenpaa 101,156