1

I tried using properties.AfterProperties in event receiver that I am using for sending updates in SharePoint list to specific e-mail addresses. So far I found that it's working well with yes/no options.

I am now wondering if there is something similar to this that works on text fields and check boxes as well? It don't seem to be working as it is, value is always counting null or something on empty fields and even when text is added. Grateful for any help. Here is my code:

using System;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.Workflow;
using System.Collections.Generic;

namespace TestEventReceiver.SendMail
{

    public class SendMail : SPItemEventReceiver
    {

        public override void ItemUpdated(SPItemEventProperties properties)
        {

            List<ItemChangedEmail> emailToAddress1 = new List<ItemChangedEmail>();
            List<ItemChangedEmail> emailToAddress2 = new List<ItemChangedEmail>();
            List<ItemChangedEmail> emailToAddress3 = new List<ItemChangedEmail>();
            emailToAddress1.Add(new ItemChangedEmail { Fields = new List<string> { "InternalColumnName1", "InternalColumnName2", "InternalColumnName3" }, Email = "email1" });
            emailToAddress2.Add(new ItemChangedEmail { Fields = new List<string> { "InternalColumnName4", "InternalColumnName5" }, Email = "email2" });
            emailToAddress3.Add(new ItemChangedEmail { Fields = new List<string> { "InternalColumnName6" }, Email = "email3" });


            foreach (var column in emailToAddress1[0].Fields)
            {
                if (!string.IsNullOrEmpty(properties.AfterProperties[column].ToString()))
                {
                    SPUtility.SendEmail(properties.Web, false, false, emailToAddress1[0].Email, "Item changes", emailToAddress1[0].EmailBody);
                }
            }

            foreach (var column in emailToAddress2[0].Fields)
            {
                if (!string.IsNullOrEmpty(properties.AfterProperties[column].ToString()))
                {
                    SPUtility.SendEmail(properties.Web, false, false, emailToAddress2[0].Email, "Item changes", emailToAddress2[0].EmailBody);
                }
            }

            foreach (var column in emailToAddress3[0].Fields)
            {
                if (!string.IsNullOrEmpty(properties.AfterProperties[column].ToString()))
                {
                    SPUtility.SendEmail(properties.Web, false, false, emailToAddress3[0].Email, "Item changes", emailToAddress3[0].EmailBody);
                }
            }

            base.ItemUpdated(properties);
        }

        public class ItemChangedEmail
        {
            public string Email { get; set; }
            public List<string> Fields { get; set; }

            public string EmailBody { get; set; }

            public ItemChangedEmail()
            {
                Email = "<html><body>Hej! <br/><br/> Something has changed...";
            }
        }

    }

}
0

I usually use this to compare values in event receiver:

public override void ItemUpdating(SPItemEventProperties properties)
{
// Valor anterior
string anteriorCharter = Internal.esNulo(properties.ListItem["Charter"], "").ToString();
// Valor nuevo
string nuevoCharter = Internal.esNulo(properties.AfterProperties["Charter"], "").ToString();

if (anteriorCharter != nuevoCharter ) { your code }

La función esNulo:

public static object esNulo(object valor, object reemplazo)
    {
        if (valor == null)
            return reemplazo;
        else
            return valor;
    }

In this case are text columns.

  • Ok I see, I will try this tomorrow. I did try to use properties.ListItem [" "] but probably not in the right way. This looks good, I am hopeful. – Sanna Wahlén Apr 20 '16 at 17:48
0

Started all over and did this instead, one for every different email to be sent.

readonly SendEmailFieldsProperties _sendEmailFieldsProperties4 = new SendEmailFieldsProperties
    {
        Fields = new List<string> { "InternalName" },
        EmailAddress = string.Empty
    };


    public override void ItemUpdating(SPItemEventProperties properties)
    {
        base.ItemUpdating(properties);

        try
        {
            CheckIfFieldsHasBeenUpdated(properties, _sendEmailFieldsProperties1);

        }
        catch (Exception exception)
        {
            throw new Exception(exception.Message);
        }
    }

private void CheckIfFieldsHasBeenUpdated(SPItemEventProperties properties, SendEmailFieldsProperties sendEmailFieldsProperties)
    {
        foreach (var fieldName in sendEmailFieldsProperties.Fields)
        {
            if ((properties.ListItem[fieldName] == null) && (!string.IsNullOrEmpty(properties.AfterProperties[fieldName].ToString())))
            {
html with text  ...
var builder = new StringBuilder();
                builder.Append(MailUtility.AppendAttribute(null, subject));
                builder.Append("<br/>");
                builder.Append(MailUtility.AppendAttribute(message, fieldName));
                builder.Append("<br/>");
                builder.Append(MailUtility.AppendAttribute(linkToItemMessage, properties.ListItem.Url));
                var result = MailUtility.SendEmail(subject, builder.ToString(),
                                                   sendEmailFieldsProperties.EmailAddress, string.Empty,
                                                   properties.Web, false);
                if (result)
                {
                    break;
                } 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.