Administration Object Model: Best Practices and Tips

SharePoint 2007

This topic lists some best practices and tips for using the Administration object model.

Working with Collections in the Administration Object Model

Collections in the Administration object model implement the IEnumerable interface and provide a GetEnumerator() method that you can use for simple iteration over the collection. The returned collection is volatile, meaning that the method only returns a snapshot of the metadata objects. If another user or application changes the collection in the database, such as by adding, modifying, or deleting elements, the snapshot will be out of sync. Thus, two enumerators instantiated from the same collection at slightly different times can return different snapshots of the collection. This "iteration once only" design encourages you to take a snapshot at the moment you need it.

Implementing the IEnumerable interface enables you to use the foreach semantics of Microsoft Visual C#. Following are the two ways you can work with collection objects.

Both methods work; however, if you want to access a specific element in the collection, you should use Method 2 because the list is indexed. Method 2 is also faster in such cases, because you do not have to enumerate through the collection, one element at a time

Method 1

EntityCollection entityColl = mySysInstance.LobSystem.Entities;
      foreach (Entity entity in entityColl)
            if (entity.Name == "ProductModel")
entity.Identifiers.Create("ProductModelID", true, "System.Int32");

Method 2

IList<Entity> entityCollection= new List<Entity>(mySysInstance.LobSystem.Entities);
      entityCollection[3].Name = "XYZ";


IDs and Using GetById() to Get a Specific Object

All metadata objects have unique IDs. The IDs are persistent throughout the lifetime of a metadata package. This means that if you delete a package and reimport it, the objects have different IDs. However, the IDs are still unique.

The metadata objects provide a method named GetById() that you can use to quickly get to a specific instance of the object, if you know its ID. You typically use this method to save the ID of an object and use it to get the object quickly at a later time. The GetById() method takes the ID of the instance. The following code example shows how you can get the ID of an object.

IList<Entity> entityCollection = new List<Entity>(mySysInstance.LobSystem.Entities);
Int EID = entityCollection[4].Id;


//and use it later like this:
Entity e = Entity.GetById(EID);

See Also

Community Additions