none
CRM提供服务是否能多个实体实现联合查询 RRS feed

  • 问题

  • 调用CRM的服务,是否能实现多个实体实现联合查询,两个实体关联起来查询呢?如果可以,请问具体怎么实现?谢谢!


    软件工程师 1/1 | MVP 0/1
    2010年4月29日 6:25

答案

  • Sdk:

    [C#]
    // Set up the CRM Service.
    CrmAuthenticationToken token = new CrmAuthenticationToken();
    token.AuthenticationType = 0;
    token.OrganizationName = "AdventureWorksCycle";
     
    CrmService service = new CrmService();
    service.Url = ""http://<servername>:<port>/mscrmservices/2007/crmservice.asmx";
    service.CrmAuthenticationTokenValue = token;
    service.Credentials = System.Net.CredentialCache.DefaultCredentials;

    // Create a column set holding the names of the columns to be retrieved.
    ColumnSet cols = new ColumnSet();
    cols.Attributes = new string [] {"name", "accountid"};

    // Create the ConditionExpression.
    ConditionExpression condition = new ConditionExpression();

    // Set the condition to be when the account owner's last name is not Cannon.
    condition.AttributeName = "lastname";
    condition.Operator = ConditionOperator.Equal;
    condition.Values = new string [] {"Cannon"};

    // Build the filter that is based on the condition.
    FilterExpression filter = new FilterExpression();
    filter.FilterOperator = LogicalOperator.And;
    filter.Conditions = new ConditionExpression[] {condition};

    // Create a LinkEntity to link the owner's information to the account.
    LinkEntity link = new LinkEntity();

    // Set the LinkEntity properties.
    link.LinkCriteria = filter;

    // Set the linking entity to account.
    link.LinkFromEntityName = EntityName.account.ToString();

    // Set the linking attribute to owninguser.
    link.LinkFromAttributeName = "owninguser";

    // The attribute being linked to is systemuserid.
    link.LinkToAttributeName = "systemuserid";

    // The entity being linked to is systemuser.
    link.LinkToEntityName = EntityName.systemuser.ToString();

    // Create an instance of the query expression class.
    QueryExpression query = new QueryExpression();

    // Set the query properties.
    query.EntityName = EntityName.account.ToString();
    query.ColumnSet = cols;
    query.LinkEntities = new LinkEntity[] {link};

    // Create the request.
    RetrieveMultipleRequest retrieve = new RetrieveMultipleRequest();

    // Set the request properties.
    retrieve.Query = query;

    // Execute the request.
    RetrieveMultipleResponse retrieved2 = (RetrieveMultipleResponse) service.Execute(retrieve);

    2010年4月29日 22:54

全部回复

  • 可以有个方法 RetrieveMultiple() 就可以实现边表查询 ,QueryExpression 类可中有个属性是LinkEntities可以对多个实体时行查询,这个在sdk上都有例子
    2010年4月29日 15:09
  • Sdk:

    [C#]
    // Set up the CRM Service.
    CrmAuthenticationToken token = new CrmAuthenticationToken();
    token.AuthenticationType = 0;
    token.OrganizationName = "AdventureWorksCycle";
     
    CrmService service = new CrmService();
    service.Url = ""http://<servername>:<port>/mscrmservices/2007/crmservice.asmx";
    service.CrmAuthenticationTokenValue = token;
    service.Credentials = System.Net.CredentialCache.DefaultCredentials;

    // Create a column set holding the names of the columns to be retrieved.
    ColumnSet cols = new ColumnSet();
    cols.Attributes = new string [] {"name", "accountid"};

    // Create the ConditionExpression.
    ConditionExpression condition = new ConditionExpression();

    // Set the condition to be when the account owner's last name is not Cannon.
    condition.AttributeName = "lastname";
    condition.Operator = ConditionOperator.Equal;
    condition.Values = new string [] {"Cannon"};

    // Build the filter that is based on the condition.
    FilterExpression filter = new FilterExpression();
    filter.FilterOperator = LogicalOperator.And;
    filter.Conditions = new ConditionExpression[] {condition};

    // Create a LinkEntity to link the owner's information to the account.
    LinkEntity link = new LinkEntity();

    // Set the LinkEntity properties.
    link.LinkCriteria = filter;

    // Set the linking entity to account.
    link.LinkFromEntityName = EntityName.account.ToString();

    // Set the linking attribute to owninguser.
    link.LinkFromAttributeName = "owninguser";

    // The attribute being linked to is systemuserid.
    link.LinkToAttributeName = "systemuserid";

    // The entity being linked to is systemuser.
    link.LinkToEntityName = EntityName.systemuser.ToString();

    // Create an instance of the query expression class.
    QueryExpression query = new QueryExpression();

    // Set the query properties.
    query.EntityName = EntityName.account.ToString();
    query.ColumnSet = cols;
    query.LinkEntities = new LinkEntity[] {link};

    // Create the request.
    RetrieveMultipleRequest retrieve = new RetrieveMultipleRequest();

    // Set the request properties.
    retrieve.Query = query;

    // Execute the request.
    RetrieveMultipleResponse retrieved2 = (RetrieveMultipleResponse) service.Execute(retrieve);

    2010年4月29日 22:54
  • 非常感谢大家,谢谢!
    软件工程师 1/1 | MVP 0/1
    2010年4月30日 4:07