This topic demonstrates how to publish the availability and personal note of the local user contact. This process involves defining an array of contact information types and a Dictionary< PublishableContactInformationType , object > that holds the contact information to be published. Publishing a set of contact information items raises the ContactInformationChanged event on the publishing contact. If the publishing contact is the local user then the event is raised both locally and for remote users who have subscribed to the local contact.

Publishing Contact Information

The following figure illustrates the classes, methods, and events used in the process of publishing contact information.

Publish Presence Information

  1. Get the LyncClient instance. Verify that the client is signed in to the server. For information about signing in to Microsoft Lync Server 2010, see Walkthrough: Sign In to Lync .

  2. Create a Dictionary< PublishableContactInformationType , object > of contact information types and the corresponding values to be updated. The Dictionary you declare and instantiate is passed into BeginPublishContactInformation .

  3. Add a ContactInformationType and the corresponding publishable value to the dictionary you created previously.

  4. Read the Self property to get an instance of Self .

  5. Optional: Declare and instantiate a state object such as a string , and fill it with appropriate state information. The callback method you provide should access this state information to provide a context for the operation.

  6. Call BeginPublishContactInformation , passing the dictionary, the callback method (or null), and the state object.

Examples

The following examples publish the presence information typically published in a custom application.

Publish a Personal Note and Current Availability

The following example method publishes a new personal note for the local user.

Tip Tip

In the example, a callback method included as a parameter in the PublishPresenceItemsmethod call. You should pass a nullvalue in the callback parameter position if you are not interested in catching the result of the publication.

C#  Copy imageCopy Code
		/// <summary>
		/// Publishes an update to a personal note
		/// </summary>
		/// <param name="newNote">string. The new personal
note text.</param>
		public void PublishPersonalNoteAndFreeAvailability(string
newNote)
		{
			//Each element of this array must contain a valid
enumeration. If null array elements 
			//are passed, an ArgumentException is raised.
			Dictionary<PublishableContactInformationType,
object> publishData = new
Dictionary<PublishableContactInformationType, object>();
		 
publishData.Add(PublishableContactInformationType.PersonalNote,
newNote);
		 
publishData.Add(PublishableContactInformationType.Availability,
ContactAvailability.Free);

			//Helper method is found in the next example.
			SendPublishRequest(publishData, "Personal Note and
Availability");
	}

The following example begins the asynchronous publication process.

C#  Copy imageCopy Code
		/// <summary>
		/// Sends a publication request and handles any exceptions
raised.
		/// </summary>
		/// <param name="publishData">Dictionary. Information
to be published.</param>
		/// <param name="PublishId">string. Unique
publication identifier.</param>
		private void SendPublishRequest(
		 Dictionary<PublishableContactInformationType,
object> publishData,
			string PublishId)
		{
			object publishState = (object)PublishId;
			object[] _PublishAsyncState = { _LyncClient.Self,
publishState };
			try
			{
				_LyncClient.Self.BeginPublishContactInformation(
					publishData,
					PublicationCallback,
					_PublishAsyncState);
		}
			catch (COMException ce)
			{
				MessageBox.Show("publish COM exception: " +
ce.ErrorCode.ToString());
		}
			catch (ArgumentException ae)
			{
				MessageBox.Show("publish Argument exception: " +
ae.Message);
		}
	}

The following example is the callback method passed into BeginPublishContactInformationin the previous example.

C#  Copy imageCopy Code
		/// <summary>
		/// callback method called by
BeginPublishContactInformation()
		/// </summary>
		/// <param name="ar">IAsyncResult. Asynchronous
result state.</param>
		private void PublicationCallback(IAsyncResult ar)
		{
			if (ar.IsCompleted)
			{
				object[] _asyncState = (object[])ar.AsyncState;
			 
((Self)_asyncState[0]).EndPublishContactInformation(ar);
		}
	}

The following example handles the ContactInformationChanged event that is raised when the current contact information state has changed. The example uses a message box to notify a user that presence has been updated and then removes the registration for Contact contact information update events.

C#  Copy imageCopy Code
		/// <summary>
		/// Handles event raised when the presence of a contact has
been updated
		/// </summary>
		/// <param name="source">Contact. Contact instance
whose presence is updated</param>
		/// <param name="data">PresenceItemsChangedEventArgs.
Collection of presence item types whose values have been
updated.</param>
		void Contact_ContactInformationChanged(object sender,
ContactInformationChangedEventArgs e)
		{
			if (((Contact)source) == _LyncClient.Self.Contact)
			{
				System.Text.StringBuilder sb = new
System.Text.StringBuilder();
				sb.Append("Changed types: " +
System.Environment.NewLine);
				foreach (ContactInformationType
changedInformationType in e.ChangedContactInformation)
				{
					sb.Append(changedInformationType.ToString() +
System.Environment.NewLine);
			}
				MessageBox.Show("Self publish succeeded: " +
sb.ToString());
		}
	}