Search code examples
c#linqdynamics-crmcrm

How can I get an entity with linq-query that is not in context?


my plugin triggers on "Create" -xxx-Entity. in ServiceContextI have notes that belong only to this entity. But for example, I want all the notes in CRM or all the records belonging to another entity that are not in ServiceContext. How can I retrieve it?

var ServiceContext = new OrganizationServiceContext(service);

 var notes = from n in ServiceContext.CreateQuery("annotation")
 where n["objectid"] == new EntityReference("xxx", xxx.Id)
                            select n;

Solution

  • The OrganizationServiceContext can query any entity without limits. You can use the same query, just remove the where clause and you'll get all notes:

    var query = from n in ServiceContext.CreateQuery("annotation")
                select n;
    var allNotes = query.ToList();
    

    Or, for notes related to another record:

    var query = from n in ServiceContext.CreateQuery("annotation")
    where n.GetAttributeValue<EntityReference>("objectid").Id.Equals(myObjectId)
    select n;
    

    For notes that have attachments, unless you need the documentbody, leaving that out of the query can speed things up.