locked
How to display all entity records in a tree? RRS feed

  • Question

  • Dear gurus,
    I am very new in CRM 4.0 development and I am asked to write a .aspx page in order to get a business unit (i.e. by its GUID via QueryString), then display all its 1->N related entity types in a tree. when a user expands a node , all records of that entity type must be added to tree.
    May you please let me know how can I basically do it and save my job? :)

    Thanks
    Tuesday, January 12, 2010 1:58 PM

Answers

  • Tuesday, January 12, 2010 2:04 PM
  • 1) To reterieve organization, use the "RetrieveOrganizationsRequest"  request class.
    Here is the code for this.

    // Provide valid Microsoft Dynamics CRM server information
    string hostname = "localhost";
    string port = "80";

    // Create the discovery service
    CrmDiscoveryService discoveryService = new CrmDiscoveryService();

    // Set the discovery service properties
    discoveryService.UseDefaultCredentials = true;
    discoveryService.Url = String.Format(
        "http://{0}:{1}/MSCRMServices/2007/{2}/CrmDiscoveryService.asmx",
        hostname, port, "AD");

    // Retrieve the organization name and endpoint Url from the
    // CrmDiscoveryService Web service.
    RetrieveOrganizationsRequest orgRequest = new RetrieveOrganizationsRequest();
    RetrieveOrganizationsResponse orgResponse =
        (RetrieveOrganizationsResponse)discoveryService.Execute(orgRequest);

    // Iterate through all the retrieved organizations
    foreach (OrganizationDetail orgDetail in orgResponse.OrganizationDetails)
    {
        Console.WriteLine(orgDetail.FriendlyName);
    }


    2) To reterieve all the entites use the RetrieveAllEntities class, here is the code from sdk

    CrmAuthenticationToken token = new CrmAuthenticationToken();
    token.OrganizationName = "AdventureWorksCycle";

    // You can use enums.cs from the SDK\Helpers folder to get the enumeration for Active Directory authentication.
    token.AuthenticationType = 0;

    // Create the metadata Web service;
    MetadataService metadataService = new MetadataService();
    metadataService.Url = "http://<servername>:<port>/MSCRMServices/2007/MetadataService.asmx";
    metadataService.CrmAuthenticationTokenValue = token;
    metadataService.Credentials = System.Net.CredentialCache.DefaultCredentials;
    metadataService.PreAuthenticate = true;


    // Create the request
    RetrieveAllEntitiesRequest allEntitiesRequest = new RetrieveAllEntitiesRequest();
    // Retrieve only the currently published changes, ignoring the changes that have
    // not been published.
    allEntitiesRequest.RetrieveAsIfPublished = false;
    allEntitiesRequest.MetadataItems = MetadataItems.EntitiesOnly;
            
    // Execute the request
    RetrieveAllEntitiesResponse allEntitiesResponse = (RetrieveAllEntitiesResponse)metadataService.Execute(allEntitiesRequest);
            
    // Iterate through the retrieved entities
    foreach (EntityMetadata entity in allEntitiesResponse.CrmMetadata)
    {
       // Process entity here
    }

    3) Finally to reterive all the records for a entity type use the RetrieveMultiple method, here is the code  from sdk.

    CrmAuthenticationToken token = new CrmAuthenticationToken();
    // You can use enums.cs from the SDK\Helpers folder to get the enumeration for Active Directory authentication.
    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 the ColumnSet that indicates the properties to be retrieved.
    ColumnSet cols = new ColumnSet();

    // Set the properties of the ColumnSet.
    cols.Attributes = new string [] {"fullname", "contactid"};

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

    // Set the condition for the retrieval to be when the contact's address' city is Sammamish.
    condition.AttributeName = "address1_city";
    condition.Operator = ConditionOperator.Like;
    condition.Values = new string [] {"Sammamish"};

    // Create the FilterExpression.
    FilterExpression filter = new FilterExpression();

    // Set the properties of the filter.
    filter.FilterOperator = LogicalOperator.And;
    filter.Conditions = new ConditionExpression[] {condition};

    // Create the QueryExpression object.
    QueryExpression query = new QueryExpression();

    // Set the properties of the QueryExpression object.
    query.EntityName = EntityName.contact.ToString();
    query.ColumnSet = cols;
    query.Criteria = filter;

    // Retrieve the contacts.
    BusinessEntityCollection contacts = service.RetrieveMultiple(query);

    i hope it helps...

    • Marked as answer by ArefK Wednesday, January 13, 2010 5:26 AM
    Tuesday, January 12, 2010 2:07 PM

All replies

  • Tuesday, January 12, 2010 2:04 PM
  • 1) To reterieve organization, use the "RetrieveOrganizationsRequest"  request class.
    Here is the code for this.

    // Provide valid Microsoft Dynamics CRM server information
    string hostname = "localhost";
    string port = "80";

    // Create the discovery service
    CrmDiscoveryService discoveryService = new CrmDiscoveryService();

    // Set the discovery service properties
    discoveryService.UseDefaultCredentials = true;
    discoveryService.Url = String.Format(
        "http://{0}:{1}/MSCRMServices/2007/{2}/CrmDiscoveryService.asmx",
        hostname, port, "AD");

    // Retrieve the organization name and endpoint Url from the
    // CrmDiscoveryService Web service.
    RetrieveOrganizationsRequest orgRequest = new RetrieveOrganizationsRequest();
    RetrieveOrganizationsResponse orgResponse =
        (RetrieveOrganizationsResponse)discoveryService.Execute(orgRequest);

    // Iterate through all the retrieved organizations
    foreach (OrganizationDetail orgDetail in orgResponse.OrganizationDetails)
    {
        Console.WriteLine(orgDetail.FriendlyName);
    }


    2) To reterieve all the entites use the RetrieveAllEntities class, here is the code from sdk

    CrmAuthenticationToken token = new CrmAuthenticationToken();
    token.OrganizationName = "AdventureWorksCycle";

    // You can use enums.cs from the SDK\Helpers folder to get the enumeration for Active Directory authentication.
    token.AuthenticationType = 0;

    // Create the metadata Web service;
    MetadataService metadataService = new MetadataService();
    metadataService.Url = "http://<servername>:<port>/MSCRMServices/2007/MetadataService.asmx";
    metadataService.CrmAuthenticationTokenValue = token;
    metadataService.Credentials = System.Net.CredentialCache.DefaultCredentials;
    metadataService.PreAuthenticate = true;


    // Create the request
    RetrieveAllEntitiesRequest allEntitiesRequest = new RetrieveAllEntitiesRequest();
    // Retrieve only the currently published changes, ignoring the changes that have
    // not been published.
    allEntitiesRequest.RetrieveAsIfPublished = false;
    allEntitiesRequest.MetadataItems = MetadataItems.EntitiesOnly;
            
    // Execute the request
    RetrieveAllEntitiesResponse allEntitiesResponse = (RetrieveAllEntitiesResponse)metadataService.Execute(allEntitiesRequest);
            
    // Iterate through the retrieved entities
    foreach (EntityMetadata entity in allEntitiesResponse.CrmMetadata)
    {
       // Process entity here
    }

    3) Finally to reterive all the records for a entity type use the RetrieveMultiple method, here is the code  from sdk.

    CrmAuthenticationToken token = new CrmAuthenticationToken();
    // You can use enums.cs from the SDK\Helpers folder to get the enumeration for Active Directory authentication.
    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 the ColumnSet that indicates the properties to be retrieved.
    ColumnSet cols = new ColumnSet();

    // Set the properties of the ColumnSet.
    cols.Attributes = new string [] {"fullname", "contactid"};

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

    // Set the condition for the retrieval to be when the contact's address' city is Sammamish.
    condition.AttributeName = "address1_city";
    condition.Operator = ConditionOperator.Like;
    condition.Values = new string [] {"Sammamish"};

    // Create the FilterExpression.
    FilterExpression filter = new FilterExpression();

    // Set the properties of the filter.
    filter.FilterOperator = LogicalOperator.And;
    filter.Conditions = new ConditionExpression[] {condition};

    // Create the QueryExpression object.
    QueryExpression query = new QueryExpression();

    // Set the properties of the QueryExpression object.
    query.EntityName = EntityName.contact.ToString();
    query.ColumnSet = cols;
    query.Criteria = filter;

    // Retrieve the contacts.
    BusinessEntityCollection contacts = service.RetrieveMultiple(query);

    i hope it helps...

    • Marked as answer by ArefK Wednesday, January 13, 2010 5:26 AM
    Tuesday, January 12, 2010 2:07 PM