Get the list of notes

最后都变了- 提交于 2019-12-25 07:19:34

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!