Moving Notes/Attachments in Dynamics CRM 4.0

Here is a simple snippet to copy Notes and/or Attachments – known as annotations in the system from one entity to another.  For example to copy the notes on a Contact to an Account.

private static void moveCrmNotes(Guid fromID, 
string fromEntityType, Guid toId, string toEntityType,
string toEntityName, CrmService.CrmService _svc) { #region notes query expression ConditionExpression notesExpression = new ConditionExpression(); notesExpression.AttributeName = "objectid"; notesExpression.Operator = ConditionOperator.Equal; notesExpression.Values = new object[] { fromID }; QueryExpression notesQuery = new QueryExpression(); notesQuery.ColumnSet = new AllColumns(); notesQuery.Criteria = new FilterExpression(); notesQuery.Criteria.Conditions = new ConditionExpression[]
              { notesExpression }; notesQuery.EntityName = fromEntityType; #endregion BusinessEntityCollection notesBec = null; annotation tempNote = null; notesBec = (BusinessEntityCollection)_svc.RetrieveMultiple(notesQuery); if (notesBec.BusinessEntities.Length > 0) { foreach (annotation currentNote in notesBec.BusinessEntities) { tempNote = new annotation(); tempNote.documentbody = currentNote.documentbody; tempNote.filename = currentNote.filename; tempNote.filesize = currentNote.filesize; tempNote.isdocument = currentNote.isdocument; tempNote.langid = currentNote.langid; tempNote.mimetype = currentNote.mimetype; tempNote.notetext = currentNote.notetext; tempNote.subject = currentNote.subject; tempNote.objectid = new Lookup(); = toEntityName; tempNote.objectid.type = toEntityType; tempNote.objectid.Value = toId; tempNote.objecttypecode = new EntityNameReference(); tempNote.objecttypecode.Value = toEntityType; _svc.Create(tempNote); } } }

Basic usage would be:

    moveCrmNotes(SourceGuid, "contact", TargetGuid, "account", 
                 "My Test Account", _svc); } finally { _svc.Dispose(); }

Author: Stuart

Crazy Person

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s