示例#1
0
        /// <summary>
        /// Creates any entity records that this sample requires.
        /// </summary>
        public static void CreateRequiredRecords(CrmServiceClient service)
        {
            // Create a unique identifier for this sample for preventing name conflicts.
            var sampleIdentifier = Guid.NewGuid();

            // Retrieve/create the system users to use for the sample.
            var ldapPath = String.Empty;

            _systemUserIds = SystemUserProvider.RetrieveDelegates(
                service, ref ldapPath);

            // Retrieve the root business unit to use for creating the team for the
            // sample.
            var businessUnitQuery = new QueryExpression
            {
                EntityName = BusinessUnit.EntityLogicalName,
                ColumnSet  = new ColumnSet("businessunitid"),
                Criteria   = new FilterExpression()
            };

            businessUnitQuery.Criteria.AddCondition("parentbusinessunitid",
                                                    ConditionOperator.Null);
            var businessUnitResult = service.RetrieveMultiple(businessUnitQuery);
            var businessUnit       = businessUnitResult.Entities[0].ToEntity <BusinessUnit>();

            // Get the GUID of the current user.
            var who         = new WhoAmIRequest();
            var whoResponse = (WhoAmIResponse)service.Execute(who);

            _currentUserId = whoResponse.UserId;

            // Create a team for use in the sample.
            var team = new Team
            {
                AdministratorId = new EntityReference(
                    "systemuser", _currentUserId),
                Name           = String.Format("User Access Sample Team {0}", sampleIdentifier),
                BusinessUnitId = businessUnit.ToEntityReference()
            };

            _teamId = service.Create(team);

            // Add the second user to the newly created team.
            var addToTeamRequest = new AddMembersTeamRequest
            {
                TeamId    = _teamId,
                MemberIds = new[] { _systemUserIds[1] }
            };

            service.Execute(addToTeamRequest);

            // Create a account for use in the sample.
            var account = new Account
            {
                Name = "User Access Sample Company",
            };

            _accountId = service.Create(account);

            // Create a task to associate to the account.
            var accountReference = new EntityReference(Account.EntityLogicalName, _accountId);
            var task             = new Task
            {
                Subject           = "User Access Sample Task",
                RegardingObjectId = accountReference
            };

            _taskId = service.Create(task);

            // Create a letter to associate to the account.
            var letter = new Letter
            {
                Subject           = "User Access Sample Letter",
                RegardingObjectId = accountReference
            };

            service.Create(letter);
        }