当前位置: 首页>>代码示例>>C#>>正文


C# OrganizationServiceProxy.Associate方法代码示例

本文整理汇总了C#中OrganizationServiceProxy.Associate方法的典型用法代码示例。如果您正苦于以下问题:C# OrganizationServiceProxy.Associate方法的具体用法?C# OrganizationServiceProxy.Associate怎么用?C# OrganizationServiceProxy.Associate使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在OrganizationServiceProxy的用法示例。


在下文中一共展示了OrganizationServiceProxy.Associate方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Run

        /// <summary>
        /// This method first connects to the Organization service. Afterwards,
        /// creates/retrieves a system user,
        /// updates the system user to associate with the salesperson role. 
        /// Note: Creating a user is only supported
        /// in an on-premises/active directory environment.
        /// </summary>
        /// <param name="serverConfig">Contains server connection information.</param>
        /// <param name="promptforDelete">When True, the user is prompted to delete all
        /// created entities.</param>
        public void Run(ServerConnection.Configuration serverConfig, bool promptforDelete)
        {
            try
            {
                //<snippetAddRoleToUser1>
                // Connect to the Organization service. 
                // The using statement assures that the service proxy is properly disposed.
                using (_serviceProxy = new OrganizationServiceProxy(serverConfig.OrganizationUri, serverConfig.HomeRealmUri,
                                                                     serverConfig.Credentials, serverConfig.DeviceCredentials))
                {
                    _serviceProxy.EnableProxyTypes();

                    CreateRequiredRecords();

                    // Find the role.
                    QueryExpression 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 roles = _serviceProxy.RetrieveMultiple(query);
                    if (roles.Entities.Count > 0)
                    {
                        Role salesRole = _serviceProxy.RetrieveMultiple(query).Entities[0].ToEntity<Role>();

                        Console.WriteLine("Role {0} is retrieved for the role assignment.", _givenRole);

                        _roleId = salesRole.Id;

                        // Associate the user with the role.
                        if (_roleId != Guid.Empty && _userId != Guid.Empty)
                        {
                            _serviceProxy.Associate(
                                        "systemuser",
                                        _userId,
                                        new Relationship("systemuserroles_association"),
                                        new EntityReferenceCollection() { new EntityReference(Role.EntityLogicalName, _roleId) });

                            Console.WriteLine("Role is associated with the user.");
                        }
                    }

                    DeleteRequiredRecords(promptforDelete);
                }
                //</snippetAddRoleToUser1>
            }
            // 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;
            }
        }
开发者ID:cesugden,项目名称:Scripts,代码行数:78,代码来源:AddRoleToUser.cs

示例2: Run


//.........这里部分代码省略.........

                    foreach (var roleId in roleIds)
                        _originalRolesIds.Add(roleId);

                    // add user to the created business unit
                    _serviceProxy.Execute(new SetBusinessSystemUserRequest()
                    {
                        BusinessId = _buisnessUnit.Id,
                        ReassignPrincipal = new EntityReference(
                            SystemUser.EntityLogicalName,
                            _users[2]),
                        UserId = _users[2]
                    });

                    #endregion

                    #region delete business unit

                    Console.WriteLine();
                    Console.WriteLine("  Deleting created business unit");

                    // remove all users from the business unit, moving them back to the
                    // parent business unit
                    _serviceProxy.Execute(new SetBusinessSystemUserRequest()
                    {
                        BusinessId = _rootBusinessUnit.Id,
                        ReassignPrincipal = new EntityReference(
                            SystemUser.EntityLogicalName, _users[2]),
                        UserId = _users[2]
                    });

                    // give the user back their original security roles
                    foreach (var roleId in roleIds)
                    {
                        _serviceProxy.Associate(
                             SystemUser.EntityLogicalName,
                             _users[2],
                             new Relationship("systemuserroles_association"),
                             new EntityReferenceCollection() { 
                                new EntityReference(
                                    Role.EntityLogicalName,
                                    roleId
                                )
                            }
                         );
                    }

                    // deactivate business unit before deleting it
                    _serviceProxy.Execute(new SetStateRequest()
                    {
                        EntityMoniker = _buisnessUnit.ToEntityReference(),
                        // mark the state as inactive (value 1)
                        State = new OptionSetValue(1),
                        Status = new OptionSetValue(-1)
                    });

                    // delete business unit
                    _serviceProxy.Delete(BusinessUnit.EntityLogicalName,
                        _buisnessUnit.Id);

                    #endregion

                    #region remove users from team

                    var teamMembers = from team in _context.TeamSet
                                      join membership in _context.TeamMembershipSet
                                      on team.TeamId equals membership.TeamId
                                      where team.TeamId == _team.Id
                                      select membership.SystemUserId.Value;

                    _serviceProxy.Execute(new RemoveMembersTeamRequest()
                    {
                        MemberIds = teamMembers.ToArray(),
                        TeamId = _team.Id
                    });

                    #endregion

                    #region delete team

                    Console.WriteLine();
                    Console.WriteLine("  Deleting the team");

                    // Delete the team
                    _serviceProxy.Delete(Team.EntityLogicalName, _team.Id);

                    #endregion

                    DeleteRequiredRecords(promptforDelete);
                }
	 //</snippetReassignBusinessUnitMembers1>
           }

            // 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;
            }
        }
开发者ID:cesugden,项目名称:Scripts,代码行数:101,代码来源:ReassignBusinessUnitMembers.cs


注:本文中的OrganizationServiceProxy.Associate方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。