locked
Help Help error message Web Service RRS feed

  • Question

  • I got an error after executing the third line.

    Data type:

    statecode: Status, new_lead : bit, new_approval: bit

     

    query.Attributes =

    new string[] { "statecode", "new_lead", "new_approval" };

     

    query.Values =

    new string[] { "0","true","true" };

    CRMSDK.

    BusinessEntityCollection AccountF = CrmService.RetrieveMultiple(query);

    Error Message:

     "Possible SOAP version mismatch: Envelope namespace http://schemas.xmlsoap.org/ws
    dl/ was unexpected. Expecting http://schemas.xmlsoap.org/soap/envelope/"

     

     
    hussain
    Tuesday, November 15, 2011 7:49 PM

Answers

  • Please see some sample reusable code.

    	List<BusinessEntity> accounts = null;
    	DynamicEntity deAccount = null;
            Guid accountCrmId = Guid.Empty;
    
            QueryByAttribute query = CreateQuery(EntityName.account.ToString(), "accountnumber", accountId, new string[] { "accountid" });
            accounts = RetrieveBusinessEntities(service, query);
     	if (accounts != null && accounts.Count() > 0)
                    {
                        deAccount = accounts[0] as DynamicEntity;
                        accountCrmId = GetKeyValue(deAccount, "accountid");
                    }
    
    
    	//Another eg with 2 parm, both are GUid
    	//CreateQuery("new__erpdocumentapplication", new string[] { "new__invoiceid", "new__creditid", }, new string[] { invoiceDocumentId.ToString(), creditDocumentId.ToString() }, new string[] { "new__erpdocumentapplicationid" });
    
    
    
    	public static List<BusinessEntity> RetrieveBusinessEntities(RetrieveMultipleResponse response)
            {
                return response.BusinessEntityCollection.BusinessEntities;
            }
    
            public static List<DynamicEntity> RetrieveDynamicEntities(RetrieveMultipleResponse response)
            {
                List<DynamicEntity> dynamicEntities = new List<DynamicEntity>();
                foreach (DynamicEntity entity in response.BusinessEntityCollection.BusinessEntities)
                {
                    dynamicEntities.Add(entity);
                }
                return dynamicEntities;
            }
    
    
    	public static List<BusinessEntity> RetrieveBusinessEntities(object service, QueryByAttribute query)
            {
                RetrieveMultipleRequest request = CreateRetrieveMultipleRequest(query);
                RetrieveMultipleResponse response = null;
                try
                {
                    if (service.GetType() == typeof(CrmService))
                    {
                        CrmService crmService = (CrmService)service;
                        response = ExecuteRetrieveMultipleResponse(crmService, request);
                    }
                    else
                    {
                        ICrmService iCrmService = (ICrmService)service;
                        response = ExecuteRetrieveMultipleResponse(iCrmService, request);
                    }
                }
                catch (SoapException soEx)
                {
                    //
    
                }
                catch (Exception ex)
                {
                    //
                }
                return RetrieveBusinessEntities(response);
            }
    
     	public static RetrieveMultipleRequest CreateRetrieveMultipleRequest(QueryByAttribute query)
            {
                if (query == null)
                    throw new Exception("QueryByAttribute (query) is null, CreateRetrieveMultipleRequest failed to create.");
    
                RetrieveMultipleRequest request = new RetrieveMultipleRequest();
                request.Query = query;
                request.ReturnDynamicEntities = true;
                return request;
            }
    
    
     	public static RetrieveMultipleResponse ExecuteRetrieveMultipleResponse(ICrmService crmService, object request)
            {
                return (RetrieveMultipleResponse)crmService.Execute(request);
            }
    
            public static RetrieveMultipleResponse ExecuteRetrieveMultipleResponse(CrmService crmService, RetrieveMultipleRequest request)
            {
                return (RetrieveMultipleResponse)crmService.Execute(request);
            }
    
    
    	public static QueryByAttribute CreateQuery(string entityName, string attributeName, string attributeValue, params string[] columnNames)
            {
                QueryByAttribute query = new QueryByAttribute();
                query.EntityName = entityName;
                if (columnNames.Length == 0)
                    query.ColumnSet = new AllColumns();
                else
                    query.ColumnSet = new ColumnSet( columnNames );
                query.Attributes = new string[] { attributeName };
                query.Values = new object[] { attributeValue };
                return query;
            }
    
            public static QueryByAttribute CreateQuery(string entityName, string[] attributes, string[] attributeValues, params string[] columnNames)
            {
                QueryByAttribute query = new QueryByAttribute();
                query.EntityName = entityName;
                if (columnNames.Length == 0)
                    query.ColumnSet = new AllColumns();
                else
                    query.ColumnSet = new ColumnSet(columnNames);
                query.Attributes = attributes ;
                query.Values = attributeValues ;
                return query;
            }
    
    
    	public static Guid GetKeyValue(DynamicEntity entity, string attribName)
            {
                if (!PropertyExists(entity, attribName))
                    return Guid.Empty;
    
                Key prop = entity.Properties[attribName] as Key;
    
                if (prop == null)
                    return Guid.Empty;
    
                return prop.Value;
    
            }
    
    	
    	//Example of using QueryExpression
    	private List<DynamicEntity> GetWorkflowSchedules(CrmService crmService)
            {
                List<DynamicEntity> listDE = new List<DynamicEntity>();
    
                RetrieveMultipleRequest req = new RetrieveMultipleRequest();
                req.ReturnDynamicEntities = true;
    
                QueryExpression qe = new QueryExpression();
                qe.ColumnSet = new AllColumns();
                qe.Distinct = false;
                qe.EntityName = "new_workflowschedule";
                FilterExpression exp = new FilterExpression();
                exp.FilterOperator = LogicalOperator.And;
    
                ConditionExpression cond1 = new ConditionExpression();
                cond1.AttributeName = "new_datenextrun";
                object[] valueList1 = { DateTime.Now.ToString("s") };
                cond1.Values = valueList1;
                cond1.Operator = ConditionOperator.LessThan;
                exp.Conditions.Add(cond1);
    
                ConditionExpression cond2 = new ConditionExpression();
                cond2.AttributeName = "statuscode";
                object[] valueList2 = { 1 };
                cond2.Values = valueList2;
                cond2.Operator = ConditionOperator.Equal;
                exp.Conditions.Add(cond2);
    
                ConditionExpression cond3 = new ConditionExpression();
                cond3.AttributeName = "new_dateend";
                object[] valueList3 = { DateTime.Now.ToString("s") };
                cond3.Values = valueList3;
                cond3.Operator = ConditionOperator.GreaterThan;
                exp.Conditions.Add(cond3);
    
                qe.Criteria = exp;
    
                req.Query = qe;
    
                RetrieveMultipleResponse resp = crmService.Execute(req) as RetrieveMultipleResponse;
    
                if (resp.BusinessEntityCollection.BusinessEntities != null)
                {
                    foreach (BusinessEntity be in resp.BusinessEntityCollection.BusinessEntities)
                    {
                        listDE.Add((DynamicEntity)be);
                    }
                }
    
                return listDE;
            }
    



    Thomas T(MCBMSS) If you find this post helpful then please "Vote as Helpful" and "Mark As Answer".
    Wednesday, November 16, 2011 4:15 AM

All replies

  • In the code the values are passed as string instead of the correct type. The correct usage is explained in the following link:

    http://msdn.microsoft.com/en-us/library/gg334767.aspx

     


    Murali
    Tuesday, November 15, 2011 10:23 PM
  • Any example available??
    hussain
    Wednesday, November 16, 2011 3:42 AM
  • Please see some sample reusable code.

    	List<BusinessEntity> accounts = null;
    	DynamicEntity deAccount = null;
            Guid accountCrmId = Guid.Empty;
    
            QueryByAttribute query = CreateQuery(EntityName.account.ToString(), "accountnumber", accountId, new string[] { "accountid" });
            accounts = RetrieveBusinessEntities(service, query);
     	if (accounts != null && accounts.Count() > 0)
                    {
                        deAccount = accounts[0] as DynamicEntity;
                        accountCrmId = GetKeyValue(deAccount, "accountid");
                    }
    
    
    	//Another eg with 2 parm, both are GUid
    	//CreateQuery("new__erpdocumentapplication", new string[] { "new__invoiceid", "new__creditid", }, new string[] { invoiceDocumentId.ToString(), creditDocumentId.ToString() }, new string[] { "new__erpdocumentapplicationid" });
    
    
    
    	public static List<BusinessEntity> RetrieveBusinessEntities(RetrieveMultipleResponse response)
            {
                return response.BusinessEntityCollection.BusinessEntities;
            }
    
            public static List<DynamicEntity> RetrieveDynamicEntities(RetrieveMultipleResponse response)
            {
                List<DynamicEntity> dynamicEntities = new List<DynamicEntity>();
                foreach (DynamicEntity entity in response.BusinessEntityCollection.BusinessEntities)
                {
                    dynamicEntities.Add(entity);
                }
                return dynamicEntities;
            }
    
    
    	public static List<BusinessEntity> RetrieveBusinessEntities(object service, QueryByAttribute query)
            {
                RetrieveMultipleRequest request = CreateRetrieveMultipleRequest(query);
                RetrieveMultipleResponse response = null;
                try
                {
                    if (service.GetType() == typeof(CrmService))
                    {
                        CrmService crmService = (CrmService)service;
                        response = ExecuteRetrieveMultipleResponse(crmService, request);
                    }
                    else
                    {
                        ICrmService iCrmService = (ICrmService)service;
                        response = ExecuteRetrieveMultipleResponse(iCrmService, request);
                    }
                }
                catch (SoapException soEx)
                {
                    //
    
                }
                catch (Exception ex)
                {
                    //
                }
                return RetrieveBusinessEntities(response);
            }
    
     	public static RetrieveMultipleRequest CreateRetrieveMultipleRequest(QueryByAttribute query)
            {
                if (query == null)
                    throw new Exception("QueryByAttribute (query) is null, CreateRetrieveMultipleRequest failed to create.");
    
                RetrieveMultipleRequest request = new RetrieveMultipleRequest();
                request.Query = query;
                request.ReturnDynamicEntities = true;
                return request;
            }
    
    
     	public static RetrieveMultipleResponse ExecuteRetrieveMultipleResponse(ICrmService crmService, object request)
            {
                return (RetrieveMultipleResponse)crmService.Execute(request);
            }
    
            public static RetrieveMultipleResponse ExecuteRetrieveMultipleResponse(CrmService crmService, RetrieveMultipleRequest request)
            {
                return (RetrieveMultipleResponse)crmService.Execute(request);
            }
    
    
    	public static QueryByAttribute CreateQuery(string entityName, string attributeName, string attributeValue, params string[] columnNames)
            {
                QueryByAttribute query = new QueryByAttribute();
                query.EntityName = entityName;
                if (columnNames.Length == 0)
                    query.ColumnSet = new AllColumns();
                else
                    query.ColumnSet = new ColumnSet( columnNames );
                query.Attributes = new string[] { attributeName };
                query.Values = new object[] { attributeValue };
                return query;
            }
    
            public static QueryByAttribute CreateQuery(string entityName, string[] attributes, string[] attributeValues, params string[] columnNames)
            {
                QueryByAttribute query = new QueryByAttribute();
                query.EntityName = entityName;
                if (columnNames.Length == 0)
                    query.ColumnSet = new AllColumns();
                else
                    query.ColumnSet = new ColumnSet(columnNames);
                query.Attributes = attributes ;
                query.Values = attributeValues ;
                return query;
            }
    
    
    	public static Guid GetKeyValue(DynamicEntity entity, string attribName)
            {
                if (!PropertyExists(entity, attribName))
                    return Guid.Empty;
    
                Key prop = entity.Properties[attribName] as Key;
    
                if (prop == null)
                    return Guid.Empty;
    
                return prop.Value;
    
            }
    
    	
    	//Example of using QueryExpression
    	private List<DynamicEntity> GetWorkflowSchedules(CrmService crmService)
            {
                List<DynamicEntity> listDE = new List<DynamicEntity>();
    
                RetrieveMultipleRequest req = new RetrieveMultipleRequest();
                req.ReturnDynamicEntities = true;
    
                QueryExpression qe = new QueryExpression();
                qe.ColumnSet = new AllColumns();
                qe.Distinct = false;
                qe.EntityName = "new_workflowschedule";
                FilterExpression exp = new FilterExpression();
                exp.FilterOperator = LogicalOperator.And;
    
                ConditionExpression cond1 = new ConditionExpression();
                cond1.AttributeName = "new_datenextrun";
                object[] valueList1 = { DateTime.Now.ToString("s") };
                cond1.Values = valueList1;
                cond1.Operator = ConditionOperator.LessThan;
                exp.Conditions.Add(cond1);
    
                ConditionExpression cond2 = new ConditionExpression();
                cond2.AttributeName = "statuscode";
                object[] valueList2 = { 1 };
                cond2.Values = valueList2;
                cond2.Operator = ConditionOperator.Equal;
                exp.Conditions.Add(cond2);
    
                ConditionExpression cond3 = new ConditionExpression();
                cond3.AttributeName = "new_dateend";
                object[] valueList3 = { DateTime.Now.ToString("s") };
                cond3.Values = valueList3;
                cond3.Operator = ConditionOperator.GreaterThan;
                exp.Conditions.Add(cond3);
    
                qe.Criteria = exp;
    
                req.Query = qe;
    
                RetrieveMultipleResponse resp = crmService.Execute(req) as RetrieveMultipleResponse;
    
                if (resp.BusinessEntityCollection.BusinessEntities != null)
                {
                    foreach (BusinessEntity be in resp.BusinessEntityCollection.BusinessEntities)
                    {
                        listDE.Add((DynamicEntity)be);
                    }
                }
    
                return listDE;
            }
    



    Thomas T(MCBMSS) If you find this post helpful then please "Vote as Helpful" and "Mark As Answer".
    Wednesday, November 16, 2011 4:15 AM