/// <summary> /// This method creates any entity records that this sample requires. /// Creates the email activity. /// </summary> public static void CreateRequiredRecords(CrmServiceClient service) { // TODO Create entity records // Create a Connection Role for account and contact var Categories = new { Business = 1, Family = 2, Social = 3, Sales = 4, Other = 5 }; ConnectionRole newConnectionRole = new ConnectionRole { Name = "Example Connection Role", Category = new OptionSetValue(Categories.Business) }; _connectionRoleId = _serviceProxy.Create(newConnectionRole); Console.WriteLine("Created {0}.", newConnectionRole.Name); // Create a related Connection Role Object Type Code record for Account ConnectionRoleObjectTypeCode newAccountConnectionRoleTypeCode = new ConnectionRoleObjectTypeCode { ConnectionRoleId = new EntityReference( ConnectionRole.EntityLogicalName, _connectionRoleId), AssociatedObjectTypeCode = Account.EntityLogicalName }; _serviceProxy.Create(newAccountConnectionRoleTypeCode); Console.WriteLine( "Created a related Connection Role Object Type Code record for Account."); // Create a related Connection Role Object Type Code record for Contact ConnectionRoleObjectTypeCode newContactConnectionRoleTypeCode = new ConnectionRoleObjectTypeCode { ConnectionRoleId = new EntityReference( ConnectionRole.EntityLogicalName, _connectionRoleId), AssociatedObjectTypeCode = Contact.EntityLogicalName }; _serviceProxy.Create(newContactConnectionRoleTypeCode); Console.WriteLine("Created a related Connection Role Object Type Code record for Contact."); }
/// <summary> /// This method creates any entity records that this sample requires. /// Create a new connectionrole instance. /// Create related Connection Role Object Type Code records /// for the account and the contact entities. /// </summary> public void CreateRequiredRecords() { // Create a Connection Role for account and contact ConnectionRole newConnectionRole = new ConnectionRole { Name = "Example Connection Role", Category = new OptionSetValue((int)connectionrole_category.Business) }; _connectionRoleId = _serviceProxy.Create(newConnectionRole); Console.WriteLine("Created {0}.", newConnectionRole.Name); // Create a related Connection Role Object Type Code record for Account ConnectionRoleObjectTypeCode newAccountConnectionRoleTypeCode = new ConnectionRoleObjectTypeCode { ConnectionRoleId = new EntityReference( ConnectionRole.EntityLogicalName, _connectionRoleId), AssociatedObjectTypeCode = Account.EntityLogicalName }; _serviceProxy.Create(newAccountConnectionRoleTypeCode); Console.WriteLine( "Created a related Connection Role Object Type Code record for Account." ); // Create a related Connection Role Object Type Code record for Contact ConnectionRoleObjectTypeCode newContactConnectionRoleTypeCode = new ConnectionRoleObjectTypeCode { ConnectionRoleId = new EntityReference( ConnectionRole.EntityLogicalName, _connectionRoleId), AssociatedObjectTypeCode = Contact.EntityLogicalName }; _serviceProxy.Create(newContactConnectionRoleTypeCode); Console.WriteLine( "Created a related Connection Role Object Type Code record for Contact." ); // Associate the connection role with itself. AssociateRequest associateConnectionRoles = new AssociateRequest { Target = new EntityReference(ConnectionRole.EntityLogicalName, _connectionRoleId), RelatedEntities = new EntityReferenceCollection() { new EntityReference(ConnectionRole.EntityLogicalName, _connectionRoleId) }, // The name of the relationship connection role association // relationship in MS CRM. Relationship = new Relationship() { PrimaryEntityRole = EntityRole.Referencing, // Referencing or Referenced based on N:1 or 1:N reflexive relationship. SchemaName = "connectionroleassociation_association" } }; _serviceProxy.Execute(associateConnectionRoles); Console.WriteLine("Associated the connection role with itself."); // Create an Account Account setupAccount = new Account { Name = "Example Account" }; _accountId = _serviceProxy.Create(setupAccount); Console.WriteLine("Created {0}.", setupAccount.Name); // Create a Contact Contact setupContact = new Contact { LastName = "Example Contact" }; _contactId = _serviceProxy.Create(setupContact); Console.WriteLine("Created {0}.", setupContact.LastName); return; }
/// <summary> /// Create and configure the organization service proxy. /// Create a new connectionrole instance and set the object type. /// Optionally delete any entity records that were created for this sample. /// </summary> /// <param name="serverConfig">Contains server connection information.</param> /// <param name="promptforDelete">When True, the user will be prompted to delete all /// created entities.</param> public void Run(ServerConnection.Configuration serverConfig, bool promptForDelete) { try { // Connect to the Organization service. // The using statement assures that the service proxy will be properly disposed. using (_serviceProxy = new OrganizationServiceProxy(serverConfig.OrganizationUri, serverConfig.HomeRealmUri, serverConfig.Credentials, serverConfig.DeviceCredentials)) { // This statement is required to enable early-bound type support. _serviceProxy.EnableProxyTypes(); //<snippetCreateConnectionRole1> // Define some anonymous types to define the range // of possible connection property values. var Categories = new { Business = 1, Family = 2, Social = 3, Sales = 4, Other = 5 }; // Create a Connection Role for account and contact ConnectionRole newConnectionRole = new ConnectionRole { Name = "Example Connection Role", Category = new OptionSetValue(Categories.Business) }; _connectionRoleId = _serviceProxy.Create(newConnectionRole); Console.WriteLine("Created {0}.", newConnectionRole.Name); // Create a related Connection Role Object Type Code record for Account ConnectionRoleObjectTypeCode newAccountConnectionRoleTypeCode = new ConnectionRoleObjectTypeCode { ConnectionRoleId = new EntityReference( ConnectionRole.EntityLogicalName, _connectionRoleId), AssociatedObjectTypeCode = Account.EntityLogicalName }; _serviceProxy.Create(newAccountConnectionRoleTypeCode); Console.WriteLine( "Created a related Connection Role Object Type Code record for Account."); // Create a related Connection Role Object Type Code record for Contact ConnectionRoleObjectTypeCode newContactConnectionRoleTypeCode = new ConnectionRoleObjectTypeCode { ConnectionRoleId = new EntityReference( ConnectionRole.EntityLogicalName, _connectionRoleId), AssociatedObjectTypeCode = Contact.EntityLogicalName }; _serviceProxy.Create(newContactConnectionRoleTypeCode); Console.WriteLine( "Created a related Connection Role Object Type Code record for Contact."); //</snippetCreateConnectionRole1> DeleteRequiredRecords(promptForDelete); } } // Catch any service fault exceptions that Microsoft Dynamics CRM throws. catch (FaultException<Microsoft.Xrm.Sdk.OrganizationServiceFault>) { // You can handle an exception here or pass it back to the calling method. throw; } }
[STAThread] // Added to support UX static void Main(string[] args) { CrmServiceClient service = null; try { service = SampleHelpers.Connect("Connect"); if (service.IsReady) { #region Sample Code ////////////////////////////////////////////// #region Set up SetUpSample(service); #endregion Set up #region Demonstrate // Define some anonymous types to define the range // of possible connection property values. var Categories = new { Business = 1, Family = 2, Social = 3, Sales = 4, Other = 5 }; // Create a Connection Role. ConnectionRole setupConnectionRole = new ConnectionRole { Name = "Example Connection Role", Category = new OptionSetValue(Categories.Business), }; _connectionRoleId = service.Create(setupConnectionRole); setupConnectionRole.Id = _connectionRoleId; Console.WriteLine("Created {0}.", setupConnectionRole.Name); // Query for all Connection Roles. QueryExpression allQuery = new QueryExpression { EntityName = ConnectionRole.EntityLogicalName, ColumnSet = new ColumnSet("connectionroleid", "name"), Distinct = true, LinkEntities = { new LinkEntity { LinkToEntityName = ConnectionRoleObjectTypeCode.EntityLogicalName, LinkToAttributeName = "connectionroleid", LinkFromEntityName = ConnectionRole.EntityLogicalName, LinkFromAttributeName = "connectionroleid", LinkCriteria = new FilterExpression { FilterOperator = LogicalOperator.And, // Set a condition to only get connection roles // related to all entities (object type code = 0). Conditions = { new ConditionExpression { AttributeName = "associatedobjecttypecode", Operator = ConditionOperator.Equal, Values = { 0 } } } } } } }; EntityCollection results = service.RetrieveMultiple(allQuery); // Here you could perform operations on all of // the connectionroles found by the query. Console.WriteLine("Retrieved {0} unassociated connectionrole instance(s).", results.Entities.Count); // Query to find roles which apply only to accounts. QueryExpression accountQuery = new QueryExpression { EntityName = ConnectionRole.EntityLogicalName, ColumnSet = new ColumnSet("connectionroleid", "name"), Distinct = true, LinkEntities = { new LinkEntity { LinkToEntityName = ConnectionRoleObjectTypeCode.EntityLogicalName, LinkToAttributeName = "connectionroleid", LinkFromEntityName = ConnectionRole.EntityLogicalName, LinkFromAttributeName = "connectionroleid", LinkCriteria = new FilterExpression { FilterOperator = LogicalOperator.And, // Set a condition to only get connection roles // related to accounts (object type code = 1). Conditions = { new ConditionExpression { AttributeName = "associatedobjecttypecode", Operator = ConditionOperator.In, Values = { Account.EntityLogicalName } } } } } } }; results = service.RetrieveMultiple(accountQuery); Console.WriteLine("Retrieved {0} account-only connectionrole instance(s).", results.Entities.Count); // Create a related Connection Role Object Type Code record for // Account. ConnectionRoleObjectTypeCode setupAccountConnectionRoleTypeCode = new ConnectionRoleObjectTypeCode { ConnectionRoleId = new EntityReference( ConnectionRole.EntityLogicalName, _connectionRoleId), AssociatedObjectTypeCode = Account.EntityLogicalName }; setupAccountConnectionRoleTypeCode.Id = service.Create(setupAccountConnectionRoleTypeCode); Console.Write("Created a related Connection Role Object Type Code"); Console.Write(" record for Account."); // Run the query to find unassociated connectionroles again. results = service.RetrieveMultiple(allQuery); Console.WriteLine(@"Retrieved {0} unassociated connectionrole instance(s).", results.Entities.Count); // Run the account-only query again. results = service.RetrieveMultiple(accountQuery); Console.WriteLine("Retrieved {0} account-only connectionrole instance(s).", results.Entities.Count); // Remove the link from account entity. service.Delete(ConnectionRoleObjectTypeCode.EntityLogicalName, setupAccountConnectionRoleTypeCode.Id); Console.WriteLine("Removed link from connectionrole to account entity."); // Run the query to find unassociated connectionroles again. results = service.RetrieveMultiple(allQuery); Console.WriteLine("Retrieved {0} unassociated connectionrole instance(s).", results.Entities.Count); // Run the account-only query again. results = service.RetrieveMultiple(accountQuery); Console.WriteLine("Retrieved {0} account-only connectionrole instance(s).", results.Entities.Count); #region Clean up CleanUpSample(service); #endregion Clean up } #endregion Demonstrate else { const string UNABLE_TO_LOGIN_ERROR = "Unable to Login to Common Data Service"; if (service.LastCrmError.Equals(UNABLE_TO_LOGIN_ERROR)) { Console.WriteLine("Check the connection string values in cds/App.config."); throw new Exception(service.LastCrmError); } else { throw service.LastCrmException; } } } #endregion Sample Code catch (Exception ex) { SampleHelpers.HandleException(ex); } finally { if (service != null) { service.Dispose(); } Console.WriteLine("Press <Enter> to exit."); Console.ReadLine(); } }
/// <summary> /// Create and configure the organization service proxy. /// Create the connection role instances. /// Associate the connection roles. /// Optionally delete any entity records that were created for this sample. /// </summary> /// <param name="serverConfig">Contains server connection information.</param> /// <param name="promptforDelete">When True, the user will be prompted to delete all /// created entities.</param> public void Run(ServerConnection.Configuration serverConfig, bool promptForDelete) { try { // Connect to the Organization service. // The using statement assures that the service proxy will be properly disposed. using (_serviceProxy = new OrganizationServiceProxy(serverConfig.OrganizationUri, serverConfig.HomeRealmUri, serverConfig.Credentials, serverConfig.DeviceCredentials)) { // This statement is required to enable early-bound type support. _serviceProxy.EnableProxyTypes(); // Create the Connection Role 1 ConnectionRole newConnectionRole1 = new ConnectionRole { Name = "Example Connection Role 1", Category = new OptionSetValue((int)connectionrole_category.Business), }; _connectionRole1Id = _serviceProxy.Create(newConnectionRole1); Console.WriteLine("Created {0}.", newConnectionRole1.Name); // Create a related Connection Role Object Type Code record for Account ConnectionRoleObjectTypeCode newAccountConnectionRole1TypeCode = new ConnectionRoleObjectTypeCode { ConnectionRoleId = new EntityReference( ConnectionRole.EntityLogicalName, _connectionRole1Id), AssociatedObjectTypeCode = Account.EntityLogicalName }; _serviceProxy.Create(newAccountConnectionRole1TypeCode); Console.WriteLine( "Created a related Connection Role 1 Object Type Code record for Account." ); // Create a related Connection Role Object Type Code record for Contact ConnectionRoleObjectTypeCode newContactConnectionRole1TypeCode = new ConnectionRoleObjectTypeCode { ConnectionRoleId = new EntityReference( ConnectionRole.EntityLogicalName, _connectionRole1Id), AssociatedObjectTypeCode = Contact.EntityLogicalName }; _serviceProxy.Create(newContactConnectionRole1TypeCode); Console.WriteLine( "Created a related Connection Role 1 Object Type Code record for Contact." ); // Create the Connection Role 2 ConnectionRole newConnectionRole2 = new ConnectionRole { Name = "Example Connection Role 2", Category = new OptionSetValue((int)connectionrole_category.Business), }; _connectionRole2Id = _serviceProxy.Create(newConnectionRole2); Console.WriteLine("Created {0}.", newConnectionRole2.Name); // Create a related Connection Role 2 Object Type Code record for Account ConnectionRoleObjectTypeCode newAccountConnectionRole2TypeCode = new ConnectionRoleObjectTypeCode { ConnectionRoleId = new EntityReference( ConnectionRole.EntityLogicalName, _connectionRole2Id), AssociatedObjectTypeCode = Account.EntityLogicalName }; _serviceProxy.Create(newAccountConnectionRole2TypeCode); Console.WriteLine( "Created a related Connection Role 2 Object Type Code record for Account." ); // Create a related Connection Role 2 Object Type Code record for Contact ConnectionRoleObjectTypeCode newContactConnectionRole2TypeCode = new ConnectionRoleObjectTypeCode { ConnectionRoleId = new EntityReference( ConnectionRole.EntityLogicalName, _connectionRole2Id), AssociatedObjectTypeCode = Contact.EntityLogicalName }; _serviceProxy.Create(newContactConnectionRole2TypeCode); Console.WriteLine( "Created a related Connection Role 2 Object Type Code record for Contact." ); //<snippetAssociateReciprocalConnectionRoles> // Associate the connection roles AssociateRequest associateConnectionRoles = new AssociateRequest { Target = new EntityReference(ConnectionRole.EntityLogicalName, _connectionRole1Id), RelatedEntities = new EntityReferenceCollection() { new EntityReference(ConnectionRole.EntityLogicalName, _connectionRole2Id) }, // The name of the relationship connection role association // relationship in MS CRM Relationship = new Relationship() { PrimaryEntityRole = EntityRole.Referencing, // Referencing or Referenced based on N:1 or 1:N reflexive relationship. SchemaName = "connectionroleassociation_association" } }; _serviceProxy.Execute(associateConnectionRoles); Console.WriteLine("Associated the connection roles."); //</snippetAssociateReciprocalConnectionRoles> DeleteRequiredRecords(promptForDelete); } } // Catch any service fault exceptions that Microsoft Dynamics CRM throws. catch (FaultException <Microsoft.Xrm.Sdk.OrganizationServiceFault> ) { // You can handle an exception here or pass it back to the calling method. throw; } }
/// <summary> /// This sample creates a role that is not linked to any entity type. All /// connection roles that apply to all are found and shown. Then the role is /// linked to the account entity and it is demonstrated that the role only works /// for accounts at this point, not for all. Subsequently the link to the account /// entity is removed and it is shown that the role is now applicable to all /// entities again. /// </summary> /// <param name="serverConfig">Contains server connection information.</param> /// <param name="promptforDelete">When True, the user will be prompted to delete all /// created entities.</param> public void Run(ServerConnection.Configuration serverConfig, bool promptForDelete) { try { //<snippetQueryConnectionRolesByEntityTypeCode1> // Connect to the Organization service. // The using statement assures that the service proxy will be properly disposed. using (_serviceProxy = new OrganizationServiceProxy(serverConfig.OrganizationUri, serverConfig.HomeRealmUri, serverConfig.Credentials, serverConfig.DeviceCredentials)) { // This statement is required to enable early-bound type support. _serviceProxy.EnableProxyTypes(); // Define some anonymous types to define the range // of possible connection property values. var Categories = new { Business = 1, Family = 2, Social = 3, Sales = 4, Other = 5 }; // Create a Connection Role. ConnectionRole setupConnectionRole = new ConnectionRole { Name = "Example Connection Role", Category = new OptionSetValue(Categories.Business), }; _connectionRoleId = _serviceProxy.Create(setupConnectionRole); setupConnectionRole.Id = _connectionRoleId; Console.WriteLine("Created {0}.", setupConnectionRole.Name); // Query for all Connection Roles. QueryExpression allQuery = new QueryExpression { EntityName = ConnectionRole.EntityLogicalName, ColumnSet = new ColumnSet("connectionroleid", "name"), Distinct = true, LinkEntities = { new LinkEntity { LinkToEntityName = ConnectionRoleObjectTypeCode.EntityLogicalName, LinkToAttributeName = "connectionroleid", LinkFromEntityName = ConnectionRole.EntityLogicalName, LinkFromAttributeName = "connectionroleid", LinkCriteria = new FilterExpression { FilterOperator = LogicalOperator.And, // Set a condition to only get connection roles // related to all entities (object type code = 0). Conditions = { new ConditionExpression { AttributeName = "associatedobjecttypecode", Operator = ConditionOperator.Equal, Values = { 0 } } } } } } }; EntityCollection results = _serviceProxy.RetrieveMultiple(allQuery); // Here you could perform operations on all of // the connectionroles found by the query. Console.WriteLine("Retrieved {0} unassociated connectionrole instance(s).", results.Entities.Count); // Query to find roles which apply only to accounts. QueryExpression accountQuery = new QueryExpression { EntityName = ConnectionRole.EntityLogicalName, ColumnSet = new ColumnSet("connectionroleid", "name"), Distinct = true, LinkEntities = { new LinkEntity { LinkToEntityName = ConnectionRoleObjectTypeCode.EntityLogicalName, LinkToAttributeName = "connectionroleid", LinkFromEntityName = ConnectionRole.EntityLogicalName, LinkFromAttributeName = "connectionroleid", LinkCriteria = new FilterExpression { FilterOperator = LogicalOperator.And, // Set a condition to only get connection roles // related to accounts (object type code = 1). Conditions = { new ConditionExpression { AttributeName = "associatedobjecttypecode", Operator = ConditionOperator.In, Values = { Account.EntityLogicalName } } } } } } }; results = _serviceProxy.RetrieveMultiple(accountQuery); Console.WriteLine("Retrieved {0} account-only connectionrole instance(s).", results.Entities.Count); // Create a related Connection Role Object Type Code record for // Account. ConnectionRoleObjectTypeCode setupAccountConnectionRoleTypeCode = new ConnectionRoleObjectTypeCode { ConnectionRoleId = new EntityReference( ConnectionRole.EntityLogicalName, _connectionRoleId), AssociatedObjectTypeCode = Account.EntityLogicalName }; setupAccountConnectionRoleTypeCode.Id = _serviceProxy.Create(setupAccountConnectionRoleTypeCode); Console.Write("Created a related Connection Role Object Type Code"); Console.Write(" record for Account."); // Run the query to find unassociated connectionroles again. results = _serviceProxy.RetrieveMultiple(allQuery); Console.WriteLine(@"Retrieved {0} unassociated connectionrole instance(s).", results.Entities.Count); // Run the account-only query again. results = _serviceProxy.RetrieveMultiple(accountQuery); Console.WriteLine("Retrieved {0} account-only connectionrole instance(s).", results.Entities.Count); // Remove the link from account entity. _serviceProxy.Delete(ConnectionRoleObjectTypeCode.EntityLogicalName, setupAccountConnectionRoleTypeCode.Id); Console.WriteLine("Removed link from connectionrole to account entity."); // Run the query to find unassociated connectionroles again. results = _serviceProxy.RetrieveMultiple(allQuery); Console.WriteLine("Retrieved {0} unassociated connectionrole instance(s).", results.Entities.Count); // Run the account-only query again. results = _serviceProxy.RetrieveMultiple(accountQuery); Console.WriteLine("Retrieved {0} account-only connectionrole instance(s).", results.Entities.Count); DeleteRequiredRecords(promptForDelete); } //</snippetQueryConnectionRolesByEntityTypeCode1> } // Catch any service fault exceptions that Microsoft Dynamics CRM throws. catch (FaultException <Microsoft.Xrm.Sdk.OrganizationServiceFault> ) { // You can handle an exception here or pass it back to the calling method. throw; } }
[STAThread] // Added to support UX static void Main(string[] args) { CrmServiceClient service = null; try { service = SampleHelpers.Connect("Connect"); if (service.IsReady) { #region Sample Code #region Set up SetUpSample(service); #endregion Set up #region Demonstrate // Create the Connection Role 1 ConnectionRole newConnectionRole1 = new ConnectionRole { Name = "Example Connection Role 1", Category = new OptionSetValue((int)connectionrole_category.Business), }; _connectionRole1Id = service.Create(newConnectionRole1); Console.WriteLine("Created {0}.", newConnectionRole1.Name); // Create a related Connection Role Object Type Code record for Account ConnectionRoleObjectTypeCode newAccountConnectionRole1TypeCode = new ConnectionRoleObjectTypeCode { ConnectionRoleId = new EntityReference( ConnectionRole.EntityLogicalName, _connectionRole1Id), AssociatedObjectTypeCode = Account.EntityLogicalName }; service.Create(newAccountConnectionRole1TypeCode); Console.WriteLine( "Created a related Connection Role 1 Object Type Code record for Account." ); // Create a related Connection Role Object Type Code record for Contact ConnectionRoleObjectTypeCode newContactConnectionRole1TypeCode = new ConnectionRoleObjectTypeCode { ConnectionRoleId = new EntityReference( ConnectionRole.EntityLogicalName, _connectionRole1Id), AssociatedObjectTypeCode = Contact.EntityLogicalName }; service.Create(newContactConnectionRole1TypeCode); Console.WriteLine( "Created a related Connection Role 1 Object Type Code record for Contact." ); // Create the Connection Role 2 ConnectionRole newConnectionRole2 = new ConnectionRole { Name = "Example Connection Role 2", Category = new OptionSetValue((int)connectionrole_category.Business), }; _connectionRole2Id = service.Create(newConnectionRole2); Console.WriteLine("Created {0}.", newConnectionRole2.Name); // Create a related Connection Role 2 Object Type Code record for Account ConnectionRoleObjectTypeCode newAccountConnectionRole2TypeCode = new ConnectionRoleObjectTypeCode { ConnectionRoleId = new EntityReference( ConnectionRole.EntityLogicalName, _connectionRole2Id), AssociatedObjectTypeCode = Account.EntityLogicalName }; service.Create(newAccountConnectionRole2TypeCode); Console.WriteLine( "Created a related Connection Role 2 Object Type Code record for Account." ); // Create a related Connection Role 2 Object Type Code record for Contact ConnectionRoleObjectTypeCode newContactConnectionRole2TypeCode = new ConnectionRoleObjectTypeCode { ConnectionRoleId = new EntityReference( ConnectionRole.EntityLogicalName, _connectionRole2Id), AssociatedObjectTypeCode = Contact.EntityLogicalName }; service.Create(newContactConnectionRole2TypeCode); Console.WriteLine( "Created a related Connection Role 2 Object Type Code record for Contact." ); // Associate the connection roles AssociateRequest associateConnectionRoles = new AssociateRequest { Target = new EntityReference(ConnectionRole.EntityLogicalName, _connectionRole1Id), RelatedEntities = new EntityReferenceCollection() { new EntityReference(ConnectionRole.EntityLogicalName, _connectionRole2Id) }, // The name of the relationship connection role association // relationship in MS CRM Relationship = new Relationship() { PrimaryEntityRole = EntityRole.Referencing, // Referencing or Referenced based on N:1 or 1:N reflexive relationship. SchemaName = "connectionroleassociation_association" } }; service.Execute(associateConnectionRoles); Console.WriteLine("Associated the connection roles."); #region Clean up CleanUpSample(service); #endregion Clean up } #endregion Demonstrate #endregion Sample Code else { const string UNABLE_TO_LOGIN_ERROR = "Unable to Login to Microsoft Dataverse"; if (service.LastCrmError.Equals(UNABLE_TO_LOGIN_ERROR)) { Console.WriteLine("Check the connection string values in cds/App.config."); throw new Exception(service.LastCrmError); } else { throw service.LastCrmException; } } } catch (Exception ex) { SampleHelpers.HandleException(ex); } finally { if (service != null) { service.Dispose(); } Console.WriteLine("Press <Enter> to exit."); Console.ReadLine(); } }
/// <summary> /// Create and configure the organization service proxy. /// Create the connection role instances. /// Associate the connection roles. /// Optionally delete any entity records that were created for this sample. /// </summary> /// <param name="serverConfig">Contains server connection information.</param> /// <param name="promptforDelete">When True, the user will be prompted to delete all /// created entities.</param> public void Run(ServerConnection.Configuration serverConfig, bool promptForDelete) { try { // Connect to the Organization service. // The using statement assures that the service proxy will be properly disposed. using (_serviceProxy = new OrganizationServiceProxy(serverConfig.OrganizationUri, serverConfig.HomeRealmUri,serverConfig.Credentials, serverConfig.DeviceCredentials)) { // This statement is required to enable early-bound type support. _serviceProxy.EnableProxyTypes(); // Create the Connection Role 1 ConnectionRole newConnectionRole1 = new ConnectionRole { Name = "Example Connection Role 1", Category = new OptionSetValue((int)connectionrole_category.Business), }; _connectionRole1Id = _serviceProxy.Create(newConnectionRole1); Console.WriteLine("Created {0}.", newConnectionRole1.Name); // Create a related Connection Role Object Type Code record for Account ConnectionRoleObjectTypeCode newAccountConnectionRole1TypeCode = new ConnectionRoleObjectTypeCode { ConnectionRoleId = new EntityReference( ConnectionRole.EntityLogicalName, _connectionRole1Id), AssociatedObjectTypeCode = Account.EntityLogicalName }; _serviceProxy.Create(newAccountConnectionRole1TypeCode); Console.WriteLine( "Created a related Connection Role 1 Object Type Code record for Account." ); // Create a related Connection Role Object Type Code record for Contact ConnectionRoleObjectTypeCode newContactConnectionRole1TypeCode = new ConnectionRoleObjectTypeCode { ConnectionRoleId = new EntityReference( ConnectionRole.EntityLogicalName, _connectionRole1Id), AssociatedObjectTypeCode = Contact.EntityLogicalName }; _serviceProxy.Create(newContactConnectionRole1TypeCode); Console.WriteLine( "Created a related Connection Role 1 Object Type Code record for Contact." ); // Create the Connection Role 2 ConnectionRole newConnectionRole2 = new ConnectionRole { Name = "Example Connection Role 2", Category = new OptionSetValue((int)connectionrole_category.Business), }; _connectionRole2Id = _serviceProxy.Create(newConnectionRole2); Console.WriteLine("Created {0}.", newConnectionRole2.Name); // Create a related Connection Role 2 Object Type Code record for Account ConnectionRoleObjectTypeCode newAccountConnectionRole2TypeCode = new ConnectionRoleObjectTypeCode { ConnectionRoleId = new EntityReference( ConnectionRole.EntityLogicalName, _connectionRole2Id), AssociatedObjectTypeCode = Account.EntityLogicalName }; _serviceProxy.Create(newAccountConnectionRole2TypeCode); Console.WriteLine( "Created a related Connection Role 2 Object Type Code record for Account." ); // Create a related Connection Role 2 Object Type Code record for Contact ConnectionRoleObjectTypeCode newContactConnectionRole2TypeCode = new ConnectionRoleObjectTypeCode { ConnectionRoleId = new EntityReference( ConnectionRole.EntityLogicalName, _connectionRole2Id), AssociatedObjectTypeCode = Contact.EntityLogicalName }; _serviceProxy.Create(newContactConnectionRole2TypeCode); Console.WriteLine( "Created a related Connection Role 2 Object Type Code record for Contact." ); //<snippetAssociateReciprocalConnectionRoles> // Associate the connection roles AssociateRequest associateConnectionRoles = new AssociateRequest { Target = new EntityReference(ConnectionRole.EntityLogicalName, _connectionRole1Id), RelatedEntities = new EntityReferenceCollection() { new EntityReference(ConnectionRole.EntityLogicalName, _connectionRole2Id) }, // The name of the relationship connection role association // relationship in MS CRM Relationship = new Relationship() { PrimaryEntityRole = EntityRole.Referencing, // Referencing or Referenced based on N:1 or 1:N reflexive relationship. SchemaName = "connectionroleassociation_association" } }; _serviceProxy.Execute(associateConnectionRoles); Console.WriteLine("Associated the connection roles."); //</snippetAssociateReciprocalConnectionRoles> DeleteRequiredRecords(promptForDelete); } } // Catch any service fault exceptions that Microsoft Dynamics CRM throws. catch (FaultException<Microsoft.Xrm.Sdk.OrganizationServiceFault>) { // You can handle an exception here or pass it back to the calling method. throw; } }
/// <summary> /// This method creates any entity records that this sample requires. /// Create a new connectionrole instance. /// </summary> public void CreateRequiredRecords() { // Define some anonymous types to define the range // of possible connection property values. var Categories = new { Business = 1, Family = 2, Social = 3, Sales = 4, Other = 5 }; // Create a Connection Role ConnectionRole setupConnectionRole = new ConnectionRole { Name = "Example Connection Role", Description = "This is an example one sided connection role.", Category = new OptionSetValue(Categories.Business), }; _connectionRoleId = _serviceProxy.Create(setupConnectionRole); Console.WriteLine("Created {0}.", setupConnectionRole.Name); // Create a related Connection Role Object Type Code record for Account ConnectionRoleObjectTypeCode newAccountConnectionRoleTypeCode = new ConnectionRoleObjectTypeCode { ConnectionRoleId = new EntityReference( ConnectionRole.EntityLogicalName, _connectionRoleId), AssociatedObjectTypeCode = Account.EntityLogicalName }; _serviceProxy.Create(newAccountConnectionRoleTypeCode); Console.WriteLine( "Created a related Connection Role Object Type Code record for Account." ); // Create a related Connection Role Object Type Code record for Contact ConnectionRoleObjectTypeCode newContactConnectionRoleTypeCode = new ConnectionRoleObjectTypeCode { ConnectionRoleId = new EntityReference( ConnectionRole.EntityLogicalName, _connectionRoleId), AssociatedObjectTypeCode = Contact.EntityLogicalName }; _serviceProxy.Create(newContactConnectionRoleTypeCode); Console.WriteLine( "Created a related Connection Role Object Type Code record for Contact." ); //Create a few account records for use in the connections. Account setupAccount1 = new Account { Name = "Example Account 1" }; _account1Id = _serviceProxy.Create(setupAccount1); Console.WriteLine("Created {0}.", setupAccount1.Name); Account setupAccount2 = new Account { Name = "Example Account 2" }; _account2Id = _serviceProxy.Create(setupAccount2); Console.WriteLine("Created {0}.", setupAccount2.Name); //Creates a contact used in the connection. Contact setupContact = new Contact { LastName = "Example Contact" }; _contactId = _serviceProxy.Create(setupContact); Console.WriteLine("Created {0}.", setupContact.LastName); // Create a new connection between Account 1 and the contact record. Connection newConnection1 = new Connection { Record1Id = new EntityReference(Account.EntityLogicalName, _account1Id), Record1RoleId = new EntityReference(ConnectionRole.EntityLogicalName, _connectionRoleId), Record2Id = new EntityReference(Contact.EntityLogicalName, _contactId) }; _connection1Id = _serviceProxy.Create(newConnection1); Console.WriteLine( "Created a connection between the account 1 and the contact."); // Create a new connection between the contact and Account 2 record Connection newConnection2 = new Connection { Record1Id = new EntityReference(Contact.EntityLogicalName, _contactId), Record1RoleId = new EntityReference(ConnectionRole.EntityLogicalName, _connectionRoleId), Record2Id = new EntityReference(Account.EntityLogicalName, _account2Id) }; _connection2Id = _serviceProxy.Create(newConnection2); Console.WriteLine( "Created a connection between the contact and the account 2."); return; }
/// <summary> /// This method creates any entity records that this sample requires. /// Create a primary connection role instance. /// Create a reciprocal connection role instance. /// Associate the connection roles. /// </summary> public void CreateRequiredRecords() { // Define some anonymous types to define the range // of possible connection property values. var Categories = new { Business = 1, Family = 2, Social = 3, Sales = 4, Other = 5 }; // Create the Connection Roles. // Create the primary connection Role instance. ConnectionRole setupPrimaryConnectionRole = new ConnectionRole { Name = "Example Primary Connection Role", Category = new OptionSetValue(Categories.Business), }; _primaryConnectionRoleId = _serviceProxy.Create(setupPrimaryConnectionRole); Console.WriteLine("Created {0}.", setupPrimaryConnectionRole.Name); // Create a related Connection Role Object Type Code record for Account // on the primary role. ConnectionRoleObjectTypeCode accountPrimaryConnectionRoleTypeCode = new ConnectionRoleObjectTypeCode { ConnectionRoleId = new EntityReference( ConnectionRole.EntityLogicalName, _primaryConnectionRoleId), AssociatedObjectTypeCode = Account.EntityLogicalName }; _serviceProxy.Create(accountPrimaryConnectionRoleTypeCode); Console.WriteLine(@"Created a related Connection Role Object Type Code record for Account on the primary role."); // Create another Connection Role. ConnectionRole setupReciprocalConnectionRole = new ConnectionRole { Name = "Example Reciprocal Connection Role", Category = new OptionSetValue(Categories.Business), }; _reciprocalConnectionRoleId = _serviceProxy.Create(setupReciprocalConnectionRole); Console.WriteLine("Created {0}.", setupReciprocalConnectionRole.Name); // Create a related Connection Role Object Type Code record for Account // on the related role. ConnectionRoleObjectTypeCode accountReciprocalConnectionRoleTypeCode = new ConnectionRoleObjectTypeCode { ConnectionRoleId = new EntityReference( ConnectionRole.EntityLogicalName, _reciprocalConnectionRoleId), AssociatedObjectTypeCode = Account.EntityLogicalName }; _serviceProxy.Create(accountReciprocalConnectionRoleTypeCode); Console.WriteLine(@"Created a related Connection Role Object Type Code record for Account on the related role."); // Associate the connection roles. AssociateRequest associateConnectionRoles = new AssociateRequest { Target = new EntityReference(ConnectionRole.EntityLogicalName, _primaryConnectionRoleId), RelatedEntities = new EntityReferenceCollection() { new EntityReference(ConnectionRole.EntityLogicalName, _reciprocalConnectionRoleId) }, // The name of the relationship connection role association // relationship in MS CRM Relationship = new Relationship() { PrimaryEntityRole = EntityRole.Referencing, // Referencing or Referenced based on N:1 or 1:N reflexive relationship. SchemaName = "connectionroleassociation_association" } }; _serviceProxy.Execute(associateConnectionRoles); return; }
[STAThread] // Required to support the interactive login experience static void Main(string[] args) { CrmServiceClient service = null; try { service = SampleHelpers.Connect("Connect"); if (service.IsReady) { // Create any entity records that the demonstration code requires SetUpSample(service); #region Demonstrate // // Create a Connection Role for account and contact ConnectionRole newConnectionRole = new ConnectionRole { Name = "Example Connection Role", Category = new OptionSetValue((int)connectionrole_category.Business) }; _connectionRoleId = _serviceProxy.Create(newConnectionRole); Console.WriteLine("Created {0}.", newConnectionRole.Name); // Create a related Connection Role Object Type Code record for Account ConnectionRoleObjectTypeCode newAccountConnectionRoleTypeCode = new ConnectionRoleObjectTypeCode { ConnectionRoleId = new EntityReference( ConnectionRole.EntityLogicalName, _connectionRoleId), AssociatedObjectTypeCode = Account.EntityLogicalName }; _serviceProxy.Create(newAccountConnectionRoleTypeCode); Console.WriteLine( "Created a related Connection Role Object Type Code record for Account." ); // Create a related Connection Role Object Type Code record for Contact ConnectionRoleObjectTypeCode newContactConnectionRoleTypeCode = new ConnectionRoleObjectTypeCode { ConnectionRoleId = new EntityReference( ConnectionRole.EntityLogicalName, _connectionRoleId), AssociatedObjectTypeCode = Contact.EntityLogicalName }; _serviceProxy.Create(newContactConnectionRoleTypeCode); Console.WriteLine( "Created a related Connection Role Object Type Code record for Contact." ); // Associate the connection role with itself. AssociateRequest associateConnectionRoles = new AssociateRequest { Target = new EntityReference(ConnectionRole.EntityLogicalName, _connectionRoleId), RelatedEntities = new EntityReferenceCollection() { new EntityReference(ConnectionRole.EntityLogicalName, _connectionRoleId) }, // The name of the relationship connection role association // relationship in MS CRM. Relationship = new Relationship() { PrimaryEntityRole = EntityRole.Referencing, // Referencing or Referenced based on N:1 or 1:N reflexive relationship. SchemaName = "connectionroleassociation_association" } }; _serviceProxy.Execute(associateConnectionRoles); Console.WriteLine("Associated the connection role with itself."); // Create an Account Account setupAccount = new Account { Name = "Example Account" }; _accountId = _serviceProxy.Create(setupAccount); Console.WriteLine("Created {0}.", setupAccount.Name); // Create a Contact Contact setupContact = new Contact { LastName = "Example Contact" }; _contactId = _serviceProxy.Create(setupContact); Console.WriteLine("Created {0}.", setupContact.LastName); return; #endregion Demonstrate } else { const string UNABLE_TO_LOGIN_ERROR = "Unable to Login to Common Data Service"; if (service.LastCrmError.Equals(UNABLE_TO_LOGIN_ERROR)) { Console.WriteLine("Check the connection string values in cds/App.config."); throw new Exception(service.LastCrmError); } else { throw service.LastCrmException; } } } catch (Exception ex) { SampleHelpers.HandleException(ex); } finally { if (service != null) { service.Dispose(); } Console.WriteLine("Press <Enter> to exit."); Console.ReadLine(); } }
/// <summary> /// Create and configure the organization service proxy. /// Create a new connectionrole instance and set the object type. /// Optionally delete any entity records that were created for this sample. /// </summary> /// <param name="serverConfig">Contains server connection information.</param> /// <param name="promptforDelete">When True, the user will be prompted to delete all /// created entities.</param> public void Run(ServerConnection.Configuration serverConfig, bool promptForDelete) { try { // Connect to the Organization service. // The using statement assures that the service proxy will be properly disposed. using (_serviceProxy = ServerConnection.GetOrganizationProxy(serverConfig)) { // This statement is required to enable early-bound type support. _serviceProxy.EnableProxyTypes(); //<snippetCreateConnectionRole1> // Define some anonymous types to define the range // of possible connection property values. var Categories = new { Business = 1, Family = 2, Social = 3, Sales = 4, Other = 5 }; // Create a Connection Role for account and contact ConnectionRole newConnectionRole = new ConnectionRole { Name = "Example Connection Role", Category = new OptionSetValue(Categories.Business) }; _connectionRoleId = _serviceProxy.Create(newConnectionRole); Console.WriteLine("Created {0}.", newConnectionRole.Name); // Create a related Connection Role Object Type Code record for Account ConnectionRoleObjectTypeCode newAccountConnectionRoleTypeCode = new ConnectionRoleObjectTypeCode { ConnectionRoleId = new EntityReference( ConnectionRole.EntityLogicalName, _connectionRoleId), AssociatedObjectTypeCode = Account.EntityLogicalName }; _serviceProxy.Create(newAccountConnectionRoleTypeCode); Console.WriteLine( "Created a related Connection Role Object Type Code record for Account."); // Create a related Connection Role Object Type Code record for Contact ConnectionRoleObjectTypeCode newContactConnectionRoleTypeCode = new ConnectionRoleObjectTypeCode { ConnectionRoleId = new EntityReference( ConnectionRole.EntityLogicalName, _connectionRoleId), AssociatedObjectTypeCode = Contact.EntityLogicalName }; _serviceProxy.Create(newContactConnectionRoleTypeCode); Console.WriteLine( "Created a related Connection Role Object Type Code record for Contact."); //</snippetCreateConnectionRole1> DeleteRequiredRecords(promptForDelete); } } // Catch any service fault exceptions that Microsoft Dynamics CRM throws. catch (FaultException <Microsoft.Xrm.Sdk.OrganizationServiceFault> ) { // You can handle an exception here or pass it back to the calling method. throw; } }
/// <summary> /// This sample creates a role that is not linked to any entity type. All /// connection roles that apply to all are found and shown. Then the role is /// linked to the account entity and it is demonstrated that the role only works /// for accounts at this point, not for all. Subsequently the link to the account /// entity is removed and it is shown that the role is now applicable to all /// entities again. /// </summary> /// <param name="serverConfig">Contains server connection information.</param> /// <param name="promptforDelete">When True, the user will be prompted to delete all /// created entities.</param> public void Run(ServerConnection.Configuration serverConfig, bool promptForDelete) { try { //<snippetQueryConnectionRolesByEntityTypeCode1> // Connect to the Organization service. // The using statement assures that the service proxy will be properly disposed. using (_serviceProxy = new OrganizationServiceProxy(serverConfig.OrganizationUri, serverConfig.HomeRealmUri,serverConfig.Credentials, serverConfig.DeviceCredentials)) { // This statement is required to enable early-bound type support. _serviceProxy.EnableProxyTypes(); // Define some anonymous types to define the range // of possible connection property values. var Categories = new { Business = 1, Family = 2, Social = 3, Sales = 4, Other = 5 }; // Create a Connection Role. ConnectionRole setupConnectionRole = new ConnectionRole { Name = "Example Connection Role", Category = new OptionSetValue(Categories.Business), }; _connectionRoleId = _serviceProxy.Create(setupConnectionRole); setupConnectionRole.Id = _connectionRoleId; Console.WriteLine("Created {0}.", setupConnectionRole.Name); // Query for all Connection Roles. QueryExpression allQuery = new QueryExpression { EntityName = ConnectionRole.EntityLogicalName, ColumnSet = new ColumnSet("connectionroleid", "name"), Distinct = true, LinkEntities = { new LinkEntity { LinkToEntityName = ConnectionRoleObjectTypeCode.EntityLogicalName, LinkToAttributeName = "connectionroleid", LinkFromEntityName = ConnectionRole.EntityLogicalName, LinkFromAttributeName = "connectionroleid", LinkCriteria = new FilterExpression { FilterOperator = LogicalOperator.And, // Set a condition to only get connection roles // related to all entities (object type code = 0). Conditions = { new ConditionExpression { AttributeName = "associatedobjecttypecode", Operator = ConditionOperator.Equal, Values = { 0 } } } } } } }; EntityCollection results = _serviceProxy.RetrieveMultiple(allQuery); // Here you could perform operations on all of // the connectionroles found by the query. Console.WriteLine("Retrieved {0} unassociated connectionrole instance(s).", results.Entities.Count); // Query to find roles which apply only to accounts. QueryExpression accountQuery = new QueryExpression { EntityName = ConnectionRole.EntityLogicalName, ColumnSet = new ColumnSet("connectionroleid", "name"), Distinct = true, LinkEntities = { new LinkEntity { LinkToEntityName = ConnectionRoleObjectTypeCode.EntityLogicalName, LinkToAttributeName = "connectionroleid", LinkFromEntityName = ConnectionRole.EntityLogicalName, LinkFromAttributeName = "connectionroleid", LinkCriteria = new FilterExpression { FilterOperator = LogicalOperator.And, // Set a condition to only get connection roles // related to accounts (object type code = 1). Conditions = { new ConditionExpression { AttributeName = "associatedobjecttypecode", Operator = ConditionOperator.In, Values = { Account.EntityLogicalName } } } } } } }; results = _serviceProxy.RetrieveMultiple(accountQuery); Console.WriteLine("Retrieved {0} account-only connectionrole instance(s).", results.Entities.Count); // Create a related Connection Role Object Type Code record for // Account. ConnectionRoleObjectTypeCode setupAccountConnectionRoleTypeCode = new ConnectionRoleObjectTypeCode { ConnectionRoleId = new EntityReference( ConnectionRole.EntityLogicalName, _connectionRoleId), AssociatedObjectTypeCode = Account.EntityLogicalName }; setupAccountConnectionRoleTypeCode.Id = _serviceProxy.Create(setupAccountConnectionRoleTypeCode); Console.Write("Created a related Connection Role Object Type Code"); Console.Write(" record for Account."); // Run the query to find unassociated connectionroles again. results = _serviceProxy.RetrieveMultiple(allQuery); Console.WriteLine(@"Retrieved {0} unassociated connectionrole instance(s).", results.Entities.Count); // Run the account-only query again. results = _serviceProxy.RetrieveMultiple(accountQuery); Console.WriteLine("Retrieved {0} account-only connectionrole instance(s).", results.Entities.Count); // Remove the link from account entity. _serviceProxy.Delete(ConnectionRoleObjectTypeCode.EntityLogicalName, setupAccountConnectionRoleTypeCode.Id); Console.WriteLine("Removed link from connectionrole to account entity."); // Run the query to find unassociated connectionroles again. results = _serviceProxy.RetrieveMultiple(allQuery); Console.WriteLine("Retrieved {0} unassociated connectionrole instance(s).", results.Entities.Count); // Run the account-only query again. results = _serviceProxy.RetrieveMultiple(accountQuery); Console.WriteLine("Retrieved {0} account-only connectionrole instance(s).", results.Entities.Count); DeleteRequiredRecords(promptForDelete); } //</snippetQueryConnectionRolesByEntityTypeCode1> } // Catch any service fault exceptions that Microsoft Dynamics CRM throws. catch (FaultException<Microsoft.Xrm.Sdk.OrganizationServiceFault>) { // You can handle an exception here or pass it back to the calling method. throw; } }
[STAThread] // Added to support UX static void Main(string[] args) { CrmServiceClient service = null; try { service = SampleHelpers.Connect("Connect"); if (service.IsReady) { #region Sample Code #region Set up SetUpSample(service); #endregion Set up #region Demonstrate // Define some anonymous types to define the range // of possible connection property values. var Categories = new { Business = 1, Family = 2, Social = 3, Sales = 4, Other = 5 }; // Create a Connection Role for account and contact var newConnectionRole = new ConnectionRole { Name = "Example Connection Role", Category = new OptionSetValue(Categories.Business) }; _connectionRoleId = service.Create(newConnectionRole); Console.WriteLine("Created {0}.", newConnectionRole.Name); // Create a related Connection Role Object Type Code record for Account var newAccountConnectionRoleTypeCode = new ConnectionRoleObjectTypeCode { ConnectionRoleId = new EntityReference( ConnectionRole.EntityLogicalName, _connectionRoleId), AssociatedObjectTypeCode = Account.EntityLogicalName }; service.Create(newAccountConnectionRoleTypeCode); Console.WriteLine( "Created a related Connection Role Object Type Code record for Account."); // Create a related Connection Role Object Type Code record for Contact var newContactConnectionRoleTypeCode = new ConnectionRoleObjectTypeCode { ConnectionRoleId = new EntityReference( ConnectionRole.EntityLogicalName, _connectionRoleId), AssociatedObjectTypeCode = Contact.EntityLogicalName }; service.Create(newContactConnectionRoleTypeCode); Console.WriteLine( "Created a related Connection Role Object Type Code record for Contact."); #region Clean up CleanUpSample(service); #endregion Clean up #endregion Demonstrate #endregion Sample Code } else { const string UNABLE_TO_LOGIN_ERROR = "Unable to Login to Common Data Service"; if (service.LastCrmError.Equals(UNABLE_TO_LOGIN_ERROR)) { Console.WriteLine("Check the connection string values in cds/App.config."); throw new Exception(service.LastCrmError); } else { throw service.LastCrmException; } } } catch (Exception ex) { SampleHelpers.HandleException(ex); } finally { if (service != null) { service.Dispose(); } Console.WriteLine("Press <Enter> to exit."); Console.ReadLine(); } }