locked
Retrieve attribute values by specifying attribute names RRS feed

  • Question

  • Hi, Friends

                            I want to retreive accountnumber's and corresponding name's of all account entiities in CRM..Which is the best way to do this?..pls help me

    Tuesday, July 27, 2010 5:49 AM

Answers

  • But I think you are already using dynamic entity, you should not have any problem to accessing attributes you can do it just like you while creating account entity records.
    Mahain
    • Marked as answer by Jim Glass Jr Tuesday, July 27, 2010 1:48 PM
    Tuesday, July 27, 2010 7:23 AM
    Moderator
  • IF you will retrieve your account information using dynamic entity that you can use the same way as, like below code

    From SDK, Have you download SDK ???

    // Create the retrieve target.
                TargetRetrieveDynamic targetRetrieve = new TargetRetrieveDynamic();

                // Set the properties of the target.
                targetRetrieve.EntityName = EntityName.contact.ToString();
                targetRetrieve.EntityId = created.id;

                // Create the request object.
                RetrieveRequest retrieve = new RetrieveRequest();

                // Set the properties of the request object.
                retrieve.Target = targetRetrieve;
                // Be aware that using AllColumns may adversely affect
                // performance and cause unwanted cascading in subsequent
                // updates. A best practice is to retrieve the least amount of
                // data required.
                retrieve.ColumnSet = new AllColumns();

                // Indicate that the BusinessEntity should be retrieved as a DynamicEntity.
                retrieve.ReturnDynamicEntities = true;

                // Execute the request.
                RetrieveResponse retrieved = (RetrieveResponse) service.Execute(retrieve);

                // Extract the DynamicEntity from the request.
                DynamicEntity entity = (DynamicEntity)retrieved.BusinessEntity;

                // Extract the fullname from the dynamic entity
                string fullname;

                for (int i = 0; i < entity.Properties.Length; i++)
                {
                   if (entity.Properties[i].Name.ToLower() == "fullname")
                   {
                      StringProperty property = (StringProperty) entity.Properties[i];
                      fullname = property.Value;
                      break;
                   }
                }


    Mahain
    • Marked as answer by Jim Glass Jr Tuesday, July 27, 2010 1:48 PM
    Tuesday, July 27, 2010 8:50 AM
    Moderator
  • Hey,

    yes, you can use crmwebservice object.


    Mahain
    • Marked as answer by Jim Glass Jr Tuesday, July 27, 2010 1:48 PM
    Tuesday, July 27, 2010 9:30 AM
    Moderator
  • could you paste your code here, And it's just example, you have to use like this, not exactly this code
    Mahain
    • Marked as answer by Jim Glass Jr Tuesday, July 27, 2010 1:48 PM
    Tuesday, July 27, 2010 9:54 AM
    Moderator

All replies

  • Hi,

    you can use retrievemultiple function for that

    refer

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

    Edit : Updated


    Mahain
    Tuesday, July 27, 2010 5:54 AM
    Moderator
  • Thank you ,

                   But, I can't specify 'condition.Values ', bcoz, I want all values..Also how can I store the retrieved values?

    Tuesday, July 27, 2010 6:03 AM
  • After retrieving

     

    BusinessEntityCollection accounts = service.RetrieveMultiple(query);

                        How can I read the business entity collection?..I want to store this values to a datatable..pls help me

    Tuesday, July 27, 2010 6:11 AM
  • Thank u very much,

                            But the solution fits only if account attribute names are known in advance..In my case, I don't know the number and names of attributes in advance..The problem is,i can't write account.accountnumber..In the place of accountnumber, I have a string( an attribute name is assigned to it at runtime)..pls help me to solve the problem in dynamic cases

    Tuesday, July 27, 2010 6:40 AM
  • But I think you are already using dynamic entity, you should not have any problem to accessing attributes you can do it just like you while creating account entity records.
    Mahain
    • Marked as answer by Jim Glass Jr Tuesday, July 27, 2010 1:48 PM
    Tuesday, July 27, 2010 7:23 AM
    Moderator
  • Thank you

    yes Mahain,

                       I already used dynamic entity..But in that case, I am able to specify properties for dynamic entity..As u pointed out, i am now facing the same problem I had faced earlier..But, here I am unable to specify properties for the account entity

    I will make it clear

    In the case of dynamic entity creation,I wrote

                      DynamicEntity n1 = new DynamicEntity();
                      n1.Name = "account";
                      StringProperty sp = new StringProperty();
                      sp.Name = "accountnumber";
                      sp.Value = "466";
                        StringProperty sp1 = new StringProperty();
                      sp1.Name = "name";
                      sp1.Value = "hqll";
                      n1.Properties = new Property[] { sp,sp1 };

    But in this case,

                BusinessEntityCollection accounts = service.RetrieveMultiple(query);

                             
                DataTable dt = new DataTable();

                string str1 = "name";
                string str2 = "accountnumber";

                dt.Columns.Add(""+str1+"");
                dt.Columns.Add("" + str2 + "");
              
                Foreach (BusinessEntity Account in accounts.BusinessEntities)
                {
                    account a = (account)Account;

                    DataRow dr = dt.NewRow();
                       
                    dr[""+str1+""] = ?
                    dr["" + str2 + ""] =?
                    dt.Rows.Add(dr);
               
                }

                  I can't write a.name or sumthing lik that...I want an alternative to the intellisence( instead of specifying a.) so that I can pass a value inside the string

                                 help me pls

    Tuesday, July 27, 2010 7:47 AM
  • Earlier, I had used 'fetchXml' to retrieve records..But I have some problems with that..The correspondence between attributes are not maintained..i.e, if the 2nd record has no account number, the acc.number of the 3rd record is inserted with the name of the 2nd record(in a row of datatable)
    Tuesday, July 27, 2010 7:56 AM
  • IF you will retrieve your account information using dynamic entity that you can use the same way as, like below code

    From SDK, Have you download SDK ???

    // Create the retrieve target.
                TargetRetrieveDynamic targetRetrieve = new TargetRetrieveDynamic();

                // Set the properties of the target.
                targetRetrieve.EntityName = EntityName.contact.ToString();
                targetRetrieve.EntityId = created.id;

                // Create the request object.
                RetrieveRequest retrieve = new RetrieveRequest();

                // Set the properties of the request object.
                retrieve.Target = targetRetrieve;
                // Be aware that using AllColumns may adversely affect
                // performance and cause unwanted cascading in subsequent
                // updates. A best practice is to retrieve the least amount of
                // data required.
                retrieve.ColumnSet = new AllColumns();

                // Indicate that the BusinessEntity should be retrieved as a DynamicEntity.
                retrieve.ReturnDynamicEntities = true;

                // Execute the request.
                RetrieveResponse retrieved = (RetrieveResponse) service.Execute(retrieve);

                // Extract the DynamicEntity from the request.
                DynamicEntity entity = (DynamicEntity)retrieved.BusinessEntity;

                // Extract the fullname from the dynamic entity
                string fullname;

                for (int i = 0; i < entity.Properties.Length; i++)
                {
                   if (entity.Properties[i].Name.ToLower() == "fullname")
                   {
                      StringProperty property = (StringProperty) entity.Properties[i];
                      fullname = property.Value;
                      break;
                   }
                }


    Mahain
    • Marked as answer by Jim Glass Jr Tuesday, July 27, 2010 1:48 PM
    Tuesday, July 27, 2010 8:50 AM
    Moderator
  • Thank u mahain,

                               But I am using crm web services..and SDK is not installed(  not prefer to use SDK)..Is this one useful when using webservices?..

    Tuesday, July 27, 2010 9:27 AM
  • Hey,

    yes, you can use crmwebservice object.


    Mahain
    • Marked as answer by Jim Glass Jr Tuesday, July 27, 2010 1:48 PM
    Tuesday, July 27, 2010 9:30 AM
    Moderator
  • Thank u mahain,

                        but, 'created' is not recognized

    Tuesday, July 27, 2010 9:36 AM
  • Sorry did not get you ???
    Mahain
    Tuesday, July 27, 2010 9:41 AM
    Moderator
  • Sorry mahain, I will make it clear,

       targetRetrieve.EntityId = created.id;

                                   In this line, 'created' is not recognized in my page..so i'm unable to specify Entityid..Also, i don't understand what u men by 'fullname'..

    Tuesday, July 27, 2010 9:47 AM
  • could you paste your code here, And it's just example, you have to use like this, not exactly this code
    Mahain
    • Marked as answer by Jim Glass Jr Tuesday, July 27, 2010 1:48 PM
    Tuesday, July 27, 2010 9:54 AM
    Moderator
  •  Hi, Mahain This is the code

    TargetRetrieveDynamic targetRetrieve = new TargetRetrieveDynamic();
    targetRetrieve.EntityName = "account";

     targetRetrieve.EntityId = created.id;


     RetrieveRequest retrieve = new RetrieveRequest(); retrieve.Target = targetRetrieve;
     retrieve.ColumnSet = new AllColumns();

     retrieve.ReturnDynamicEntities = true;
    RetrieveResponse retrieved = (RetrieveResponse)service.Execute(retrieve);
     DynamicEntity entity = (DynamicEntity)retrieved.BusinessEntity;
        
    DataTable dt=new DataTable();
              
     string head;
     string tat = "accountnumber,name";
     foreach (string str in tat.Split(','))
     {
       for (int i = 0; i < entity.Properties.Length; i++)
       {
          for (int x = 0; x < dt.Columns.Count; x++)
         {
            DataRow dr = dt.NewRow();
           if (entity.Properties[i].Name.ToLower() == "" + str + "")
             {

                  head = dt.Columns[x].ToString();
                
                  StringProperty property = (StringProperty)entity.Properties[i];
                   dr["" + head + ""] = property.Value;
                }
           }
                    }
                }

                                Here in the line(BOLD),'created' is not recognized in my page..In my code the string 'tat' may have any number of values at runtime..I want to retrieve the attributes( specified by the value of string 'tat') of all accounts..If the one account has only account name and having no number,the account number column of datatable should be empty and name column should display name.

                         

    Tuesday, July 27, 2010 10:45 AM
  • In the datatable , I want like this..Suppose there are 5 accounts..Here there is no account number for the second account and there is no name for the fourth one

    accountnumber

              name

       123

    Aaron Fitz

      

    Adam Park

    124

    Baker’s

     125

     

    126

    Advanced co.

     

                         When using xml fetch, i got

    accountnumber

              name

       123

    Aaron Fitz

       124

    Adam Park

    125

    Baker’s

    126

    Advanced co.

     

                  which is certainly dissappointing

    Tuesday, July 27, 2010 10:52 AM