locked
Urgent Please : Retriving Entity including RelatedEntity using OrganizationService and without using SDK RRS feed

  • Question

  • At Present I am getting  orders with following code, Now I want to get even related entities like products with salesorder. Can any one please suggest me code block for retrieving related entity with salesorder.

    I dont want to use any sdk dlls here. only Organization service reference.

    OrganizationService.ColumnSet cset = new ColumnSet(); cset.Columns = new string[] { "new_orderid", "salesorderid", "pricelevelid", "transactioncurrencyid", "customerid" };

    QueryExpression salesorderQuery = new QueryExpression { EntityName = "salesorder", ColumnSet = cset, Criteria = new FilterExpression { }, // LinkEntities= new LinkEntity[]{relatedEntity}; }; Entity[] salesorder = client.RetrieveMultiple(salesorderQuery).Entities;



    Thanks Regards Prashanth Kamasamudram Even the least work done for others awakens the power within; even thinking the least good of others gradually instills into the heart the strength of a lion.



    Thursday, December 19, 2013 12:19 PM

Answers

  • Finally Got it :)

    Only reason was , there  are no columns assigned even though I have set All Columns to true. Following code is just retrieving account with related contacts, and I am serializing retrieved entity with related entities and saving it in my local disk. 

    Hope some one will find this helpful in case they are facing same problem

    	private void GetRelatedContacts(OrganizationService.OrganizationServiceClient client)
            {
                using (client)
                {
                    //create the query expression object
                    QueryExpression query = new QueryExpression();
                    //Query on reated entity records 
                    query.EntityName = "contact";
                    OrganizationService.ColumnSet cset = new ColumnSet();
                    cset.AllColumns = true;
                    cset.Columns = new string[] { "col1" };
                  
                    //Retrieve the all attributes of the related record
                    query.ColumnSet = cset;
    
                    //create the relationship object
                    Relationship relationship = new Relationship();
    
                    //add the condition where you can retrieve only the account related active contacts 
                    query.Criteria = new FilterExpression();
                    ConditionExpression cExpression = new ConditionExpression();
                    cExpression.AttributeName = "statecode";
                    cExpression.Operator = ConditionOperator.Equal;
                    cExpression.Values = new object[] { "Active" };
                    query.Criteria.Conditions = new ConditionExpression[] { cExpression };
    
                    // name of relationship between account & contact
                    relationship.SchemaName = "account_primary_contact";
    
                    //create relationshipQueryCollection Object
                    RelationshipQueryCollection relatedEntity = new RelationshipQueryCollection();
    
                    //Add the your relation and query to the RelationshipQueryCollection
                    relatedEntity.Add(new KeyValuePair<Relationship,QueryBase>(relationship,query)); //  .Add(relationship, query);
    
                    //create the retrieve request object
                    OrganizationRequest request=new OrganizationRequest();
                    EntityReference ettyRef = new EntityReference();
                    ettyRef.Id=new Guid("3941833F-3062-E311-877E-005056000007");
                    ettyRef.LogicalName="account";
                   // RetrieveRequest request = new RetrieveRequest();
                    OrganizationService.ColumnSet orgcset = new ColumnSet();
                    orgcset.AllColumns = true;
                    //add the relatedentities query
                    request.RequestName="Retrieve";//.RelatedEntitiesQuery = relatedEntity;
                    request.Parameters = new ParameterCollection();
                    request.Parameters.Add(new KeyValuePair<string, object>("Target", ettyRef));//'ColumnSet' 
                    request.Parameters.Add(new KeyValuePair<string, object>("ColumnSet", orgcset));
                    request.Parameters.Add(new KeyValuePair<string, object>("RelatedEntitiesQuery", relatedEntity));
    
                    //execute the request
                    OrganizationResponse response = (OrganizationResponse)client.Execute(request);
    
                    foreach (KeyValuePair<string, object> obj in response.Results)
                    {
    
                        if (obj.Value.GetType() == typeof(OrganizationService.Entity))
                        {
    
                           Entity ent = (Entity)obj.Value;
                           XmlDocument doc= SerializeEntityToXML(ent);
                           doc.Save(@"D:\" + ent.LogicalName + "_" + ent.Id + ".xml");
    
                        }
    
                    }
                }
    
    
            }



    Thanks Regards Prashanth Kamasamudram Even the least work done for others awakens the power within; even thinking the least good of others gradually instills into the heart the strength of a lion.

    Friday, December 20, 2013 2:08 PM

All replies

  • I Have tried to use following code but no luck. With this I am getting entity but Related Entities is still showing 0 

           
    		private void GetRelatedContacts(OrganizationService.OrganizationServiceClient client)
            {
                using (client)
                {
    
                    //create the query expression object
                    QueryExpression query = new QueryExpression();
    
                    //Query on related entity records 
                    query.EntityName = "contact";
                    OrganizationService.ColumnSet cset = new ColumnSet();
                    cset.AllColumns = true;
                    //Retrieve the all attributes of the related record
                    query.ColumnSet = cset;
    
                    //create the relationship object
                    Relationship relationship = new Relationship();
    
                    //add the condition where you can retrieve only the account related active contacts 
                    query.Criteria = new FilterExpression();
                    ConditionExpression cExpression = new ConditionExpression();
                    cExpression.AttributeName = "statecode";
                    cExpression.Operator = ConditionOperator.Equal;
                    cExpression.Values = new object[] { "Active" };
                    query.Criteria.Conditions = new ConditionExpression[] { cExpression };
    
                    // name of relationship between account & contact
                    relationship.SchemaName = "account_primary_contact";
    
                    //create relationshipQueryCollection Object
                    RelationshipQueryCollection relatedEntity = new RelationshipQueryCollection();
    
                    //Add the your relation and query to the RelationshipQueryCollection
                    relatedEntity.Add(new KeyValuePair<Relationship,QueryBase>(relationship,query));
    
                    //create the retrieve request object
                    OrganizationRequest request=new OrganizationRequest();
                    EntityReference ettyRef = new EntityReference();
                    ettyRef.Id=new Guid("3941833F-3062-E311-877E-005056000007");
                    ettyRef.LogicalName="account";
                   
                    OrganizationService.ColumnSet orgcset = new ColumnSet();
                    orgcset.AllColumns = true;
                    //add the relatedentities query
                    request.RequestName="Retrieve";
                    request.Parameters = new ParameterCollection();
                    request.Parameters.Add(new KeyValuePair<string, object>("Target", ettyRef));
                         request.Parameters.Add(new KeyValuePair<string, object>("ColumnSet", orgcset));
    
                    //execute the request
                    OrganizationResponse response = (OrganizationResponse)client.Execute(request);
    
                    RelatedEntityCollection = new RelatedEntityCollection();
    
                }
    
    
            }

    Any help please...its urgent.

    Friday, December 20, 2013 11:47 AM
  • It doesn't look like you're using the relatedEntity in your request. You need to include this line:

    request.Parameters.Add(new KeyValuePair<string, object>("RelatedEntitiesQuery", relatedEntity));
    


    Microsoft CRM MVP - http://mscrmuk.blogspot.com/ http://www.excitation.co.uk

    Friday, December 20, 2013 12:18 PM
    Moderator
  • Hi Thankyou for reply,

        Yes I have added that line after posting this code here. But now I am getting following exception.

          The formatter threw an exception while trying to deserialize the message: There was an error while trying to deserialize parameter http://schemas.microsoft.com/xrm/2011/Contracts/Services:request. The InnerException message was 'The use of type 'System.Collections.Generic.KeyValuePair`2[[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.Object, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]' as a get-only collection is not supported with NetDataContractSerializer.  Consider marking the type with the CollectionDataContractAttribute attribute or the SerializableAttribute attribute or adding a setter to the property.'.  Please see InnerException for more details.

    I am trying to use following

    OrganizationService.ColumnSet cset = new ColumnSet(); cset.AllColumns = true; //Retrieve the all attributes of the related record

    cset.Columns = new ObservableCollection<string> { allColumns.ToString() }; query.ColumnSet = cset;

     


    But cant fount ObservableCollection in system.collection.objectmodel

     


    Thanks Regards Prashanth Kamasamudram Even the least work done for others awakens the power within; even thinking the least good of others gradually instills into the heart the strength of a lion.


    Friday, December 20, 2013 12:21 PM
  • Finally Got it :)

    Only reason was , there  are no columns assigned even though I have set All Columns to true. Following code is just retrieving account with related contacts, and I am serializing retrieved entity with related entities and saving it in my local disk. 

    Hope some one will find this helpful in case they are facing same problem

    	private void GetRelatedContacts(OrganizationService.OrganizationServiceClient client)
            {
                using (client)
                {
                    //create the query expression object
                    QueryExpression query = new QueryExpression();
                    //Query on reated entity records 
                    query.EntityName = "contact";
                    OrganizationService.ColumnSet cset = new ColumnSet();
                    cset.AllColumns = true;
                    cset.Columns = new string[] { "col1" };
                  
                    //Retrieve the all attributes of the related record
                    query.ColumnSet = cset;
    
                    //create the relationship object
                    Relationship relationship = new Relationship();
    
                    //add the condition where you can retrieve only the account related active contacts 
                    query.Criteria = new FilterExpression();
                    ConditionExpression cExpression = new ConditionExpression();
                    cExpression.AttributeName = "statecode";
                    cExpression.Operator = ConditionOperator.Equal;
                    cExpression.Values = new object[] { "Active" };
                    query.Criteria.Conditions = new ConditionExpression[] { cExpression };
    
                    // name of relationship between account & contact
                    relationship.SchemaName = "account_primary_contact";
    
                    //create relationshipQueryCollection Object
                    RelationshipQueryCollection relatedEntity = new RelationshipQueryCollection();
    
                    //Add the your relation and query to the RelationshipQueryCollection
                    relatedEntity.Add(new KeyValuePair<Relationship,QueryBase>(relationship,query)); //  .Add(relationship, query);
    
                    //create the retrieve request object
                    OrganizationRequest request=new OrganizationRequest();
                    EntityReference ettyRef = new EntityReference();
                    ettyRef.Id=new Guid("3941833F-3062-E311-877E-005056000007");
                    ettyRef.LogicalName="account";
                   // RetrieveRequest request = new RetrieveRequest();
                    OrganizationService.ColumnSet orgcset = new ColumnSet();
                    orgcset.AllColumns = true;
                    //add the relatedentities query
                    request.RequestName="Retrieve";//.RelatedEntitiesQuery = relatedEntity;
                    request.Parameters = new ParameterCollection();
                    request.Parameters.Add(new KeyValuePair<string, object>("Target", ettyRef));//'ColumnSet' 
                    request.Parameters.Add(new KeyValuePair<string, object>("ColumnSet", orgcset));
                    request.Parameters.Add(new KeyValuePair<string, object>("RelatedEntitiesQuery", relatedEntity));
    
                    //execute the request
                    OrganizationResponse response = (OrganizationResponse)client.Execute(request);
    
                    foreach (KeyValuePair<string, object> obj in response.Results)
                    {
    
                        if (obj.Value.GetType() == typeof(OrganizationService.Entity))
                        {
    
                           Entity ent = (Entity)obj.Value;
                           XmlDocument doc= SerializeEntityToXML(ent);
                           doc.Save(@"D:\" + ent.LogicalName + "_" + ent.Id + ".xml");
    
                        }
    
                    }
                }
    
    
            }



    Thanks Regards Prashanth Kamasamudram Even the least work done for others awakens the power within; even thinking the least good of others gradually instills into the heart the strength of a lion.

    Friday, December 20, 2013 2:08 PM