Muhammad Ali Khan

MS CRM 3.0, MS CRM 4.0, Sharepoint 2007 & Sharepoint 2010

Reterive Mutliple Dynamic Entity Using Attribute Other than ID

Posted by Ali Khan on November 22, 2007

A lot of examples related to DynamicEntity uses either Create, Update or Retrieve (Single). What I was looking for was to retrieved multiple Dynamic Entity but searching on other attributes other than ID, but with no success

So I decided to write it myself J

Here is the sample code of my finding:-

            CrmService service = new CrmService();
            service.Credentials = System.Net.CredentialCache.DefaultCredentials;

            QueryByAttribute query = new QueryByAttribute();
            query.ColumnSet = new AllColumns();
            query.EntityName = “account”;  // please mention your entity name here

            query.Attributes = new string[] { “name” };   // your attribute here.
            query.Values = new string[] { “geopaq” };    // your attribute value here.

            RetrieveMultipleRequest retrieve = new RetrieveMultipleRequest();
            retrieve.ReturnDynamicEntities = true;
            retrieve.Query = query;

            RetrieveMultipleResponse response = (RetrieveMultipleResponse)service.Execute(retrieve);
            foreach (DynamicEntity entity in response.BusinessEntityCollection.BusinessEntities)
                Console.WriteLine(“Entity name=” + entity.Name);
                foreach (Property property in entity.Properties)
                    if (property is CrmBooleanProperty)
                        CrmBooleanProperty bProperty = property as CrmBooleanProperty;
                        Console.WriteLine(“Property Name=” + bProperty.Name + “value=” + bProperty.Value.Value);
                    else if (property is CrmDateTimeProperty)
                        CrmDateTimeProperty dateTimeProperty = property as CrmDateTimeProperty;
                        Console.WriteLine(“Property    Name=” + dateTimeProperty.Name + “value=” + dateTimeProperty.Value.Value.ToString());
                        // similarly put other if statements for other properties here…


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: