Skip to main content

Notifications

Dynamics 365 general forum

How to migrate all emails with attachments from one Dynamics 365 site to other by using web API in c#?

Posted on by 505

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#?

  • C. Jensen Profile Picture
    C. Jensen 384 on at
    RE: How to migrate all emails with attachments from one Dynamics 365 site to other by 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

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

November Spotlight Star - Khushbu Rajvi

Congratulations to a top community star!

Forum Structure Changes Coming on 11/8!

In our never-ending quest to help the Dynamics 365 Community members get answers faster …

Dynamics 365 Community Platform update – Oct 28

Welcome to the next edition of the Community Platform Update. This is a status …

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,253 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,188 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans