问题
How to obtain the list (or e.g. collection) of guids of all notes associated with current entity?
I know that it is in some way connected with service.Retreive(...), but can't make working code.
Attach 1:
public void Execute(IServiceProvider serviceProvider)
{
    Entity entity = null;
    var context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
    if (entity.Contains("new_parentopportunity"))
    {
        try
        {
            IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
            IOrganizationService service = factory.CreateOrganizationService(context.UserId);
            Guid projectGuid = entity.Id;
            Guid oppGuid = ((EntityReference)entity["new_parentopportunity"]).Id;
            for (int i = 0; i < 100; i++) //Instead of 100, I'll place obtained collection.Length
            {
                Entity opp = service.Retrieve("", oppGuid, new Microsoft.Xrm.Sdk.Query.ColumnSet(true)); //RETREIVE ENTITY NOTE HERE
                var temop = CreateNoteAttachment(opp);
                service.Create(temp);
            }
        }
        catch (Exception ex)
        {
            throw new InvalidPluginExecutionException("An error occurred in the plug-in.", ex);
        }
    }
}
private Entity CreateNoteAttachment(Entity oppNote)
{
    //some code here
}
Update 1:
I also write it in queries, can you look at this, whethere everything fine there? PasteBin
回答1:
Two ways to query for related notes:
Early Bound:
 var  notes =
                service.AnnotationSet.Where(annotation => annotation.Opportunity_Annotation.Id == entity.Id)
                    .Select(annotation => annotation.Id)
                    .ToList();
FetchXml:
  var fetchXml = string.Format("<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>" +
                                    "<entity name='annotation'>" +
                                    "<attribute name='annotationid' />" +
                                    "<filter type='and'>"+
                                    "<condition attribute='objectid' operator='eq' value='{0}'/>"+
                                    "</filter>"+
                                    "</entity>" +
                                    "</fetch>", entity.Id);
            var response = service.RetrieveMultiple(new FetchExpression(fetchXml));
            var notes = response.Entities;
    来源:https://stackoverflow.com/questions/38246846/get-the-list-of-notes