Skip to content

Example: Owner Teams

Magnus Gether Sørensen edited this page Aug 30, 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. The code shown in this example will use our early-bound context generator, XrmContext. We use this, since it has some great features compared to CrmSvcUtil. For a comparison, look at https://github.com/delegateas/XrmContext/wiki/functionality

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;
    }
}