private XmlDocument SetAlertsWhenDoNotDisturbSetting(XmlDocument xmlDoc, AlertsWhenDoNotDisturbType alertsWhenDoNotDisturb, string xmlns) { if (alertsWhenDoNotDisturb == AlertsWhenDoNotDisturbType.NoChange) { return(xmlDoc); } XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlDoc.NameTable); nsmgr.AddNamespace("alerts", xmlns); XmlNode alertsWhenDoNotDisturbNode = xmlDoc.SelectSingleNode("//alerts:alertsWhenDoNotDisturb", nsmgr); if (alertsWhenDoNotDisturbNode != null) { if ((alertsWhenDoNotDisturb == AlertsWhenDoNotDisturbType.DisplayAllAlerts) && (alertsWhenDoNotDisturbNode.InnerText.ToLowerInvariant() != "displayallalerts")) { // alertsWhenDoNotDisturb node will be modified with value of displayAllAlerts xmlDoc.DocumentElement.RemoveChild(alertsWhenDoNotDisturbNode); XmlNode newNode = xmlDoc.CreateNode(XmlNodeType.Element, "alertsWhenDoNotDisturb", xmlns); newNode.InnerText = "displayAllAlerts"; xmlDoc.DocumentElement.AppendChild(newNode); } if ((alertsWhenDoNotDisturb == AlertsWhenDoNotDisturbType.DisplayAlertsFromHighPresence) && (alertsWhenDoNotDisturbNode.InnerText.ToLowerInvariant() != "displayalertsfromhighpresence")) { // alertsWhenDoNotDisturb node will be modified with value of displayAlertsFromHighPresence xmlDoc.DocumentElement.RemoveChild(alertsWhenDoNotDisturbNode); XmlNode newNode = xmlDoc.CreateNode(XmlNodeType.Element, "alertsWhenDoNotDisturb", xmlns); newNode.InnerText = "displayAlertsFromHighPresence"; xmlDoc.DocumentElement.AppendChild(newNode); } if ((alertsWhenDoNotDisturb == AlertsWhenDoNotDisturbType.NoAlerts) && (alertsWhenDoNotDisturbNode.InnerText.ToLowerInvariant() != "noalerts")) { // alertsWhenDoNotDisturb node will be modified with value of noAlerts xmlDoc.DocumentElement.RemoveChild(alertsWhenDoNotDisturbNode); XmlNode newNode = xmlDoc.CreateNode(XmlNodeType.Element, "alertsWhenDoNotDisturb", xmlns); newNode.InnerText = "noAlerts"; xmlDoc.DocumentElement.AppendChild(newNode); } } else { // alertsWhenDoNotDisturb node does not exist -> it will be created XmlNode newNode = xmlDoc.CreateNode(XmlNodeType.Element, "alertsWhenDoNotDisturb", xmlns); switch (alertsWhenDoNotDisturb) { case AlertsWhenDoNotDisturbType.DisplayAllAlerts: newNode.InnerText = "displayAllAlerts"; break; case AlertsWhenDoNotDisturbType.DisplayAlertsFromHighPresence: newNode.InnerText = "displayAlertsFromHighPresence"; break; case AlertsWhenDoNotDisturbType.NoAlerts: newNode.InnerText = "noAlerts"; break; default: // Should not occur break; } xmlDoc.DocumentElement.AppendChild(newNode); } return(xmlDoc); }
public void SetAlertNotificationSetting(string userUri, NotifyAdditionToContactListType notifyAdditionToContactList, AlertsWhenDoNotDisturbType alertsWhenDoNotDisturb) { const string defaultDataXml = "<alerts xmlns=\"http://schemas.microsoft.com/2006/09/sip/options/alerts\"></alerts>"; const string alertsXmlns = "http://schemas.microsoft.com/2006/09/sip/options/alerts"; if ((notifyAdditionToContactList != NotifyAdditionToContactListType.NoChange) || (alertsWhenDoNotDisturb != AlertsWhenDoNotDisturbType.NoChange)) { string newDataXml = ""; userEndpoint.InitializeUserEndpoint(userUri); userEndpoint.SubscribeToPresence(); if (userEndpoint.User.ContactGroupServices.CurrentState == CollaborationSubscriptionState.Subscribed) { XmlDocument doc = new XmlDocument(); if (userEndpoint.AlertNotification != null) { string dataXml = userEndpoint.AlertNotification.Category.GetCategoryDataXml(); doc.LoadXml(dataXml); string xmlns = doc.DocumentElement.Attributes["xmlns"].Value; if (xmlns != alertsXmlns) { doc.LoadXml(defaultDataXml); } } else { doc.LoadXml(defaultDataXml); } doc = SetNotifyAdditionToContactListSetting(doc, notifyAdditionToContactList, alertsXmlns); doc = SetAlertsWhenDoNotDisturbSetting(doc, alertsWhenDoNotDisturb, alertsXmlns); newDataXml = doc.InnerXml; } else { throw new Exception("Collaboration subscription state is not subscribed"); } PresenceCategory cat = new CustomPresenceCategory("alerts", newDataXml); List <PresenceCategory> catCollection = new List <PresenceCategory>(); catCollection.Add(cat); userEndpoint.User.LocalOwnerPresence.EndPublishPresence(userEndpoint.User.LocalOwnerPresence.BeginPublishPresence(catCollection, null, null)); } }
public void ProcessAlertNotifications(string usersFile, string alertWhenAddToContactListNotification, string alertWhenDNDNotification) { List <string> usersList = new List <string>(); try { usersList = FileAccess.GetUsersFileData(usersFile); } catch (Exception ex) { Log.WriteLogEntry("ERROR", String.Format("Error while reading Users file: {0}", ex.Message), "Error while reading Users file."); CleanupEnvironment(); Program.CloseApplicationOnError(); } NotifyAdditionToContactListType notifyAdditionToContactList = NotifyAdditionToContactListType.NoChange; switch (alertWhenAddToContactListNotification.ToLowerInvariant()) { case "": notifyAdditionToContactList = NotifyAdditionToContactListType.NoChange; break; case "yes": notifyAdditionToContactList = NotifyAdditionToContactListType.Yes; break; case "no": notifyAdditionToContactList = NotifyAdditionToContactListType.No; break; default: // Should not happen Log.WriteLogEntry("ERROR", "Incorrect value for NotifyAdd parameter."); CleanupEnvironment(); Program.CloseApplicationOnError(); break; } AlertsWhenDoNotDisturbType alertsWhenDoNotDisturb = AlertsWhenDoNotDisturbType.NoChange; switch (alertWhenDNDNotification.ToLowerInvariant()) { case "": alertsWhenDoNotDisturb = AlertsWhenDoNotDisturbType.NoChange; break; case "noalerts": alertsWhenDoNotDisturb = AlertsWhenDoNotDisturbType.NoAlerts; break; case "allalerts": alertsWhenDoNotDisturb = AlertsWhenDoNotDisturbType.DisplayAllAlerts; break; case "alertsfromworkgroup": alertsWhenDoNotDisturb = AlertsWhenDoNotDisturbType.DisplayAlertsFromHighPresence; break; default: // Should not happen Log.WriteLogEntry("ERROR", "Incorrect value for NotifyWhenDND parameter."); CleanupEnvironment(); Program.CloseApplicationOnError(); break; } if (usersList.Count > 0) { ucmaPlatform.StartupPlatform(); ucmaEndpoint = new Endpoint(ucmaPlatform.CollabPlatform); AlertsManager alertsManager = new AlertsManager(ucmaEndpoint); foreach (string user in usersList) { Log.WriteLogEntry("INFO", String.Format("Processing user {0}...", user.ToLowerInvariant()), String.Format("Processing user {0}...", user.ToLowerInvariant())); try { alertsManager.SetAlertNotificationSetting(user, notifyAdditionToContactList, alertsWhenDoNotDisturb); } catch (RegisterException rex) { Log.WriteLogEntry("ERROR", String.Format("Error while processing user {0}: {1}. Inner Exception: {2}. Diag Info: {3}.", user.ToLowerInvariant(), rex.Message, (rex.InnerException == null ? "N/A" : rex.InnerException.Message), (String.IsNullOrEmpty(rex.DiagnosticInformation.ToString()) ? "N/A" : rex.DiagnosticInformation.ToString())), String.Format("Error while processing user {0}", user.ToLowerInvariant())); } catch (Exception ex) { Log.WriteLogEntry("ERROR", String.Format("Error while processing user {0}: {1}. Inner Exception: {2}", user.ToLowerInvariant(), ex.Message, (ex.InnerException == null ? "N/A" : ex.InnerException.Message)), String.Format("Error while processing user {0}", user.ToLowerInvariant())); } } } else { Log.WriteLogEntry("WARNING", "Users file is empty or invalid. No action will be performed."); } }