using Microsoft.SharePoint; using Microsoft.SharePoint.Administration; using SPSolutions.SharePoint.Logging; using System; using System.Collections; using System.Collections.Generic; using System.Xml.Xsl; namespace SPSolutions.SharePoint.Alerts.Tokenization { public class AlertParamsTokenizer : Tokenizer { private SPAlertHandlerParams m_alertHandlerParams; private string m_notificationMessageXslt; private SPList m_list; private List m_items; private Hashtable m_eventXmlTokens; public SPList List { get { return this.m_list; } set { this.m_list = value; } } public List Items { get { if (this.m_items == null) { this.m_items = new List(); } return this.m_items; } set { this.m_items = value; } } public Hashtable EventXmlTokens { get { if (this.m_eventXmlTokens == null && !string.IsNullOrEmpty(this.m_alertHandlerParams.eventData[0].eventXml)) { this.m_eventXmlTokens = SPAlertTemplateUtil.GetTokensForeventXml(this.m_alertHandlerParams.eventData[0].eventXml); } return this.m_eventXmlTokens; } set { this.m_eventXmlTokens = value; } } public AlertParamsTokenizer() { } public AlertParamsTokenizer(SPAlertHandlerParams alertHandlerParams, string notificationMessageXslt) { this.m_alertHandlerParams = alertHandlerParams; this.m_notificationMessageXslt = notificationMessageXslt; base.Tokens = new Hashtable(); using (SPSite sPSite = new SPSite(alertHandlerParams.siteId)) { using (SPWeb sPWeb = sPSite.OpenWeb(alertHandlerParams.webId)) { this.List = sPWeb.Lists[this.m_alertHandlerParams.a.ListID]; for (int i = 0; i < this.m_alertHandlerParams.eventData.Length; i++) { try { if (this.m_alertHandlerParams.eventData[i].eventType != 4) { this.Items.Add(this.List.GetItemById(this.m_alertHandlerParams.eventData[i].itemId)); } } catch (Exception ex) { TraceProvider.WriteTrace(0u, TraceProvider.TraceSeverity.Monitorable, Guid.Empty, "w3wp.exe", "Alert Manager", "CustomAlertHandler", "Error getting ListItem:" + this.m_alertHandlerParams.eventData[i].eventType.ToString() + ", " + ex.Message); } } } } } public override void PopulateTokenValues() { for (int i = 0; i < this.m_alertHandlerParams.eventData.Length; i++) { try { if (this.m_alertHandlerParams.eventData[i].eventType == 1) { base.Tokens["EventDescription"] = "has been added"; } else { if (this.m_alertHandlerParams.eventData[i].eventType == 4) { base.Tokens["EventDescription"] = "has been deleted"; } else { if (this.m_alertHandlerParams.eventData[i].eventType == 4080) { base.Tokens["EventDescription"] = "has been replied to"; } else { if (this.m_alertHandlerParams.eventData[i].eventType == 2 || this.m_alertHandlerParams.eventData[i].eventType == -1) { base.Tokens["EventDescription"] = "has been changed"; } } } } } catch (Exception innerException) { throw new Exception("Error setting EventDescription fom eventType.", innerException); } using (SPSite sPSite = new SPSite(this.m_alertHandlerParams.siteId)) { using (SPWeb sPWeb = sPSite.OpenWeb(this.m_alertHandlerParams.webId)) { SPList sPList = sPWeb.Lists[this.List.ID]; try { base.Tokens["WebTitle"] = sPWeb.Title; base.Tokens["ListTitle"] = sPList.Title; base.Tokens["ListId"] = sPList.ID.ToString(); } catch (Exception innerException2) { throw new Exception("Error setting title tokens.", innerException2); } try { base.Tokens["SiteAbsoluteUrl"] = sPSite.Url; base.Tokens["WebAbsoluteUrl"] = sPWeb.Url; // 2017-12-05: possibility to add public web urls to alert templates try { using (var ps = new SPSite(sPSite.ID, SPUrlZone.Extranet)) { using (var pw = ps.OpenWeb(sPWeb.ID)) { base.Tokens["SiteAbsolutePublicUrl"] = ps.Url; base.Tokens["WebAbsolutePublicUrl"] = pw.Url; } using (var rs = new SPSite(ps.WebApplication.Sites[0].ID, SPUrlZone.Extranet)) { base.Tokens["WebAppAbsolutePublicUrl"] = rs.Url; } } } catch { throw; } base.Tokens["ListAbsoluteUrl"] = sPSite.MakeFullUrl(sPList.DefaultViewUrl); base.Tokens["MobileAbsoluteUrl"] = base.Tokens["WebAbsoluteUrl"] + "/_layouts/15/mobile/mbllists.aspx"; } catch (Exception innerException3) { throw new Exception("Error setting absolute tokens.", innerException3); } try { base.Tokens["WebLink"] = string.Format("{0}", base.Tokens["WebTitle"], base.Tokens["WebAbsoluteUrl"]); base.Tokens["ListLink"] = string.Format("{0}", base.Tokens["ListTitle"], base.Tokens["ListAbsoluteUrl"]); } catch (Exception innerException4) { throw new Exception("Error setting link tokens.", innerException4); } try { if (this.m_alertHandlerParams.eventData[i].eventType != 4) { var item = sPList.GetItemById(this.m_alertHandlerParams.eventData[i].itemId); if (item != null && sPList.BaseTemplate == SPListTemplateType.DiscussionBoard) { if (item.ContentTypeId.IsChildOf(new SPContentTypeId("0x0107"))) // message { try { int parentDiscussionId = (int) item["ParentFolderId"]; var parentDiscussionItem = sPList.GetItemById(parentDiscussionId); if (parentDiscussionItem != null) { base.Tokens["ParentDiscussionTitle"] = parentDiscussionItem.Title; } } catch { } } else if (item.ContentTypeId.IsChildOf(new SPContentTypeId("0x012002"))) // discussion { base.Tokens["ParentDiscussionTitle"] = item.Title; } } } } catch (Exception innerException5) { throw new Exception("Error setting item tokens.", innerException5); } } } } XsltArgumentList xsltArgumentList = new XsltArgumentList(); try { foreach (DictionaryEntry dictionaryEntry in base.Tokens) { if ((string)dictionaryEntry.Key != "AlertDetailsTable") { xsltArgumentList.AddParam(((string)dictionaryEntry.Key).Replace(" ", "_").Replace("{", "").Replace("}", ""), string.Empty, (base.Tokens[(string)dictionaryEntry.Key] == null) ? string.Empty : base.Tokens[(string)dictionaryEntry.Key]); } } for (int j = 0; j < this.m_alertHandlerParams.eventData.Length; j++) { Hashtable tokens; object key; (tokens = base.Tokens)[key = "AlertDetailsTable"] = tokens[key] + SPAlertTemplateUtil.TransformXmlToHtmlUsingXsl(this.m_alertHandlerParams.eventData[j].eventXml, this.m_notificationMessageXslt, xsltArgumentList); } } catch (Exception innerException6) { throw new Exception("Error populating AlertDetailsTable.", innerException6); } } private void AddListItemTokens(SPSite site, SPWeb web, SPListItem item, SPAlertEventData eventData) { int num = 0; string text = string.Empty; string text2 = string.Empty; string arg = string.Empty; try { if (item != null) { num = eventData.itemId; } } catch (Exception innerException) { throw new Exception("Error setting itemId", innerException); } try { if (item == null) { arg = site.Url + "/" + eventData.itemFullUrl; } else { if (!string.IsNullOrEmpty(this.m_alertHandlerParams.a.Properties["dispformurl"])) { arg = this.m_alertHandlerParams.a.Properties["dispformurl"]; } else { if (item.ContentType != null && !string.IsNullOrEmpty(item.ContentType.DisplayFormUrl)) { arg = item.ContentType.DisplayFormUrl; } else { arg = this.List.Forms[4].Url; } } } text = string.Format("{0}/{1}?ID={2}", web.Url, arg, num.ToString()); } catch (Exception innerException2) { throw new Exception("Error setting item Url", innerException2); } try { if (item != null) { text2 = item.Title; } else { text2 = eventData.itemFullUrl; text2 = text2.Substring(text2.LastIndexOf("/") + 1, text2.Length - (text2.LastIndexOf("/") + 1)); } } catch (Exception innerException3) { throw new Exception("Error setting item Title", innerException3); } base.Tokens["ItemId"] = num.ToString(); base.Tokens["ItemTitle"] = text2; base.Tokens["ItemDisplayFormAbsoluteUrl"] = text; base.Tokens["ItemLink"] = string.Format("{0}", text2, text); } public override Hashtable AddTokens(Hashtable tokens) { foreach (DictionaryEntry dictionaryEntry in base.Tokens) { tokens[dictionaryEntry.Key] = dictionaryEntry.Value; } return tokens; } } }