I need to migrate all emails with attachments from one dynamics 365 site to other.
How can I achieve this using web API in c#?
Hi Priyank
I have done that in a migration project. I used the Organization Service, but you should be able to use the Web API as well.
I used the RetrieveMultiple on the Organization Service Proxy, to retrieve all the emails. And one by one I created the emails in the destination, using the same ActivityId as on the source system.
For each email I created, I checked if there were any attachments. And if there was, I copied them to the destination.
The following snippet is part of a migration solution I have created.
var query = new QueryExpression() { EntityName = "activitymimeattachment", ColumnSet = new ColumnSet(false) }; query.Criteria.AddCondition("objectid", ConditionOperator.Equal, SourceReference.Id); var attachments = Source.Proxy.RetrieveMultiple(query); if (attachments != null && attachments.Entities.Count > 0) { var columns = new ColumnSet( "activitymimeattachmentid", "attachmentnumber", "body", "filename", "mimetype", "objectid", "objecttypecode", "subject" ); foreach (var item in attachments.Entities) { var attachment = Destination.Retrieve(item.LogicalName, item.Id, new ColumnSet(false), false); if (attachment == null) { attachment = Source.Retrieve(item, columns); if (attachment != null) { var Attributes = attachment.Attributes.ToArray(); foreach (var attribute in Attributes) { if (attribute.Value.GetType() == typeof(EntityReference)) { var lookup = GetReferenceTarget((EntityReference)attribute.Value, attribute.Key); if (lookup != null) { attachment.Attributes[attribute.Key] = lookup; } } } Destination.CreateEntity(attachment); } } } }
Best regards
Christian Jensen
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