كيف يمكنني إنشاء تنبيهات مخصصة لعناوين البريد الإلكتروني الخارجية؟

sharepoint.stackexchange https://sharepoint.stackexchange.com//questions/76085

  •  10-12-2019
  •  | 
  •  

سؤال

لدينا في شركتنا معلومات سرية مخزنة في SharePoint.يضغط بعض الأشخاص ذوي الفعالية السياسية من أجل إرسال التنبيهات إلى عناوين بريدهم الإلكتروني الخارجية.كيف يمكنني إرسال تنبيهات بريد إلكتروني مخصصة تحتوي على محتوى إلى رسائل بريد إلكتروني داخلية وتنبيهات بدون محتوى إلى عناوين بريد إلكتروني خارجية؟

هل كانت مفيدة؟

المحلول

إنشاء قوالب تنبيه جديدة

1 قم بإنشاء نسخة من القوالب الجاهزة، وقم بتغيير الاسم والمعرف، وأضف استدعاء المعالج المخصص لكل منها.

  • اجعل القوالب المخصصة تحتوي على ".Ext" في نهاية أسمائها.

  • تستدعي الميزات المخصصة أيضًا NotificationHandlerAssembly المخصص

    using System;
    using System.Runtime.InteropServices;
    using Microsoft.SharePoint;
    using Microsoft.SharePoint.Administration;
    using System.Xml;
    
    namespace Prov.Shared.CCPersonalEmail.Features.AlertTemplates
    {
        public class AlertTemplatesEventReceiver : SPFeatureReceiver
        {
            public override void FeatureActivated(SPFeatureReceiverProperties properties)
            {
                //The Properties we will add to the Alert Template
                //(Since this feature lives in the same project with the 
                //IAlertNotifyHandler we just refer to our own assembly)
                string ClassToAdd = "CCPersonalClass";
                SPFeatureDefinition FeatDef = (SPFeatureDefinition)properties.Feature.Definition;
                string ourAssemblyName = FeatDef.ReceiverAssembly;
    
                SPWebApplication WebApplication = (SPWebApplication)properties.Feature.Parent;
                SPWebService WebService = WebApplication.WebService; 
                SPAlertTemplateCollection colSPAT = new SPAlertTemplateCollection(WebService);
    
                foreach (SPAlertTemplate SPAT in colSPAT)
                {
                    if (SPAT.Name.Contains(".Ext"))
                    {
                        SPAT.Delete();
                    }
                }
    
                foreach (SPAlertTemplate SPAT in colSPAT)
                {
                    if (SPAT.Name.Contains("SPAlertTemplateType"))
                    {
                        SPAlertTemplate newSPAT = new SPAlertTemplate();
                        XmlDocument xmlSpit = new XmlDocument();
    
                        xmlSpit.LoadXml(SPAT.Xml.ToString());
    
                        //create node and add value
                        XmlNode node = xmlSpit.CreateNode(XmlNodeType.Element, "Properties", string.Empty);
    
                        //create 3 nodes
                        XmlNode nodeAssembly = xmlSpit.CreateElement("NotificationHandlerAssembly");
                        XmlNode nodeClassName = xmlSpit.CreateElement("NotificationHandlerClassName");
                        XmlNode nodeProperties = xmlSpit.CreateElement("NotificationHandlerProperties");
    
                        nodeAssembly.InnerText = ourAssemblyName;
                        nodeClassName.InnerText = ourAssemblyName.Substring(0,ourAssemblyName.IndexOf(",")) + "." + ClassToAdd;
    
                        //add to parent node
                        node.AppendChild(nodeAssembly);
                        node.AppendChild(nodeClassName);
                        node.AppendChild(nodeProperties);
    
                        //add to elements collection
                        xmlSpit.DocumentElement.AppendChild(node);
    
                        newSPAT.Xml = xmlSpit.InnerXml;
                        newSPAT.Name = SPAT.Name.ToString() + ".Ext";
                        newSPAT.Id = Guid.NewGuid();
    
                        colSPAT.Add(newSPAT);
                    }
                }
    
    
    
                foreach (SPTimerServiceInstance timerservice in WebApplication.Farm.TimerService.Instances)
                {
                    timerservice.Stop();
                    timerservice.Start();
                }
    
            }
    
    
    
            public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
            {
                SPWebApplication WebApplication = (SPWebApplication)properties.Feature.Parent;
                SPWebService WebService = WebApplication.WebService; 
                SPAlertTemplateCollection colSPAT = new SPAlertTemplateCollection(WebService);
    
                foreach (SPAlertTemplate SPAT in colSPAT)
                {
                    if (SPAT.Name.Contains(".Ext"))
                    {
                        SPAT.Delete();
                    }
                }
            }
    
        }
    }
    

تجميع معالج الإشعارات المخصصة

1 تطبق الفئة CCPersonalClass واجهة IAlertNotifyHandler التي تحتوي على الطريقة المحددة OnNotification

  • محاولات إرسال تنبيه مخصص (إذا فشل، أرسل تنبيهًا عاديًا.)

  • تنبيه مخصص:

    • يحصل على بيانات حقل البريد الإلكتروني الشخصي من قائمة معلومات المستخدم

    • إذا كان البريد الإلكتروني الشخصي فارغًا، فإنه يرسل تنبيهًا عاديًا فقط

    • إذا تم ملء البريد الإلكتروني الشخصي

      • إرسال البريد الإلكتروني العادي إلى البريد الإلكتروني الداخلي

      • إرسال بريد إلكتروني مخفض إلى عنوان خارجي

      • يستبدل http:// مع http://ext-

      • في حالة إرسال بريد إلكتروني خارجي، قم بإزالة جميع سجلات التفاصيل واترك الرؤوس فقط.

{

    using System;
    using Microsoft.SharePoint;
    using Microsoft.SharePoint.Utilities;
    using System.Text.RegularExpressions;
    using Framework.Logger;
    using System.Collections.Specialized;

    namespace Shared.CCPersonalEmail
    {
        class CCPersonalClass: IAlertNotifyHandler
        {
            public bool OnNotification(SPAlertHandlerParams alertHandler)
            {
                using (new SPMonitoredScope("Shared.CCPersonalClass OnNotification"))
                {
                    string siteUrl = alertHandler.siteUrl;
                    using (SPSite site = new SPSite(siteUrl + alertHandler.webUrl))
                    {
                        using (SPWeb web = site.OpenWeb())
                        {
                            try
                            {
                                return CustomAlertNotification(web, alertHandler, siteUrl);
                            }
                            catch
                            {
                                //if it fails due to configuration still the default alert should work
                                SPUtility.SendEmail(web, alertHandler.headers, alertHandler.body);
                                return false;
                            }
                        }
                    }
                }
            }
            private bool CustomAlertNotification(SPWeb web, SPAlertHandlerParams alertHandler, string siteUrl)
            {
                using (new SPMonitoredScope("Shared.CCPersonalClass CustomAlertNotification"))
                {
                    string PersonalEmail = string.Empty;
                    int eventCount = alertHandler.eventData.GetLength(0);

                    //Get the Personal Email from the User Information List
                    try
                    {
                        PersonalEmail = GetPersonalEmail(alertHandler.headers["to"], web);
                    }
                    catch (Exception e)
                    {
                        Logger.LogError(e, "CCPersonalClass");
                    }

                    //Send each alert in the alertHandler
                    for (int i = 0; i < eventCount; i++)
                    {
                        try
                        {
                            if (string.IsNullOrEmpty(PersonalEmail))
                            {
                                SPUtility.SendEmail(web, alertHandler.headers, alertHandler.body);
                            }
                            else
                            {
                                //Send a normal notification to the work email
                                SPUtility.SendEmail(web, alertHandler.headers, alertHandler.body);

                                //Send an abbreviated email to the external email
                                string txt = alertHandler.body.ToString().Replace("http://", "http://ext-");

                                //Digest messages- removing detail records
                                string strRegex = "<tr>\\s*<td class=\"[a-z]*?\"> <div class=\"nobr\">.*?</td>{0}s*</tr>|";
                                //Immediate messages- removing detail records
                                strRegex = strRegex + "<tr>\\s*<td class=\"formlabel\">.*<td class=\"altvb\">&nbsp;</td>\\s*</tr>";

                                txt = Regex.Replace(txt, strRegex, string.Empty, RegexOptions.Singleline);
                                StringDictionary PersonalHeader = alertHandler.headers;
                                PersonalHeader["cc"] = PersonalEmail;
                                SPUtility.SendEmail(web, PersonalHeader, txt);
                            }
                        }
                        catch (Exception e)
                        {
                           Logger.LogError(e, "CCPersonalClass");
                        }

                    }
                    if (eventCount > 1)
                        return true;
                    else
                        return false;
                }
            }
            private string GetPersonalEmail(string WorkEmail, SPWeb web)
            {
                using (new SPMonitoredScope("Shared.CCPersonalClass GetPersonalEmail"))
                {
                    SPQuery SelectUser = new SPQuery();
                    string SelectUserQS = string.Format("<Query><Where><Eq><FieldRef Name='EMail' /><Value Type='Text'>{0}</Value></Eq></Where></Query>", WorkEmail);
                    SelectUser.Query = SelectUserQS;
                    SPList UserList = web.Site.RootWeb.Lists["User Information List"];
                    SPListItemCollection SelectedUserCol = UserList.GetItems(SelectUser);
                    SPListItem SelectedUser = SelectedUserCol[0];
                    return (string)SelectedUser["PersonalEmail"];
                }
            }
        }
    }

ضبط التنبيهات لاستخدام القالب الخارجي

1 يتم تحديث المحتوى الموجود باستخدام جهاز استقبال الأحداث المميزة:

  • التنبيهات الموجودة:

    • يتم قلب Site.allwebs.alerts إلى نفس اسم القالب الذي كان موجودًا به مع إلحاق .ext
  • القوائم الموجودة:

    • قامت قوائم Site.allwebs.lists بقلب نماذج التنبيه المرفقة إلى نظيرتها ‎.ext

{

    using System;
    using System.Runtime.InteropServices;
    using System.Security.Permissions;
    using Microsoft.SharePoint;
    using Microsoft.SharePoint.Security;
    using Prov.Framework.Logger;

    namespace Prov.Shared.CCPersonalEmail.Features.CCAlerts
    {
        [Guid("a3889d39-ea91-441e-9039-157c512441e4")]
        public class CCAlertsEventReceiver : SPFeatureReceiver
        {
            // Set AlertTemplates on sites and alerts to custom templates.
            public override void FeatureActivated(SPFeatureReceiverProperties properties)
            {
                using (SPSite site = (SPSite)properties.Feature.Parent)
                {
                    AlertTemplateSelector objSelectedAlertTemplate = new AlertTemplateSelector(site);

                    //Add PersonalEmail field if it does not exist yet
                    SPFieldCollection UserInfoFields = site.RootWeb.Lists["User Information List"].Fields;
                    try
                    {
                        bool FieldExists = UserInfoFields.ContainsField("PersonalEmail");
                        if (!FieldExists)
                        {
                            UserInfoFields.Add("PersonalEmail", SPFieldType.Text, false);
                        }
                    }
                    catch (Exception e)
                    {
                        Logger.LogError(e, "CCPersonalClass");
                    }

                    foreach (SPWeb web in site.AllWebs)
                    {
                        using (web)
                        {
                            web.AllowUnsafeUpdates = true;

                            foreach (SPList List in web.Lists)
                            {
                                try
                                {
                                    List.AlertTemplate = objSelectedAlertTemplate.SwapAlertTemplate(List.AlertTemplate.Name, true);
                                    List.Update();
                                }
                                catch (Exception e)
                                {
                                    Logger.LogError(e, "CCAlertsEventReceiver");
                                }
                            }

                            foreach (SPAlert Alert in web.Alerts)
                            {
                                try
                                {
                                    Alert.AlertTemplate = objSelectedAlertTemplate.SwapAlertTemplate(Alert.AlertTemplate.Name, true);
                                    Alert.Update();
                                }
                                catch (Exception e)
                                {
                                    Logger.LogError(e, "CCAlertsEventReceiver");
                                }
                            }
                            web.AllowUnsafeUpdates = false;
                        }
                    }
                }
            }


            // Revert AlertsTemplates on sites and alerts back to out of box templates.

            public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
            {
                using (SPSite site = (SPSite)properties.Feature.Parent)
                {
                    AlertTemplateSelector objSelectedAlertTemplate = new AlertTemplateSelector(site);

                    foreach (SPWeb web in site.AllWebs)
                    {
                        using (web)
                        {
                            web.AllowUnsafeUpdates = true;

                            foreach (SPList List in web.Lists)
                            {
                                try
                                {
                                    List.AlertTemplate = objSelectedAlertTemplate.SwapAlertTemplate(List.AlertTemplate.Name, false);
                                    List.Update();
                                }
                                catch (Exception e)
                                {
                                    Logger.LogError(e, "CCAlertsEventReceiver");
                                }
                            }

                            foreach (SPAlert Alert in web.Alerts)
                            {
                                try
                                {
                                    Alert.AlertTemplate = objSelectedAlertTemplate.SwapAlertTemplate(Alert.AlertTemplate.Name, false);
                                Alert.Update();
                                }
                                catch (Exception e)
                                {
                                    Logger.LogError(e, "CCAlertsEventReceiver");
                                }
                            }
                            web.AllowUnsafeUpdates = false;
                        }
                    }
                }
            }
        }
    }

يتم استخدام الكود التالي من خلال ميزة التنشيط وإلغاء التنشيط ومعالج الأحداث OnListCreation

{

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Microsoft.SharePoint.Administration;
    using Microsoft.SharePoint;

    namespace Shared.CCPersonalEmail
    {
        class AlertTemplateSelector
        {
            private SPAlertTemplateCollection atc;
            private SPSite site;

            public AlertTemplateSelector(SPSite site)
            {
                this.site = site;
                this.atc = new SPAlertTemplateCollection((SPWebService)site.WebApplication.Parent);
            }

            public SPAlertTemplate SwapAlertTemplate(string PreviousAlert, bool isActivating)
            {

                if ((!PreviousAlert.EndsWith(".Ext")) && (isActivating))
                {
                    PreviousAlert = string.Format("{0}.Ext", PreviousAlert);
                }
                if ((PreviousAlert.EndsWith(".Ext")) && (!isActivating))
                {
                    PreviousAlert = PreviousAlert.Replace(".Ext", "");
                }

                SPAlertTemplate newTemplate = atc[PreviousAlert];
                if (newTemplate == null)
                {
                    if (isActivating)
                    {
                        newTemplate = atc["SPAlertTemplateType.GenericList.Ext"];
                    }
                    else
                    {
                        newTemplate = atc["SPAlertTemplateType.GenericList"];
                    }
                }
                return newTemplate;
            }

        }
    }

2 شبكات المستقبل وقوائمها المستقبلية

  • يقوم حدث SPListEventReceiver ListAdded() بقلب القالب في كافة القوائم التي تم إنشاؤها في مجموعة الموقع

{

    namespace Shared.CCPersonalEmail.OnListCreation
    {
        public class OnListCreation : SPListEventReceiver
        {
           public override void ListAdded(SPListEventProperties properties)
           {
                base.ListAdded(properties);
                SPWeb web = properties.Web;
                SPList List = web.Lists[properties.ListId];

                AlertTemplateSelector objSelectedAlertTemplate = new AlertTemplateSelector(web.Site);
                List.AlertTemplate = objSelectedAlertTemplate.SwapAlertTemplate(List.AlertTemplate.Name, true);
                List.Update();
           }
        }
    }

التدبير المنزلي

1 أثناء تفعيل الميزة

  • قم بإنشاء حقل بريد شخصي في قائمة معلومات المستخدم الخاصة بموقع rootweb

  • يتم تضمين الرمز في ميزة استقبال الحدث المنشط أعلاه

2 أثناء إلغاء تنشيط الميزة

  • يصبح البريد الإلكتروني الشخصي محتوى، لذا لا تتم إزالته عند إلغاء التنشيط

نصائح أخرى

إذا كان معزولا بقائمة واحدة فقط، فقد تكون قادرا على استخدام سير عمل بدلا من ذلك.يمكنك إنشاء قائمة احتجاز لتتبع نوع الشيء للبريد الإلكتروني على قائمة محددة من عناوين البريد الإلكتروني.عند إنشاء عنصر أو تم تحريره (مهما كان منطق التنبيه يجب أن يكون)، فإنك تبحث عن القائمة المناسبة لعناوين البريد الإلكتروني، والبريد الإلكتروني الذي يجمد البريد الإلكتروني المرعي.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى sharepoint.stackexchange
scroll top