Export (0) Print
Expand All

Using Dynamic Entities

CRM 1.2
banner art

This sample shows how to create, retrieve, and update a contact as a DynamicEntity.

Example

[C#]
using System;
using System.Collections;
using CrmSdk;

namespace Microsoft.Crm.Sdk.HowTo
{
   /// <summary>
   /// This sample shows how to create a contact with a DynamicEntity and
   /// retrieve it as a DynamicEntity.
   /// </summary>
   public class DynamicEntityHowTo
   {
      public DynamicEntityHowTo()
      {
      }

      public static bool Run()
      {
         bool success = false;

         try
         {
            // Set up the CRM Service.
            CrmService service = new CrmService();
            service.Credentials =
                System.Net.CredentialCache.DefaultCredentials;

            #region Setup Data Required for this Sample

            // Create the account object.
            account account = new account();
            account.name = "Fourth Coffee";

            // Create the target object for the request.
            TargetCreateAccount target = new TargetCreateAccount();
            target.Account = account;

            // Create the request object.
            CreateRequest createRequest = new CreateRequest();
            createRequest.Target = target;

            // Execute the request.
            CreateResponse createResponse =
               (CreateResponse)service.Execute(createRequest);
            Guid accountID = createResponse.id;

            #endregion

            #region Create Contact Dynamically

            // Set the properties of the contact using property objects.
            StringProperty firstname = new StringProperty();
            firstname.Name = "firstname";
            firstname.Value = "Jesper";
            StringProperty lastname = new StringProperty();
            lastname.Name = "lastname";
            lastname.Value = "Aaberg";
         
            // Create the DynamicEntity object.
            DynamicEntity contactEntity = new DynamicEntity();

            // Set the name of the entity type.
            contactEntity.Name = EntityName.contact.ToString();

            // Set the properties of the contact.
            contactEntity.Properties = new Property[] 
                {firstname, lastname};
      
            // Create the target.
            TargetCreateDynamic targetCreate = new TargetCreateDynamic();
            targetCreate.Entity = contactEntity;

            // Create the request object.
            CreateRequest create = new CreateRequest();

            // Set the properties of the request object.
            create.Target = targetCreate;

            // Execute the request.
            CreateResponse created = 
                (CreateResponse) service.Execute(create);
         
            #endregion
         
            #region Retrieve Contact Dynamically

            // 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;
            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;
               }
            }

            #endregion

            #region  Update the DynamicEntity

            // This part of the example demonstrates how to update
            // properties of a DynamicEntity.  

            // Set the contact properties dynamically.
            // Contact Credit Limit
            CrmMoneyProperty money = new CrmMoneyProperty();

            // Specify the property name of the DynamicEntity.
            money.Name="creditlimit"; 
            money.Value = new CrmMoney();

            // Specify a $10000 credit limit.
            money.Value.Value=10000M; 

            // Contact PreferredContactMethodCode property
            PicklistProperty picklist = new PicklistProperty();

            // Specify the property name of the DynamicEntity. 
            picklist.Name="preferredcontactmethodcode"; 
            picklist.Value = new Picklist();

            // Set the property's picklist index to 1.
            picklist.Value.Value = 1;

            // Contact ParentCustomerId property.
            CustomerProperty parentCustomer = new CustomerProperty();

            // Specify the property name of the DynamicEntity.
            parentCustomer.Name = "parentcustomerid"; 
            parentCustomer.Value = new Customer();

            // Set the customer type to account.
            parentCustomer.Value.type = EntityName.account.ToString();

            // Specify the GUID of an existing CRM account.
            parentCustomer.Value.Value = accountID; 

            // Update the DynamicEntities properties collection to add new
            // properties. Convert the properties array of DynamicEntity 
            // to an ArrayList.
            ArrayList arrProps = new ArrayList(entity.Properties);

            // Add properties to ArrayList.
            arrProps.Add(money);
            arrProps.Add(picklist);
            arrProps.Add(parentCustomer);

            // Update the properties array on the DynamicEntity.
            entity.Properties =
                (Property[])arrProps.ToArray(typeof(Property));

            // Create the update target.
            TargetUpdateDynamic updateDynamic = new TargetUpdateDynamic();

            // Set the properties of the target.
            updateDynamic.Entity = entity;

            // Create the update request object.
            UpdateRequest update = new UpdateRequest();

            // Set request properties.
            update.Target = updateDynamic;

            // Execute the request.
            UpdateResponse updated =
                (UpdateResponse)service.Execute(update);

            #endregion

            #region check success

            if (retrieved.BusinessEntity is DynamicEntity)
            {
               success =  true;
            }

            #endregion
   
            #region Remove Data Required for this Sample

            service.Delete(EntityName.contact.ToString(), created.id);
            service.Delete(EntityName.account.ToString(), accountID);

            #endregion
         }
         catch (System.Web.Services.Protocols.SoapException ex)
         {
            // Add your error handling code here...
            Console.WriteLine(ex.Message + ex.Detail.InnerXml);
         }
         
         return success;
      }
   }
}
[Visual Basic .NET]
Imports System
Imports System.Collections
Imports CrmSdk

Namespace Microsoft.Crm.Sdk.HowTo
   ' <summary>
   ' This sample shows how to create a contact with a DynamicEntity and
   ' retrieve it as a DynamicEntity.
   ' </summary>
   Public Class DynamicEntityHowTo
      Public Sub New()
      End Sub

      Public Shared Function Run() As Boolean
         Dim success As Boolean = False

         Try
            ' Set up the CRM Service.
            Dim service As CrmService = New CrmService
            service.Credentials =
                System.Net.CredentialCache.DefaultCredentials

            '#region Setup Data Required for this Sample

            ' Create the account object.
            Dim account As account = New account
            account.name = "Fourth Coffee"

            ' Create the target object for the request.
            Dim target As TargetCreateAccount = New TargetCreateAccount
            target.Account = account

            ' Create the request object.
            Dim createRequest As CreateRequest = New CreateRequest
            createRequest.Target = target

            ' Execute the request.
            Dim createResponse As CreateResponse =
                CType(service.Execute(createRequest), CreateResponse)
            Dim accountID As Guid = createResponse.id

            '#endregion

            '#region Create Contact Dynamically

            ' Set the properties of the contact using property objects.
            Dim firstname As StringProperty = New StringProperty
            firstname.Name = "firstname"
            firstname.Value = "Jesper"
            Dim lastname As StringProperty = New StringProperty
            lastname.Name = "lastname"
            lastname.Value = "Aaberg"

            ' Create the DynamicEntity object.
            Dim contactEntity As DynamicEntity = New DynamicEntity

            ' Set the name of the entity type.
            contactEntity.Name = EntityName.contact.ToString()

            ' Set the properties of the contact.
            contactEntity.Properties = New StringProperty() 
                {firstname, lastname}

            ' Create the target.
            Dim targetCreate As TargetCreateDynamic =
                New TargetCreateDynamic
            targetCreate.Entity = contactEntity

            ' Create the request object.
            Dim create As CreateRequest = New CreateRequest

            ' Set the properties of the request object.
            create.Target = targetCreate

            ' Execute the request.
            Dim created As CreateResponse = 
                CType(service.Execute(create), CreateResponse)

            '#endregion

            '#region Retrieve Contact Dynamically

            ' Create the retrieve target.
            Dim targetRetrieve As TargetRetrieveDynamic =
                New TargetRetrieveDynamic

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

            ' Create the request object.
            Dim retrieve As RetrieveRequest = New RetrieveRequest

            ' Set the properties of the request object.
            retrieve.Target = targetRetrieve
            retrieve.ColumnSet = New AllColumns

            ' Indicate that the BusinessEntity should be retrieved as a
            ' DynamicEntity.
            retrieve.ReturnDynamicEntities = True

            ' Execute the request.
            Dim retrieved As RetrieveResponse =
                CType(service.Execute(retrieve), RetrieveResponse)

            ' Extract the DynamicEntity from the request.
            Dim entity As DynamicEntity = 
                CType(retrieved.BusinessEntity, DynamicEntity)

            ' Extract the fullname from the dynamic entity
            Dim fullname As String

            Dim i As Integer
            For i = 0 To entity.Properties.Length - 1 Step i + 1
               If entity.Properties(i).Name.ToLower() = "fullname" Then
                  Dim [property] As StringProperty =
                      CType(entity.Properties(i), StringProperty)
                  fullname = [property].Value
                  Exit Function
               End If
            Next

            '#endregion

            '#region  Update the DynamicEntity

            ' This part of the example demonstrates how to update
            ' properties of a DynamicEntity.  

            ' Set the contact properties dynamically.
            ' Contact Credit Limit
            Dim money As CrmMoneyProperty = New CrmMoneyProperty

            ' Specify the property name of the DynamicEntity.
            money.Name = "creditlimit"
            money.Value = New CrmMoney

            ' Specify a $10000 credit limit.
            money.Value.Value = 10000D

            ' Contact PreferredContactMethodCode property
            Dim picklist As PicklistProperty = New PicklistProperty

            ' Specify the property name of the DynamicEntity. 
            picklist.Name = "preferredcontactmethodcode"
            picklist.Value = New Picklist

            ' Set the property's picklist index to 1.
            picklist.Value.Value = 1

            ' Contact ParentCustomerId property.
            Dim parentCustomer As CustomerProperty = New CustomerProperty

            ' Specify the property name of the DynamicEntity.
            parentCustomer.Name = "parentcustomerid"
            parentCustomer.Value = New Customer

            ' Set the customer type to account.
            parentCustomer.Value.type = EntityName.account.ToString()

            ' Specify the GUID of an existing CRM account.
            parentCustomer.Value.Value = accountID

            ' Update the DynamicEntities properties collection to add new
            ' properties. Convert the properties array of DynamicEntity to 
            ' an ArrayList.
            Dim arrProps As ArrayList = New ArrayList(entity.Properties)

            ' Add properties to ArrayList.
            arrProps.Add(money)
            arrProps.Add(picklist)
            arrProps.Add(parentCustomer)

            ' Update the properties array on the DynamicEntity.
            entity.Properties =
                CType(arrProps.ToArray(GetType([Property])), [Property]())

            ' Create the update target.
            Dim updateDynamic As TargetUpdateDynamic =
                New TargetUpdateDynamic

            ' Set the properties of the target.
            updateDynamic.Entity = entity

            ' Create the update request object.
            Dim update As UpdateRequest = New UpdateRequest

            ' Set request properties.
            update.Target = updateDynamic

            ' Execute the request.
            Dim updated As UpdateResponse =
                CType(service.Execute(update), UpdateResponse)

            '#endregion

            '#region check success

            If TypeOf retrieved.BusinessEntity Is DynamicEntity Then
               success = True
            End If

            '#endregion

            '#region Remove Data Required for this Sample

            service.Delete(EntityName.contact.ToString(), created.id)
            service.Delete(EntityName.account.ToString(), accountID)

            '#endregion

         Catch ex As System.Web.Services.Protocols.SoapException
            ' Add your error handling code here...
            Console.WriteLine(ex.Message + ex.Detail.InnerXml)
         End Try

         Return success
      End Function
   End Class
End Namespace

© 2007 Microsoft Corporation. All rights reserved.


Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft