Presence can be published either with or without the assistance of a publication grammar. One endpoint type, UserEndpoint, can publish its owner’s presence in either way. The other endpoint type, ApplicationEndpoint, cannot publish presence using a publication grammar.

Publishing Presence Categories Using a Publication Grammar

A UserEndpointinstance can be used to publish presence using a publication grammar using the PresenceCategoryclass. This type of publication matches that of Office Communicator.

To publish presence categories using a publication grammar
  1. Create a CustomPresenceCategoryinstance with a category named "state".

  2. Create a Listinstance whose base type is PresenceCategory.

  3. Add the presence item to the list created in the previous step.

  4. Publish the presence information using BeginPublishPresenceand EndPublishPresence. These two methods conform to the .NET asynchronous programming pattern.

The following example code is for purposes of illustration only.

Copy Code
private static String _busyXml = "<state
xmlns=\"http://schemas.microsoft.com/2006/09/sip/state\"
xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"
manual=\"true\"
xsi:type=\"userState\"><availability>{0}</availability></state>";
private static int _busyAvail = 6500;


CustomPresenceCategory customCat = new
CustomPresenceCategory("state", String.Format(_busyXml,
_busyAvail));
result = _userEndpoint.LocalOwnerPresence.BeginPublishPresence( new
PresenceCategory[] { customCat }, PublishPresenceCompleted,
_userEndpoint.LocalOwnerPresence);
  


private void PublishPresenceCompleted(IAsyncResult result)
{
  try
  {
	LocalOwnerPresence lop = result.AsyncState as
LocalOwnerPresence;
	lop.EndPublishPresence(result);
  }
  catch (PublishSubscribeException pse)
  {
   // Write exception handling code here.
  }
  catch (RealTimeException rte)
  {
   // Write exception handling code here.
  }
}

Publishing Presence Categories without a Publication Grammar

A UserEndpointinstance can be used to publish presence without using the publication grammar by means of the PresenceCategoryWithMetadataclass. An ApplicationEndpointinstance cannot use a publication grammar to publish presence, which means that it is responsible for supplying the details of which containers will be published to, the expiry policy for publication, the instance ID, and other information.

To publish presence categories without a publication grammar
  1. Create a PresenceCategoryWithMetadatainstance. In the example code, the PresenceCategoryWithMetadatainstance is created with a custom presence category named "state".

  2. Create a Listinstance whose base type is PresenceCategoryWithMetadata.

  3. Add the presence item to the list created in the previous step.

  4. Publish the presence information using BeginPublishPresenceand EndPublishPresence. These two methods conform to the .NET asynchronous programming pattern.

The following example code is for purposes of illustration only.

Copy Code
string userStateBody = "<state
xmlns=\"http://schemas.microsoft.com/2006/09/sip/state\"
xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"
xsi:type=\"userState\"><availability>3500</availability></state>";

// Instance ID is 1 and container ID is 0.
PresenceCategoryWithMetadata itemToPublish = new
PresenceCategoryWithMetadata(1, 0, new
CustomPresenceCategory("state", userStateBody));

List<PresenceCategoryWithMetadata> itemList = new
List<PresenceCategoryWithMetadata>();

itemList.Add(itemToPublish);
IAsyncResult result =
_userEndpoint.LocalOwnerPresence.BeginPublishPresence(itemList,
PublishPresenceCompleted, _userEndpoint.LocalOwnerPresence);
….

private void PublishPresenceCompleted(IAsyncResult result)
{
  try
  {
	LocalOwnerPresence lop = result.AsyncState as
LocalOwnerPresence;
	lop.EndPublishPresence(result);
  }
  catch (PublishSubscribeException pse)
  {
	// Write exception handling code here.
  }
  catch (RealTimeException rte)
  {
	// Write exception handling code here.
  }

}

Instance IDs enable a publisher to publish a given category of data multiple times. For example, a publisher can use two different endpoints to publish data. These endpoints can publish the same category, but for the server to consider them as distinct publications, each must have its own instance ID.

Instance IDs are provided by the publishing endpoint. The endpoint might provide a fixed instance ID (for example, 0) when the data logically has only one instance, such as an offline presence note sent by a publisher.

The ExpiryPolicyand Expiresproperties on a PresenceCategoryWithMetadatainstance can be used to control how long container data is published: for the lifetime of the publishing endpoint, for a specific time period, or until the data is changed. If a category is published with ExpiryPolicy.Timeand an Expiresvalue of 0, the category instance is deleted.