Leading announcement: I appreciate the feedback I’ve gotten from some of you about the API of the new client.  One piece of key feedback I want very much to incorporate is how we can unify accessing single-valued and multi-valued attributes.  Please send any additional comments my way.

For this week’s post we see the benefits of promoted properties and transactions.  The full source code is available on MSDN code gallery.

Example 1: Create a Named Group

In this example we create a new object and pass it to the client to be created on the server.

public Guid CreateGroup()
{
    RmGroup newGroup = new RmGroup();
    newGroup.DisplayName = "My New Group";
    // Demonstrate accessing a non-promoted attribute
    newGroup[RmGroup.AttributeNames.MembershipAddWorkflow].Value = "OwnerApproval";
    // Any user object's ObjectId
    newGroup.Owner = this.OwnerObjectId;
 
    return this.client.Create(newGroup);
}

Now let’s add some users to the group.

Example 2: Add members to Group

In this example we demonstrate using a transaction to track our changes to the group object.  To add a member to a group simply add another reference to the ExplicitMember attribute. The public client exposes the ExplicitMember as List<Guid>, and in this example we add another Guid.  Once all of the changes are complete, we accept the changes and send the transaction to the client.  The client transforms the changes into da:ModifyRequest/da:Change elements and sends them over the wire.

public void AddUsers(Guid groupId)

{
 
    RmGroup group = this.client.Get(groupId) as RmGroup;
    RmResourceTransaction transaction = new RmResourceTransaction(group);
    transaction.BeginChanges();
    int numberAdded = 0;
    foreach(RmResource resource in this.client.Enumerate("/Person[Department='Sales']"))
    {
        RmPerson person = resource as RmPerson;
        if(person != null)
        {
            numberAdded++;
            group.ExplicitMember.Add(person.ObjectID);
        }
    }
    Assert.IsTrue(numberAdded >0);
    transaction.AcceptChanges();
    Assert.IsTrue(this.client.Put(transaction));
}

We successfully created a group and added users to it programmatically.