ClientUI.ViewModel.ObservableConnection.GetOppositeRole C# (CSharp) Méthode

GetOppositeRole() public static méthode

public static GetOppositeRole ( Xrm.Sdk.EntityReference role, Xrm.Sdk.EntityReference record ) : Xrm.Sdk.EntityReference
role Xrm.Sdk.EntityReference
record Xrm.Sdk.EntityReference
Résultat Xrm.Sdk.EntityReference
        public static EntityReference GetOppositeRole(EntityReference role, EntityReference record)
        {
            EntityReference oppositeRole = null;
            int? etc = GetEntityTypeCodeFromName(record.LogicalName);

            // Add the opposite connection role
            string getOppositeRole = String.Format(@"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='true' count='1'>
                          <entity name='connectionrole'>
                            <attribute name='category' />
                            <attribute name='name' />
                            <attribute name='connectionroleid' />
                            <attribute name='statecode' />
                            <filter type='and'>
                              <condition attribute='statecode' operator='eq' value='0' />
                            </filter>
                            <link-entity name='connectionroleassociation' from='connectionroleid' to='connectionroleid' intersect='true'>
                                  <link-entity name='connectionrole' from='connectionroleid' to='associatedconnectionroleid' alias='ad'>
                                    <filter type='and'>
                                      <condition attribute='connectionroleid' operator='eq' value='{0}' />
                                    </filter>
                                  </link-entity>
                                 <link-entity name='connectionroleobjecttypecode' from='connectionroleid' to='connectionroleid' intersect='true' >
                                    <filter type='or' >
                                        <condition attribute='associatedobjecttypecode' operator='eq' value='{1}' />
                                        <condition attribute='associatedobjecttypecode' operator='eq' value='0' /> <!-- All types-->
                                    </filter>
                                </link-entity>
                            </link-entity>
                          </entity>
                        </fetch>", role.Id.ToString(), etc);

            EntityCollection results = (EntityCollection)OrganizationServiceProxy.RetrieveMultiple(getOppositeRole);

            if (results.Entities.Count > 0)
            {
                oppositeRole = results.Entities[0].ToEntityReference();
            }
            return oppositeRole;
        }

Usage Example

Exemple #1
0
        private void connection_PropertyChanged(object sender, Xrm.ComponentModel.PropertyChangedEventArgs e)
        {
            Connection connectionToUpdate = new Connection();
            Connection updated            = (Connection)sender;

            connectionToUpdate.ConnectionID = new Guid(updated.Id);
            bool updateRequired = false;

            switch (e.PropertyName)
            {
            case "record2roleid":
                // Check if the record1id is loaded - if not load it now so we can work out the opposite role
                if (updated.Record1Id == null)
                {
                    Connection connection = (Connection)OrganizationServiceProxy.Retrieve(Connection.LogicalName, updated.ConnectionID.Value, new string[] { "record1id" });
                    updated.Record1Id = connection.Record1Id;
                }
                connectionToUpdate.Record2RoleId = updated.Record2RoleId;
                connectionToUpdate.Record1RoleId = ObservableConnection.GetOppositeRole(updated.Record2RoleId, updated.Record1Id);

                updateRequired = true;
                break;

            case "description":
                connectionToUpdate.Description = updated.Description;
                updateRequired = true;
                break;

            case "effectivestart":
                connectionToUpdate.EffectiveStart = updated.EffectiveStart;
                updateRequired = true;
                break;

            case "effectiveend":
                connectionToUpdate.EffectiveEnd = updated.EffectiveEnd;
                updateRequired = true;
                break;
            }


            // Auto save
            if (updateRequired)
            {
                OrganizationServiceProxy.BeginUpdate(connectionToUpdate, delegate(object state)
                {
                    try
                    {
                        OrganizationServiceProxy.EndUpdate(state);
                        ErrorMessage.SetValue(null);
                    }
                    catch (Exception ex)
                    {
                        ErrorMessage.SetValue(ex.Message);
                    }
                });
            }
        }