Udostępnij za pośrednictwem


Sample scenario: Add and remove members in a group

In our last post we created a new group.  Now we’ll doing something truly exciting: We’ll add members to the group.  Adding and removing members is the same Put request.  The difference is we specify “Add” or “Remove” as the operation for each member.  The sample client records values added and removed in code, and it computes the difference when constructing a Put request.  You don’t have to track which users were added or removed.

In this example we will now add a user to the group  created in the last post.  The code:

static void Topic16AddMemberToGroup()

        {

            ResourceManagementObject createdGroup = null;

 

            using (WSEnumerationClient enumerationClient = new WSEnumerationClient())

            {

                enumerationClient.ClientCredentials = GetCredentials();

 

                enumerationClient.Enumerate("/Group[DisplayName='CreatedGroupDisplay']");

                if (enumerationClient.IsContextValid)

                {

                    IList<ResourceManagementObject> groups = enumerationClient.Pull(1);

                    if (groups.Count > 0)

                    {

                         createdGroup = groups[0];

                    }

                }

 

                if (null == createdGroup)

                {

                    throw new InvalidOperationException("The created group was not found");

                }

            }

 

            if (createdGroup["ExplicitMember"] == null)

            {

                createdGroup["ExplicitMember"] = new ResourceManagementAttribute(true, "ExplicitMember");

            }

            createdGroup["ExplicitMember"] = ResourceManagementAttributeFactory.ConvertToMultiValuedAttribute(createdGroup["ExplicitMember"]);

 

            using (WSEnumerationClient enumerationClient = new WSEnumerationClient())

            {

                enumerationClient.ClientCredentials = GetCredentials();

 

                enumerationClient.Enumerate("/Person");

                while (enumerationClient.IsContextValid)

                {

                    foreach (ResourceManagementObject person in enumerationClient.Pull())

                    {

                        // Please note that ResourceManagementAttribute will note which are new and not.

                        createdGroup["ExplicitMember"].AddMultiValue(person.ObjectId);

                    }

                }

            }

 

            WSTransferClient transferClient = new WSTransferClient();

            transferClient.ClientCredentials = GetCredentials();

 

            transferClient.Put(createdGroup);

 

            Console.WriteLine("Topic 16 Complete");

        }

    }

 

  1. We don’t know the object ID of our created group so we first search for it.  We send an enumeration filter for groups that match our display name.  In production code multiple groups could have the same display name, so this filter will not necessarily return the group we want first.  For our purposes, we hope you excuse the assumption.
  2. Once we have the group we check that the ExplicitMember attribute exists.  This is a list of Person object IDs who are explicitly members of our group.
  3. Now we need the object ID of the person or persons to add.  In our example we add everyone in ILM “2” RC to this group by enumerating all Person objects.
  4. To add a member to the group, you simply add another object ID to the multi-valued attribute.
  5. We put the copy of the group on the ILM Service.  The put request is how you update objects.  The sample client computes which members are new and only sends updates for those.
  6. To remove members you remove the object ID from the multi-valued attribute.

That’s it!  We have successfully updated an existing object in ILM, and more importantly, we added members to our group.

You now have the building blocks to recreate much of the ILM “2” portal’s functionality.  You can list users, filter for users in a particular group, create objects, and add members to a group.  I hope this initial tour has been useful and fruitful for you.  Thank you for following along and providing useful feedback.  We are ever excited to release ILM “2”.  After the winter hiatus we will continue on with different extensibility scenarios in ILM “2”.  Happy exploring!

Comments

  • Anonymous
    November 05, 2009
    When i try to add a member to a group that has owner approval via the web service it gets bounced.  What are the steps i need to complete inorder to get the required security token in order to add the group?? Code examples would be nice :-)