Search

Using Dynamic Entities

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





//# Using Dynamic Entities
using System;
using System.Collections;

using CrmSdk;
using Microsoft.Crm.Sdk.Utility;

namespace Microsoft.Crm.Sdk.HowTo
{
///
/// This sample shows how to create a contact with a DynamicEntity and
/// retrieve it as a DynamicEntity.
///

public class DynamicEntityHowTo
{
static void Main(string[] args)
{
// TODO: Change the server URL and organization to match your Microsoft
// Dynamics CRM Server and Microsoft Dynamics CRM organization.
DynamicEntityHowTo.Run("http://localhost:5555", "CRM_SDK");
}

public static bool Run(string crmServerUrl, string orgName)
{
bool success = false;

try
{
// Set up the CRM Service.
CrmService service = CrmServiceUtility.GetCrmService(crmServerUrl, orgName);

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

#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.
// SDK:parentCustomer.Value.Value = new Guid("A0F2D8FE-6468-DA11-B748-000D9DD8CDAC");
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;
}
}
}