locked
Update an Annotation - How to get the AnnotationId ? RRS feed

  • Question

  • Hello !

    I have a custom entity in CRM 2011 Online and there are Annotations attached to the custom entity.

    My task is to update some of these Annotations using the SOAP endpoint.

    So my idea was to make a QueryExpression and search for the notetext inside the existing Annotations.

    After I have found the Annotation inside CRM 2011 Online that match the condition I should be able to get the AnnotationId.

    Sadly that don't work and I don't know why, can anybody help me please !

    private static Guid GetAnnotationGuid(OrganizationServiceProxy organizationProxy, Entity targetEntity, string suspectString)
    {
                Guid RecordId = Guid.Empty;
    
                string AttributeName = "notetext";
                string ConditionAttributeEquallTo = suspectString;
    
                try
                {
                    QueryExpression query = new QueryExpression("annotation");
                    ConditionExpression condition = new ConditionExpression(AttributeName, ConditionOperator.Equal, ConditionAttributeEquallTo);
                    FilterExpression filter = new FilterExpression();
                    filter.Conditions.Add(condition);
                    query.Criteria.AddFilter(filter);
                    EntityCollection SearchContactResult = organizationProxy.RetrieveMultiple(query);
                    if (SearchContactResult.Entities.Count == 1)
                    {
                        foreach (Entity result in SearchContactResult.Entities)
                        {
                            RecordId = result.Id;
                            Console.WriteLine("GetAnnotationID : " + RecordId);
                        }
                    }
                    else
                        Console.WriteLine("nothing found");
                }
                catch
                {
                    throw;
                }
                return RecordId;
    }

    I already checked out the MSDN but I couldn't find anything useful.

    There is an article called "Sample: Upload, Retrieve, and Download an Attachment" about creating an annotation but that didn't help me in any way cause the Annotations are created long time ago and I don't have the possibility to use the annotationId like they do in the example.
    Tuesday, November 26, 2013 6:29 PM

All replies

  • Hello,

    Your code looks fine except several things:

    1. You haven't defined ColumnSet for your query.

    2. In case you will get 2 or more records as a results - you will get nothing found message.

    I would suggest to use following code instead:

            private static Guid GetAnnotationGuid(OrganizationServiceProxy organizationProxy, Entity targetEntity, string suspectString)
            {
                Guid RecordId = Guid.Empty;
    
                string AttributeName = "notetext";
                string ConditionAttributeEquallTo = suspectString;
    
                try
                {
                    QueryExpression query = new QueryExpression("annotation")
                    {
                        ColumnSet = new ColumnSet(false)
                    };
                    query.Criteria.AddCondition(AttributeName, ConditionOperator.Equal, ConditionAttributeEquallTo);
    
                    Entity annotation = organizationProxy.RetrieveMultiple(query).Entities.FirstOrDefault();
                    
                    if (annotation != null)
                    {
                        RecordId = annotation.Id;
                        Console.WriteLine("GetAnnotationID : " + RecordId);
                    }
                    else
                        Console.WriteLine("nothing found");
                }
                catch
                {
                    throw;
                }
                return RecordId;
            }
    


    Dynamics CRM MVP/ Technical Evangelist at SlickData LLC
    My blog

    Tuesday, November 26, 2013 6:42 PM
    Moderator
  • Hello again !

    thank you for the fast reply after I tried your example and it also didn't work here. I created a new Annotation inside an existing custom entity via the webinterface with the simple notetext "testing" and searched for it. To my suprise it works so I checked out how I create the Annotations and traverse through the attributes list from Annotations in the MSDN. Result I forgot to add the attribute "isdocument" = false  *facepalm*

    Anyways, thank you very much for helping me solving the problem !

    btw:

    Your code looks fine except several things:

    I had to laugh ;-)


    • Edited by Eric Simpson Tuesday, November 26, 2013 7:34 PM - word
    Tuesday, November 26, 2013 7:33 PM