fork download
  1. using System;
  2. using System.ServiceModel;
  3. using System.IdentityModel.Tokens;
  4. using System.ServiceModel.Security;
  5.  
  6. // These namespaces are found in the Microsoft.Xrm.Sdk.dll and
  7. // Microsoft.Crm.Sdk.Proxy.dll assemblies.
  8. using Microsoft.Xrm.Sdk;
  9. using Microsoft.Xrm.Sdk.Query;
  10. using Microsoft.Xrm.Sdk.Client;
  11. using Microsoft.Crm.Sdk.Messages;
  12.  
  13. namespace Microsoft.Crm.Sdk.Samples
  14. {
  15. /// <summary>
  16. /// Demonstrates how to perform create, retrieve, update, and delete entity
  17. /// record operations.</summary>
  18. /// <remarks>
  19. /// At run-time, you will be given the option to delete all the
  20. /// entity records created by this program.</remarks>
  21. public class CRUDOperations
  22. {
  23. static public void Main(string[] args)
  24. {
  25. // The connection to the Organization web service.
  26. OrganizationServiceProxy serviceProxy = null;
  27.  
  28. try
  29. {
  30. // Obtain the target organization's web address and client logon credentials
  31. // from the user by using a helper class.
  32. ServerConnection serverConnect = new ServerConnection();
  33. ServerConnection.Configuration config = serverConnect.GetServerConfiguration();
  34.  
  35. // Establish an authenticated connection to the Organization web service.
  36. serviceProxy = new OrganizationServiceProxy(config.OrganizationUri, config.HomeRealmUri,
  37. config.Credentials, config.DeviceCredentials);
  38.  
  39. CRUDOperations app = new CRUDOperations();
  40.  
  41. // Create any records that must exist in the database. These record references are
  42. // stored in a collection so the records can be deleted later.
  43. EntityReferenceCollection records =
  44. app.CreateRequiredEntityRecords(serviceProxy);
  45.  
  46. // Perform the primary operation of this sample.
  47. app.Run(serviceProxy, records);
  48.  
  49. // Delete all remaining records that were created by this sample.
  50. app.DeleteEntityRecords(serviceProxy, records, true);
  51. }
  52.  
  53. // Some exceptions to consider catching.
  54. catch (FaultException<Microsoft.Xrm.Sdk.OrganizationServiceFault> e) { HandleException(e); }
  55. catch (TimeoutException e) { HandleException(e); }
  56. catch (SecurityTokenValidationException e) { HandleException(e); }
  57. catch (ExpiredSecurityTokenException e) { HandleException(e); }
  58. catch (MessageSecurityException e) { HandleException(e); }
  59. catch (SecurityNegotiationException e) { HandleException(e); }
  60. catch (SecurityAccessDeniedException e) { HandleException(e); }
  61. catch (Exception e) { HandleException(e); }
  62.  
  63. finally
  64. {
  65. // Always dispose the service object to close the service connection and free resources.
  66. if (serviceProxy != null) serviceProxy.Dispose();
  67.  
  68. Console.WriteLine("Press <Enter> to exit.");
  69. Console.ReadLine();
  70. }
  71. }
  72.  
  73. /// <summary>
  74. /// This method performs entity create, retrieve, and update operations.
  75. /// The delete operation is handled in the DeleteRequiredrecords() method.
  76. /// </summary>
  77. /// <param name="serviceProxy">An established connection to the Organization web service.</param>
  78. /// <param name="records">A collection of entity records created by this sample.</param>
  79. public void Run(OrganizationServiceProxy serviceProxy, EntityReferenceCollection records)
  80. {
  81. // Enable early-bound entity types. This enables use of IntelliSense in Visual Studio
  82. // and avoids spelling errors in attribute names when using the Entity property bag.
  83. serviceProxy.EnableProxyTypes();
  84.  
  85. // Here we will use the interface instead of the proxy object.
  86. IOrganizationService service = (IOrganizationService)serviceProxy;
  87.  
  88. // Display information about the logged on user.
  89. Guid userid = ((WhoAmIResponse)service.Execute(new WhoAmIRequest())).UserId;
  90. SystemUser systemUser = (SystemUser)service.Retrieve("systemuser", userid,
  91. new ColumnSet(new string[] {"firstname", "lastname"}));
  92. Console.WriteLine("Logged on user is {0} {1}.", systemUser.FirstName, systemUser.LastName);
  93.  
  94. // Retrieve the version of Microsoft Dynamics CRM.
  95. RetrieveVersionRequest versionRequest = new RetrieveVersionRequest();
  96. RetrieveVersionResponse versionResponse =
  97. (RetrieveVersionResponse)service.Execute(versionRequest);
  98. Console.WriteLine("Microsoft Dynamics CRM version {0}.", versionResponse.Version);
  99.  
  100. // Instantiate an account object. Note the use of the option set enumerations defined
  101. // in OptionSets.cs.
  102. Account account = new Account { Name = "Fourth Coffee" };
  103. account.AccountCategoryCode = new OptionSetValue((int)AccountAccountCategoryCode.PreferredCustomer);
  104. account.CustomerTypeCode = new OptionSetValue((int)AccountCustomerTypeCode.Investor);
  105.  
  106. // Create an account record named Fourth Coffee.
  107. // Save the record reference so we can delete it during cleanup later.
  108. Guid accountId = service.Create(account);
  109. records.Add(new EntityReference(Account.EntityLogicalName, accountId));
  110. Console.Write("{0} {1} created, ", account.LogicalName, account.Name);
  111.  
  112. // Retrieve the account containing several of its attributes. This results in
  113. // better performance compared to retrieving all attributes.
  114. ColumnSet cols = new ColumnSet(
  115. new String[] { "name", "address1_postalcode", "lastusedincampaign" });
  116.  
  117. Account retrievedAccount = (Account)service.Retrieve("account", accountId, cols);
  118. Console.Write("retrieved, ");
  119.  
  120. // Update the postal code attribute.
  121. retrievedAccount.Address1_PostalCode = "98052";
  122.  
  123. // There is no address 2 postal code needed.
  124. retrievedAccount.Address2_PostalCode = null;
  125.  
  126. // Shows use of a Money value.
  127. retrievedAccount.Revenue = new Money(5000000);
  128.  
  129. // Shows use of a Boolean value.
  130. retrievedAccount.CreditOnHold = false;
  131.  
  132. // Update the account record.
  133. service.Update(retrievedAccount);
  134. Console.WriteLine("and updated.");
  135. }
  136.  
  137. /// <summary>
  138. /// Create any entity records that the Run() method requires.
  139. /// </summary>
  140. public EntityReferenceCollection CreateRequiredEntityRecords(OrganizationServiceProxy service)
  141. {
  142. // For this sample, all required entity records are created in the Run() method.
  143. return new EntityReferenceCollection();
  144. }
  145.  
  146. /// <summary>
  147. /// Delete all remaining entity records that were created by this sample.
  148. /// <param name="prompt">When true, the user is prompted whether
  149. /// the records created in this sample should be deleted; otherwise, false.</param>
  150. /// </summary>
  151. public void DeleteEntityRecords(OrganizationServiceProxy service,
  152. EntityReferenceCollection records, bool prompt)
  153. {
  154. bool deleteRecords = true;
  155.  
  156. if (prompt)
  157. {
  158. Console.WriteLine("\nDo you want these entity records deleted? (y/n) [y]: ");
  159. String answer = Console.ReadLine();
  160.  
  161. deleteRecords = (answer.StartsWith("y") || answer.StartsWith("Y") || answer == String.Empty);
  162. }
  163.  
  164. if (deleteRecords)
  165. {
  166. while (records.Count > 0)
  167. {
  168. EntityReference entityRef = records[records.Count-1];
  169. service.Delete(entityRef.LogicalName, entityRef.Id);
  170. records.Remove(entityRef);
  171. }
  172.  
  173. Console.WriteLine("Entity records have been deleted.");
  174. }
  175. }
  176.  
  177. /// Handle a thrown exception.
  178. /// </summary>
  179. /// <param name="ex">An exception.</param>
  180. private static void HandleException(Exception e)
  181. {
  182. // Display the details of the exception.
  183. Console.WriteLine("\n" + e.Message);
  184. Console.WriteLine(e.StackTrace);
  185.  
  186. if (e.InnerException != null) HandleException(e.InnerException);
  187. }
  188. }
  189. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cs(2,14): error CS0234: The type or namespace name `ServiceModel' does not exist in the namespace `System'. Are you missing `System.ServiceModel' assembly reference?
prog.cs(3,14): error CS0234: The type or namespace name `IdentityModel' does not exist in the namespace `System'. Are you missing an assembly reference?
prog.cs(4,14): error CS0234: The type or namespace name `ServiceModel' does not exist in the namespace `System'. Are you missing `System.ServiceModel' assembly reference?
prog.cs(8,17): error CS0234: The type or namespace name `Xrm' does not exist in the namespace `Microsoft'. Are you missing an assembly reference?
prog.cs(9,17): error CS0234: The type or namespace name `Xrm' does not exist in the namespace `Microsoft'. Are you missing an assembly reference?
prog.cs(10,17): error CS0234: The type or namespace name `Xrm' does not exist in the namespace `Microsoft'. Are you missing an assembly reference?
prog.cs(11,25): error CS0234: The type or namespace name `Messages' does not exist in the namespace `Microsoft.Crm.Sdk'. Are you missing an assembly reference?
prog.cs(79,25): error CS0246: The type or namespace name `OrganizationServiceProxy' could not be found. Are you missing an assembly reference?
prog.cs(79,64): error CS0246: The type or namespace name `EntityReferenceCollection' could not be found. Are you missing an assembly reference?
prog.cs(140,16): error CS0246: The type or namespace name `EntityReferenceCollection' could not be found. Are you missing an assembly reference?
prog.cs(151,41): error CS0246: The type or namespace name `OrganizationServiceProxy' could not be found. Are you missing an assembly reference?
prog.cs(152,41): error CS0246: The type or namespace name `EntityReferenceCollection' could not be found. Are you missing an assembly reference?
Compilation failed: 12 error(s), 0 warnings
stdout
Standard output is empty