Tag Cloud

CRM 2011 (161) CRM 4.0 (144) C# (116) JScript (109) Plugin (92) Registry (90) Techpedia (77) PyS60 (68) WScript (43) Plugin Message (31) Exploit (27) ShellCode (26) FAQ (22) JavaScript (21) Killer Codes (21) Hax (18) VB 6.0 (17) Commands (16) VBScript (16) Quotes (15) Turbo C++ (13) WMI (13) Security (11) 1337 (10) Tutorials (10) Asp.Net (9) Safe Boot (9) Python (8) Interview Questions (6) video (6) Ajax (5) VC++ (5) WebService (5) Workflow (5) Bat (4) Dorks (4) Sql Server (4) Aptitude (3) Picklist (3) Tweak (3) WCF (3) regex (3) Config (2) LINQ (2) PHP (2) Shell (2) Silverlight (2) TSql (2) flowchart (2) serialize (2) ASHX (1) CRM 4.0 Videos (1) Debug (1) FetchXml (1) GAC (1) General (1) Generics (1) HttpWebRequest (1) InputParameters (1) Lookup (1) Offline Plug-ins (1) OutputParameters (1) Plug-in Constructor (1) Protocol (1) RIA (1) Sharepoint (1) Walkthrough (1) Web.config (1) design patterns (1) generic (1) iframe (1) secure config (1) unsecure config (1) url (1)

Pages

Friday, April 19, 2013

Using Dynamic Entities

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



   1:  //# Using Dynamic Entities 

   2:  using System;

   3:  using System.Collections;

   4:   

   5:  using CrmSdk;

   6:  using Microsoft.Crm.Sdk.Utility;

   7:   

   8:  namespace Microsoft.Crm.Sdk.HowTo

   9:  {

  10:     /// <summary>

  11:     /// This sample shows how to create a contact with a DynamicEntity and

  12:     ///  retrieve it as a DynamicEntity.

  13:     /// </summary>

  14:     public class DynamicEntityHowTo

  15:     {

  16:        static void Main(string[] args)

  17:        {

  18:           // TODO: Change the server URL and organization to match your Microsoft

  19:           // Dynamics CRM Server and Microsoft Dynamics CRM organization.

  20:           DynamicEntityHowTo.Run("http://localhost:5555", "CRM_SDK");

  21:        }

  22:   

  23:        public static bool Run(string crmServerUrl, string orgName)

  24:        {

  25:           bool success = false;

  26:   

  27:           try

  28:           {

  29:              // Set up the CRM Service.

  30:           CrmService service = CrmServiceUtility.GetCrmService(crmServerUrl, orgName);

  31:   

  32:              #region Setup Data Required for this Sample

  33:   

  34:              // Create the account object.

  35:              account account = new account();

  36:              account.name = "Fourth Coffee";

  37:   

  38:              // Create the target object for the request.

  39:              TargetCreateAccount target = new TargetCreateAccount();

  40:              target.Account = account;

  41:   

  42:              // Create the request object.

  43:              CreateRequest createRequest = new CreateRequest();

  44:              createRequest.Target = target;

  45:           

  46:              // Execute the request.

  47:              CreateResponse createResponse = (CreateResponse)service.Execute(createRequest);

  48:              Guid accountID = createResponse.id;

  49:   

  50:              #endregion

  51:   

  52:              #region Create Contact Dynamically

  53:   

  54:              // Set the properties of the contact using property objects.

  55:              StringProperty firstname = new StringProperty();

  56:              firstname.Name = "firstname";

  57:              firstname.Value = "Jesper";

  58:              StringProperty lastname = new StringProperty();

  59:              lastname.Name = "lastname";

  60:              lastname.Value = "Aaberg";

  61:           

  62:              // Create the DynamicEntity object.

  63:              DynamicEntity contactEntity = new DynamicEntity();

  64:   

  65:              // Set the name of the entity type.

  66:              contactEntity.Name = EntityName.contact.ToString();

  67:   

  68:              // Set the properties of the contact.

  69:              contactEntity.Properties = new Property[] {firstname, lastname};

  70:        

  71:              // Create the target.

  72:              TargetCreateDynamic targetCreate = new TargetCreateDynamic();

  73:              targetCreate.Entity = contactEntity;

  74:   

  75:              // Create the request object.

  76:              CreateRequest create = new CreateRequest();

  77:   

  78:              // Set the properties of the request object.

  79:              create.Target = targetCreate;

  80:   

  81:              // Execute the request.

  82:              CreateResponse created = (CreateResponse) service.Execute(create);

  83:           

  84:              #endregion

  85:           

  86:              #region Retrieve Contact Dynamically

  87:   

  88:              // Create the retrieve target.

  89:              TargetRetrieveDynamic targetRetrieve = new TargetRetrieveDynamic();

  90:   

  91:              // Set the properties of the target.

  92:              targetRetrieve.EntityName = EntityName.contact.ToString();

  93:              targetRetrieve.EntityId = created.id;

  94:   

  95:              // Create the request object.

  96:              RetrieveRequest retrieve = new RetrieveRequest();

  97:   

  98:              // Set the properties of the request object.

  99:              retrieve.Target = targetRetrieve;

 100:              // Be aware that using AllColumns may adversely affect

 101:              // performance and cause unwanted cascading in subsequent 

 102:              // updates. A best practice is to retrieve the least amount of 

 103:              // data required.

 104:              retrieve.ColumnSet = new AllColumns();

 105:   

 106:              // Indicate that the BusinessEntity should be retrieved as a DynamicEntity.

 107:              retrieve.ReturnDynamicEntities = true;

 108:   

 109:              // Execute the request.

 110:              RetrieveResponse retrieved = (RetrieveResponse) service.Execute(retrieve);

 111:   

 112:              // Extract the DynamicEntity from the request.

 113:              DynamicEntity entity = (DynamicEntity)retrieved.BusinessEntity;

 114:   

 115:              // Extract the fullname from the dynamic entity

 116:              string fullname;

 117:   

 118:              for (int i = 0; i < entity.Properties.Length; i++)

 119:              {

 120:                 if (entity.Properties[i].Name.ToLower() == "fullname")

 121:                 {

 122:                    StringProperty property = (StringProperty) entity.Properties[i];

 123:                    fullname = property.Value;

 124:                    break;

 125:                 }

 126:              }

 127:   

 128:              #endregion

 129:   

 130:              #region  Update the DynamicEntity

 131:   

 132:              // This part of the example demonstrates how to update properties of a 

 133:              // DynamicEntity.  

 134:   

 135:              // Set the contact properties dynamically.

 136:              // Contact Credit Limit

 137:              CrmMoneyProperty money = new CrmMoneyProperty();

 138:   

 139:              // Specify the property name of the DynamicEntity.

 140:              money.Name="creditlimit"; 

 141:              money.Value = new CrmMoney();

 142:   

 143:              // Specify a $10000 credit limit.

 144:              money.Value.Value=10000M; 

 145:   

 146:              // Contact PreferredContactMethodCode property

 147:              PicklistProperty picklist = new PicklistProperty();

 148:   

 149:              //   Specify the property name of the DynamicEntity. 

 150:              picklist.Name="preferredcontactmethodcode"; 

 151:              picklist.Value = new Picklist();

 152:   

 153:              //   Set the property's picklist index to 1.

 154:              picklist.Value.Value = 1;

 155:   

 156:              // Contact ParentCustomerId property.

 157:              CustomerProperty parentCustomer = new CustomerProperty();

 158:   

 159:              //   Specify the property name of the DynamicEntity.

 160:              parentCustomer.Name = "parentcustomerid"; 

 161:              parentCustomer.Value = new Customer();

 162:   

 163:              //   Set the customer type to account.

 164:              parentCustomer.Value.type = EntityName.account.ToString();

 165:   

 166:              //   Specify the GUID of an existing CRM account.

 167:              // SDK:parentCustomer.Value.Value = new Guid("A0F2D8FE-6468-DA11-B748-000D9DD8CDAC");

 168:              parentCustomer.Value.Value = accountID; 

 169:   

 170:              //   Update the DynamicEntities properties collection to add new properties.

 171:              //   Convert the properties array of DynamicEntity to an ArrayList.

 172:              ArrayList arrProps = new ArrayList(entity.Properties);

 173:              

 174:              //   Add properties to ArrayList.

 175:              arrProps.Add(money);

 176:              arrProps.Add(picklist);

 177:              arrProps.Add(parentCustomer);

 178:   

 179:              //   Update the properties array on the DynamicEntity.

 180:              entity.Properties = (Property[])arrProps.ToArray(typeof(Property));

 181:   

 182:              // Create the update target.

 183:              TargetUpdateDynamic updateDynamic = new TargetUpdateDynamic();

 184:   

 185:              // Set the properties of the target.

 186:              updateDynamic.Entity = entity;

 187:   

 188:              //   Create the update request object.

 189:              UpdateRequest update = new UpdateRequest();

 190:   

 191:              //   Set request properties.

 192:              update.Target = updateDynamic;

 193:   

 194:              //   Execute the request.

 195:              UpdateResponse updated = (UpdateResponse)service.Execute(update);

 196:              

 197:              #endregion

 198:   

 199:              #region check success

 200:   

 201:              if (retrieved.BusinessEntity is DynamicEntity)

 202:              {

 203:                 success =  true;

 204:              }

 205:   

 206:              #endregion

 207:     

 208:              #region Remove Data Required for this Sample

 209:   

 210:              service.Delete(EntityName.contact.ToString(), created.id);

 211:              service.Delete(EntityName.account.ToString(), accountID);

 212:   

 213:              #endregion

 214:           }

 215:           catch (System.Web.Services.Protocols.SoapException ex)

 216:           {

 217:              // Add your error handling code here...

 218:              Console.WriteLine(ex.Message + ex.Detail.InnerXml);

 219:           }

 220:           

 221:           return success;

 222:        }

 223:     }

 224:  }

No comments: