none
event receiver for email notification team leaders RRS feed

  • Question

  • i have a document library that has the 5 views. there are the 5 teams(sharepoint groups):

    tim I-2019, tim II-2019, tim III-2019, tim IV-2019 and tim V-2019.

    each team(group) has  its  own view: tim I-2019 > timI, tim II-2019 > timII.... on each view is set target audience for sharepoint group.

    each team has the team leader that also has membership in team (sharepoint group).

    the problem is, when is changed item properties i need event receiver that with email will notify the team leader. the email must contains  document name and link to the view. for example, the team leader of tim I-2019 need  link in mail to the timI view, team leader of tim II-2019 need link to the tim II view etc.

    I don't know how resolve this problem. my code is below and he works just one view, i can't make selection view per team leader

      var obradjivac = properties.ListItem["Обрађивач - одобрење"] == null ? "Изабери" : properties.ListItem["Обрађивач - одобрење"].ToString();
                            var vodjatima = properties.ListItem["Вођа тима - одобрење"] == null ? "Изабери" : properties.ListItem["Вођа тима - одобрење"].ToString();
                            var vrhovni = properties.ListItem["Врховни ревизор - одобрење"] == null ? "Изабери" : properties.ListItem["Врховни ревизор - одобрење"].ToString();
                            string vracanjenaobraduvodjatima = properties.ListItem["Број(Вођа тима - враћање на поновну обраду)"].ToString();
                           string vracanjenaobraduvrhovnirevizor = properties.ListItem["Број(Врховни ревизор - враћање на поновну обраду)"].ToString();
                            
                            if (vodjatima.ToString() == "Не")
                            {
                                properties.ListItem["Обрађивач - одобрење"] = "Не";
                                properties.ListItem["Вођа тима - одобрење"] = "Изабери";
                                properties.ListItem["Врховни ревизор - одобрење"] = "Изабери";
                                double broj_v = Convert.ToDouble(vracanjenaobraduvodjatima);
                               broj_v = broj_v + 1;
                                properties.ListItem["Број(Вођа тима - враћање на поновну обраду)"] = broj_v;
                            }
                            else if (vodjatima.ToString() == "Изабери" && obradjivac.ToString() == "Да" && vrhovni.ToString() == "Изабери")
                            {
                                
                                string url = properties.WebUrl;
                                using (SPSite site = new SPSite(url))
                                {
                                    using (SPWeb web = site.OpenWeb())
                                    {
                                        // myemail@test.com is obviously replaced with a real working email
                                        SendMail(web, "podrska@dri.rs", properties.ListItem["Субјект ревизије:Вођа тима - Е-маил"].ToString(), "Обавештење о одобрењу документа - Вођа тима", "Потребно је одобрити документ" + " " + properties.ListItem["Име"] + ", локација за одобрење " + properties.WebUrl + "/" + properties.List.DefaultViewUrl, "", "");
                                    }
                                }
                            }
                            
        public void SendMail(SPWeb web, string from, string to, string subject, string body, string cc, string bcc)
            {
                try
                {
                    StringDictionary headers = new StringDictionary();
                    string[] mailvodjavrhovni = to.Split('#');
                    string mail = mailvodjavrhovni[1];
                    headers.Add("from", from);
                    headers.Add("to", mail);
                    headers.Add("subject", subject);
                    if (!String.IsNullOrEmpty(cc)) headers.Add("cc", cc);
                    if (!String.IsNullOrEmpty(bcc)) headers.Add("bcc", bcc);
                    headers.Add("content-type", "text/html");
                    SPUtility.SendEmail(web, headers, body);
                }
                catch (Exception exc)
                {
                    // a custom method to write error messages to ULS
                  //  SPULSTraceListener traceULS = new SPULSTraceListener();
                    SPDiagnosticsService.Local.WriteTrace(0, new SPDiagnosticsCategory("Failed - sending mail", TraceSeverity.Unexpected, EventSeverity.Error), TraceSeverity.Unexpected, exc.Message, exc.StackTrace);
                        
                    return;
                }
            }

    Thursday, May 23, 2019 6:06 AM

All replies

  • Hi,

    Each view has Url property so you could sent the url to leader.

    Sample code.

     var list = web.Lists.TryGetList("MyList");
                    SPView view = list.Views["View1"];
                    var fullUrl=list.ParentWeb.Url + "/" + view.Url

    Best Regards,

    Lee


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.

    Friday, May 24, 2019 2:47 AM
  • ok, for this code but my problem is:

    the scenario -  the user that is member of group tim I-2019 access to document over the view 'timI', he edits properties of document in document library and select in document field properties.ListItem["Обрађивач - одобрење"] 'Да', then he clicks on ok in edit form, automatically the team leader receives email that contains link for the view 'timI' where he can see this document. how to determine based on membership in group which view belogns to group(group timI-2019 > view timI...)?

    this part of the code refers to this problem

      else if (vodjatima.ToString() == "Изабери" && obradjivac.ToString() == "Да" && vrhovni.ToString() == "Изабери")
                            {
                                
                                string url = properties.WebUrl;
                                using (SPSite site = new SPSite(url))
                                {
                                    using (SPWeb web = site.OpenWeb())
                                    {
                                        // myemail@test.com is obviously replaced with a real working email
                                        SendMail(web, "podrska@dri.rs", properties.ListItem["Субјект ревизије:Вођа тима - Е-маил"].ToString(), "Обавештење о одобрењу документа - Вођа тима", "Потребно је одобрити документ" + " " + properties.ListItem["Име"] + ", локација за одобрење " + properties.WebUrl + "/" + properties.List.DefaultViewUrl, "", "");
                                    }
                                }
                            }

    Friday, May 24, 2019 5:47 AM
  • Hi,

    If each user belongs to a unique SharePoint group, check user group by below function and then get specific view(url) based on group.

    public static bool IsUserInGroup(this SPUser user,string groupName)
    
        {
    
            return user.Groups.Cast<SPGroup>().Any(g => g.Name.Equals(groupName));
    
        }

    Best Regards,

    Lee


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.

    Friday, May 24, 2019 7:17 AM
  • ok. but i don't understand how get specific view(url) based on group? this is my problem. which is connection between the group and view. i think that only connection is the target audience. i'm assigned group to view in target audience. the other connections i does not see
    Friday, May 24, 2019 8:05 AM
  • Hi,

    Try below code to get Target Audience.

    using (SPSite site = new SPSite("http://sp:12001"))
                {
                    SPWeb web = site.OpenWeb();
                    var list = web.Lists.TryGetList("MyList");
                    SPView view = list.Views["View1"];
                    var fullUrl = list.ParentWeb.Url + "/" + view.Url;
    
    
                    using (SPLimitedWebPartManager webPartManager = web.GetLimitedWebPartManager(view.Url, PersonalizationScope.Shared))
                    {
                        SPServiceContext context = SPServiceContext.GetContext(site);
                        AudienceManager audienceManager = new AudienceManager(context);
                        var audience=webPartManager.WebParts[0].AuthorizationFilter;
                        Console.WriteLine("");
                    }


    Best Regards,

    Lee


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.

    Friday, May 24, 2019 8:26 AM
  • i changed your code a little like on picture, but i receive error like on picture

    the problem is with PersonalizedScope that i can make the testing

    Friday, May 24, 2019 9:10 AM
  • Hi,

    Try reference System.Web DLL.


    Best Regards,

    Lee


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.


    Friday, May 24, 2019 9:19 AM
  • now is ok, but audience is empty, like on picture

    Friday, May 24, 2019 9:50 AM
  • my listview page timI has the 8 web parts. maybe next  line is problem

    webPartManager.WebParts[0].AuthorizationFilter;

    maybe webpart under 0 is not listview web part

    Friday, May 24, 2019 12:00 PM
  • above text is based on image below

    Friday, May 24, 2019 12:25 PM
  • Hi,

    The index[0] just an example, try to filter the webpart based on proper property.

    Sample code for your reference.

    SPServiceContext context = SPServiceContext.GetContext(site);
                        AudienceManager audienceManager = new AudienceManager(context);
                        foreach(var wp in webPartManager.WebParts)
                        {
                            if(wp.GetType().Name== "XsltListViewWebPart")
                            {
                                XsltListViewWebPart _WP = wp as XsltListViewWebPart;
                                if (_WP.DisplayName == "yourview")
                                {
                                    var audience = webPartManager.WebParts[0].AuthorizationFilter;
                                }
                            }
                            
                        }


    Best Regards,

    Lee


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.

    Monday, May 27, 2019 1:24 AM
  • i used your code but 'audience' is empty again

    if i understand code var audience receives group from target audience of web part? in my code i must disable manual set of view

    SPView view = properties.List.Views["timI"];

    how i get collection views of document library and from the for each loop i compare audience with field properties.listitem["ТИМ"] from document library?

    Monday, May 27, 2019 8:31 AM
  • i changed part of my code like

    foreach ( SPView view in properties.List.Views){
                            var fullUrl = properties.List.ParentWeb.Url + "/" + view.Url;
    
    
                            using (SPLimitedWebPartManager webPartManager = properties.Web.GetLimitedWebPartManager(fullUrl, PersonalizationScope.Shared))
                            {
                                SPServiceContext context = SPServiceContext.GetContext(properties.Web.Site);
                                AudienceManager audienceManager = new AudienceManager(context);
                                foreach (var wp in webPartManager.WebParts)
                                {
                                    if (wp.GetType().Name == "XsltListViewWebPart")
                                    {
                                        XsltListViewWebPart _WP = wp as XsltListViewWebPart;
                                        if (_WP.DisplayName == "timI")
                                        {
                                            var audience = webPartManager.WebParts[0].AuthorizationFilter;
                                        }
                                    }
    
                                }
                            }

    now, how i receive 'audience'?

    Monday, May 27, 2019 8:58 AM
  • Hi,

    What’s the value of audience now? if it returns like ;;;;GroupName, just split it.

     var audience = webPartManager.WebParts[0].AuthorizationFilter;

    Best Regards,

    Lee


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.

    Wednesday, May 29, 2019 9:46 AM
  • i changed my code and i debugged them, i found that every view 'audience' return value ;;;;GroupName, but for my view 'timI value is empty.

    the target audience of my list view web part




    • Edited by gogi1000 Wednesday, May 29, 2019 12:06 PM edit
    Wednesday, May 29, 2019 11:52 AM
  • Hi,

    You’re still access the first webpart based on your code.

    webPartManager.WebParts[0].AuthorizationFilter;

    Replace webPartManager.WebParts[0] as wp(variable in loop).

    Best Regards,

    Lee


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.

    Thursday, May 30, 2019 8:30 AM