none
IAlertNotifyHandler & SPUtility.SendEmail RRS feed

  • Question

  • I need some help with figuring out a strange bug in SharePoint or my code. I've written a custom alert template to produce the desired alert email; however, needed to also implement IAlertNotifyHandler because we had to rewrite the URLs (for security purposes... didn't want to expose internal URLs). So I wrote the following bit of code in IAlertNotifyHandler to extract all the URLs from the alert email body using a regular expression, change the urls, and then do a find and replace. I verify that before sending edited html body that it has correctly changed the URLs (manually), then let the debugger fly to send the email off. 
    This is where the problem comes... sometimes the URLs gets messed up... specifically the last URL in the email. They way the email is formatted is such that you can see the URL in href but also between the <a> tags. So it is written twice. The URL shows correctly between the <a> tags, but not in the href. It adds, what seems like, a random character and spaces in between the URL that break it. It'll do something like:
        <a href="https://blah.blah.com/sp-sites/dev!  2/blah/_layouts/,Info=blah.blah.com,ssl+blah.com" >https://blah.blah.com/sp-sites/dev2/blah/_layouts/,Info=blah.blah.com,ssl+blah.com</a>
    


    I need some help figuring out why this happens... Could anyone pretty please help me out? 
    Here is my IAlertNotifyHandler code:
      
      public bool OnNotification(SPAlertHandlerParams ahp)
            {
                try
                {
                    using (SPSite site = new SPSite(ahp.siteUrl + ahp.webUrl))
                    {
                        using (SPWeb web = site.OpenWeb())
                        {
                            foreach (var alert in ahp.eventData)
                            {
                               
                                try
                                {
                                    string txt = ahp.body.ToString();
                                    Regex regx = new Regex(@"https://([\w+?\.\w+])+([a-zA-Z0-9\~\!\@\#\$\%\^\&amp;\*\(\)_\-\=\+\\\/\?\.\:\;\'\,]*)?", RegexOptions.IgnoreCase);
                                    MatchCollection matchCollection = regx.Matches(txt);
                                    List<string> matches = matchCollection
                                                    .Cast<Match>()
                                                    .Select(m => m.Value)
                                                    .Distinct()
                                                    .OrderByDescending(item => item.Length)
                                                    .ToList();
                                   
                                    foreach (string match in matches)
                                    {
                                        string sslvpnurl = "https://blah.com{0},Info={1},SSL+{2}";
                                        System.Uri uri = new Uri(match);
                                       string host = uri.Host;
                                        string[] localPath = uri.Segments;
     
                                        if (match.Contains("aspx"))
                                        {
                                            string path = string.Join("", localPath.Take(localPath.Length - 1).ToArray<string>());
                                            string item = localPath[localPath.Length - 1];
                                            sslvpnurl = String.Format(sslvpnurl, path, host, item);
                                        }
                                        else
                                        {
                                            sslvpnurl = String.Format(sslvpnurl, string.Join("", localPath), host, String.Empty); ;
                                        }
     
                                        txt = txt.Replace(match, sslvpnurl);
                                    }
     
                                    SPUtility.SendEmail(web, true, false, ahp.headers["to"], ahp.headers["subject"], txt);
                                }
                                catch (Exception ex)
                                {
                                    SPUtility.SendEmail(web, ahp.headers, ahp.body);
                                    return true;
                                }
                            }
                        }
                    }
                }
                catch (System.Exception ex)
                {
                    return false;
                }
     
                return true;
            }
    
     
    Saturday, October 22, 2011 9:36 PM

Answers

  • Solved it. It actually was an issue with not adding a newline to my custom email templates. For future references in your Custom Alert Templates xml make sure to add some newlines. I think SPUtility.SendEmail has a character count restriction per line. 
    • Marked as answer by Petethesweets Monday, October 24, 2011 2:28 AM
    Monday, October 24, 2011 2:28 AM

All replies

  • I think you are extracting value from Field of type Hyperlink in SharePoint List. 

    SPListItem item = list.GetItemById(1);

    string URL = item[“URL”].ToString();

    The only problem with this is it gets the value, you end up with two URL’s separated by a comma. This is because one is the actual display text for the field and the other is the value (the actual URL itself).

    In order to get just the URL value, you need to use the following object model code:

    SPFieldUrlValue value = new SPFieldUrlValue(item[“URL”].ToString());

    string URL = value.Url;


    http://moss-solutions.blogspot.com/
    Sunday, October 23, 2011 9:23 AM
  • Hi Raghavendra, thank you for your help. Getting the URL to rewrite doesn't seem to be the issue as it works for the other <a> tags and URLs... just seems like its occurring in the last <a> tag in the email. 

    Do you think it may be an issue with the HTML that I'm putting in the email body? I see a lot of "\r\n" in the HTML string that I pull from the alert email body - do you think that could be causing it to break my URL? In the body of my custom email template I dont put any newlines ("r\n\").

     

    • Marked as answer by Petethesweets Monday, October 24, 2011 2:28 AM
    • Unmarked as answer by Petethesweets Monday, October 24, 2011 2:28 AM
    Sunday, October 23, 2011 7:27 PM
  • Solved it. It actually was an issue with not adding a newline to my custom email templates. For future references in your Custom Alert Templates xml make sure to add some newlines. I think SPUtility.SendEmail has a character count restriction per line. 
    • Marked as answer by Petethesweets Monday, October 24, 2011 2:28 AM
    Monday, October 24, 2011 2:28 AM
  • 2048 chars per line.
    Friday, November 4, 2011 6:16 PM