Skip to main content

Retrieve Entity and EntityCollection From CRM 2011 Plugin C#

For EntityCollection 
 private EntityCollection RetrieveEntityByAttribute(ref IOrganizationService service, string _Entity, string[] _ColumnSet, string[] _AttributeRange, object[] _AttributeValue)
        {
            EntityCollection mcs_QueueItemComplaint = null;
            ColumnSet Indexcol = new ColumnSet(_ColumnSet);
            QueryByAttribute indexattribute = new QueryByAttribute();
            indexattribute.EntityName = _Entity;
            indexattribute.Attributes.AddRange(_AttributeRange);
            indexattribute.Values.AddRange(_AttributeValue);
            indexattribute.ColumnSet = Indexcol;
            RetrieveMultipleRequest req_index = new RetrieveMultipleRequest();
            req_index.Query = indexattribute;
            RetrieveMultipleResponse resp_index = (RetrieveMultipleResponse)service.Execute(req_index);
            if (resp_index != null)
            {
                mcs_QueueItemComplaint = resp_index.EntityCollection;
            }
            return mcs_QueueItemComplaint;
        }
For Entity

private Entity RetrieveEntityByAttribute(ref IOrganizationService service, string _Entity, string[] _ColumnSet, string[] _AttributeRange, object[] _AttributeValue)

        {

            Entity _mcsindexdynamics = null;
            Guid userid = Guid.Empty;
            ColumnSet Indexcol = new ColumnSet(_ColumnSet);
            QueryByAttribute indexattribute = new QueryByAttribute();
            indexattribute.EntityName = _Entity;
            indexattribute.Attributes.AddRange(_AttributeRange);
            indexattribute.Values.AddRange(_AttributeValue);
            indexattribute.ColumnSet = Indexcol;
            RetrieveMultipleRequest req_index = new RetrieveMultipleRequest();
            req_index.Query = indexattribute;
            RetrieveMultipleResponse resp_index = (RetrieveMultipleResponse)service.Execute(req_index);
            EntityCollection mcs_index = resp_index.EntityCollection;
            if (mcs_index.Entities.Count > 0)
            {
                _mcsindexdynamics = (Entity)mcs_index.Entities[0];
            }
            return _mcsindexdynamics;
        }

Comments