private void RemoveUserFromQueue(Entity user, Guid queueId)
        {
            var queueReference = new EntityReference("queue", queueId);
            var queueRefs      = new EntityReferenceCollection(new List <EntityReference>()
            {
                queueReference
            });

            _client.Disassociate(
                "systemuser",
                user.Id,
                new Relationship("queuemembership_association"),
                queueRefs);
        }
        /// <summary>
        /// Deletes the custom entity record that was created for this sample.
        /// <param name="prompt">Indicates whether to prompt the user
        /// to delete the entity created in this sample.</param>
        /// </summary>
        public static void DeleteRequiredRecords(CrmServiceClient service, bool prompt)
        {
            bool deleteRecords = true;

            if (prompt)
            {
                Console.WriteLine("\nDo you want these entity records deleted? (y/n)");
                String answer = Console.ReadLine();

                deleteRecords = (answer.StartsWith("y") || answer.StartsWith("Y"));
            }

            if (deleteRecords)
            {
                service.Disassociate("systemuser",
                                     _userId,
                                     new Relationship("systemuserroles_association"),
                                     new EntityReferenceCollection()
                {
                    new EntityReference("role", _roleId)
                });
                Console.WriteLine("Entity records have been deleted.");
            }
        }
        [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
                    // Retrieve a user.
                    SystemUser user = service.Retrieve(SystemUser.EntityLogicalName,
                                                       _userId, new ColumnSet(new String[] { "systemuserid", "firstname", "lastname" })).ToEntity <SystemUser>();

                    if (user != null)
                    {
                        Console.WriteLine("{1} {0} user account is retrieved.", user.FirstName, user.LastName);
                        // Find the role.
                        var query = new QueryExpression
                        {
                            EntityName = "role",
                            ColumnSet  = new ColumnSet("roleid"),
                            Criteria   = new FilterExpression
                            {
                                Conditions =
                                {
                                    new ConditionExpression
                                    {
                                        AttributeName = "name",
                                        Operator      = ConditionOperator.Equal,
                                        Values        = { _givenRole }
                                    }
                                }
                            }
                        };

                        // Get the role.
                        EntityCollection roles = service.RetrieveMultiple(query);

                        // Disassociate the role.
                        if (roles.Entities.Count > 0)
                        {
                            Role salesRole = service.RetrieveMultiple(query).Entities[0].ToEntity <Role>();

                            Console.WriteLine("Role {0} is retrieved.", _givenRole);

                            service.Disassociate(
                                "systemuser",
                                user.Id,
                                new Relationship("systemuserroles_association"),
                                new EntityReferenceCollection()
                            {
                                new EntityReference("role", salesRole.Id)
                            });
                            Console.WriteLine("Role {0} is disassociated from user {1} {2}.", _givenRole, user.FirstName, user.LastName);
                            #endregion Demonstrate

                            #region Clean up
                            CleanUpSample(service);
                            #endregion Clean up
                        }
                        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();
            }
        }
 public void Disassociate(string entityName, Guid entityId, Relationship relationship, EntityReferenceCollection relatedEntities)
 {
     _service.Disassociate(entityName, entityId, relationship, relatedEntities);
 }
Beispiel #5
0
        [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

                    // Retrieve a user.
                    SystemUser user = service.Retrieve(SystemUser.EntityLogicalName,
                                                       _userId, new ColumnSet(new String[] { "systemuserid", "firstname", "lastname" })).ToEntity <SystemUser>();

                    if (user != null)
                    {
                        Console.WriteLine("{1} {0} user account is retrieved.", user.LastName, user.FirstName);

                        // Find a role.
                        var query = new QueryExpression
                        {
                            EntityName = Role.EntityLogicalName,
                            ColumnSet  = new ColumnSet("roleid"),
                            Criteria   = new FilterExpression
                            {
                                Conditions =
                                {
                                    new ConditionExpression
                                    {
                                        AttributeName = "name",
                                        Operator      = ConditionOperator.Equal,
                                        Values        = { _givenRole }
                                    }
                                }
                            }
                        };

                        // Get the role.
                        EntityCollection givenRoles = service.RetrieveMultiple(query);

                        if (givenRoles.Entities.Count > 0)
                        {
                            Role givenRole = givenRoles.Entities[0].ToEntity <Role>();

                            Console.WriteLine("Role {0} is retrieved.", _givenRole);

                            Console.WriteLine("Checking association between user and role.");

                            // Establish a SystemUser link for a query.
                            var systemUserLink = new LinkEntity()
                            {
                                LinkFromEntityName    = SystemUserRoles.EntityLogicalName,
                                LinkFromAttributeName = "systemuserid",
                                LinkToEntityName      = SystemUser.EntityLogicalName,
                                LinkToAttributeName   = "systemuserid",
                                LinkCriteria          =
                                {
                                    Conditions =
                                    {
                                        new ConditionExpression(
                                            "systemuserid", ConditionOperator.Equal, user.Id)
                                    }
                                }
                            };

                            // Build the query.
                            QueryExpression linkQuery = new QueryExpression()
                            {
                                EntityName   = Role.EntityLogicalName,
                                ColumnSet    = new ColumnSet("roleid"),
                                LinkEntities =
                                {
                                    new LinkEntity()
                                    {
                                        LinkFromEntityName    = Role.EntityLogicalName,
                                        LinkFromAttributeName = "roleid",
                                        LinkToEntityName      = SystemUserRoles.EntityLogicalName,
                                        LinkToAttributeName   = "roleid",
                                        LinkEntities          = { systemUserLink }
                                    }
                                },
                                Criteria =
                                {
                                    Conditions =
                                    {
                                        new ConditionExpression("roleid", ConditionOperator.Equal, givenRole.Id)
                                    }
                                }
                            };

                            // Retrieve matching roles.
                            EntityCollection matchEntities = service.RetrieveMultiple(linkQuery);

                            // if an entity is returned then the user is a member
                            // of the role
                            Boolean isUserInRole = (matchEntities.Entities.Count > 0);

                            if (isUserInRole)
                            {
                                Console.WriteLine("User do not belong to the role.");
                            }
                            else
                            {
                                Console.WriteLine("User belong to this role.");
                            }
                        }
                        // Disassociate the role.
                        if (givenRoles.Entities.Count > 0)
                        {
                            Role salesRole = service.RetrieveMultiple(query).Entities[0].ToEntity <Role>();

                            Console.WriteLine("Role {0} is retrieved.", _givenRole);

                            service.Disassociate(
                                "systemuser",
                                user.Id,
                                new Relationship("systemuserroles_association"),
                                new EntityReferenceCollection()
                            {
                                new EntityReference("role", salesRole.Id)
                            });
                            Console.WriteLine("Role {0} is disassociated from user {1} {2}.", _givenRole, user.FirstName, user.LastName);
                        }
                    }


                    #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();
            }
        }
        [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
                    // Associate the accounts to the contact record.

                    // Create a collection of the entities that will be
                    // associated to the contact.
                    var relatedEntities = new EntityReferenceCollection();
                    relatedEntities.Add(new EntityReference(Account.EntityLogicalName, _account1Id));
                    relatedEntities.Add(new EntityReference(Account.EntityLogicalName, _account2Id));
                    relatedEntities.Add(new EntityReference(Account.EntityLogicalName, _account3Id));

                    // Create an object that defines the relationship between the contact and account.
                    var relationship = new Relationship("account_primary_contact");


                    //Associate the contact with the 3 accounts.
                    service.Associate(Contact.EntityLogicalName, _contactId, relationship,
                                      relatedEntities);

                    Console.WriteLine("The entities have been associated.");

                    //Disassociate the records.
                    service.Disassociate(Contact.EntityLogicalName, _contactId, relationship,
                                         relatedEntities);

                    Console.WriteLine("The entities have been disassociated.");
                    #endregion Demonstrate

                    #region Clean up
                    CleanUpSample(service);
                    #endregion Clean up
                }
                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();
            }
        }
 public void Disassociate(string entityName, Guid entityId, Relationship relationship, EntityReferenceCollection relatedEntities)
 {
     Retry(() => { CrmServiceClient.Disassociate(entityName, entityId, relationship, relatedEntities); return(true); });
 }