Skip to content

Example: Owner Teams

Thomas Malowanczyk edited this page Jun 20, 2018 · 3 revisions

Scenario

A member of a owner team should be able to access any records that the team it self has privilege to.

Creating The Test

The following test ensures that the specification is met. Note that the TypeDeclarations.cs file should be included in the solution and the namespace XrmMockup.TypeDeclarations should be referenced.

public void Initialize()
{
    EntityReference businessUnitId = crm.RootBusinessUnit;

    businessUnit1 = new BusinessUnit {
        ParentBusinessUnitId = businessUnitId,
        Name = "Business Unit 1" };
    businessUnit1.Id = orgAdminService.Create(businessUnit1);

    // SystemCustomizer - read account - user level, write account - user level
    var team = new Team {
        Name = "Team",
        BusinessUnitId = businessUnit1.ToEntityReference(),
        TeamType = Team_TeamType.Owner };
    team1 = crm.CreateTeam(orgAdminService, team1, 
        SecurityRoles.SystemCustomizer).ToEntityReference();

    user = crm.CreateUser(orgAdminService, businessUnit2.ToEntityReference(), 
        SecurityRoles.SystemCustomizer).ToEntityReference();

    parentAccount = new EntityReference(Account.EntityLogicalName, 
        orgAdminService.Create(new Account { Name = "Parent 1" }));

    var account = new Account {
        Name = "Child 1",
        ParentAccountId = parentAccount,
        OwnerId = team1.ToEntityReference() };
    account1 = new EntityReference(Account.EntityLogicalName, 
        orgGodService.Create(account)).ToEntityReference();

}

[TestMethod]
public void TeamAccessTest()
{
    Assert.IsFalse(CanRead(user1, account1));

    crm.AddUsersToTeam(team1, user1);
    Assert.IsTrue(CanRead(user1, account1));

    crm.RemoveUsersFromTeam(team1, user1);
    Assert.IsFalse(CanRead(user1, account1));
}

public bool CanRead(SystemUser user, EntityReference account)
{
    try
    {
        var entity = Account.Retrieve(crm.CreateOrganizationService(user.Id), account.Id);
        return entity != null;
    }
    catch (FaultException e)
    {
        return false;
    }
}