Tuesday, December 13, 2016

Send Notification email to the Assignee, change assignee and when status changes to Created BY

  Send Notification email to the Assignee, change  assignee and when status changes to Created By user









Add an event receiver to the issue tracker list:

  public enum IssueItemState
        {
            NewAssigned,
            StatusChanged
        }
        /// <summary>
        /// An item is being added.
        /// </summary>
        public override void ItemAdding(SPItemEventProperties properties)
        {
            base.ItemAdding(properties);
        }

        /// <summary>
        /// An item is being updated.
        /// </summary>
        public override void ItemUpdating(SPItemEventProperties properties)
        {
            base.ItemUpdating(properties);
            SPListItem item = properties.ListItem;
            SPUser newAssigneeUser = null;
            try
            {
                // Status Changed
                if (properties.ListItem["Status"].ToString().ToLower() != properties.AfterProperties["Status"].ToString().ToLower())
                {
                    SendNotification(item, null, IssueItemState.StatusChanged);
                }

                // New Assignee / Assignment Changed
                int userBeforeID = item.ParentList.ParentWeb.EnsureUser(item["AssignedTo"].ToString().Split('#')[1]).ID;
                int userAfterID = item.ParentList.ParentWeb.EnsureUser(properties.AfterProperties["AssignedTo"].ToString().Split('|')[1]).ID;
                newAssigneeUser = item.ParentList.ParentWeb.SiteUsers.GetByID(userAfterID);

                if (userBeforeID != userAfterID)
                {
                    SendNotification(item, newAssigneeUser, IssueItemState.NewAssigned);
                }
            }
            catch (Exception ex) { }
        }

        /// <summary>
        /// An item was added.
        /// </summary>
        public override void ItemAdded(SPItemEventProperties properties)
        {
            base.ItemAdded(properties);
            SendNotification(properties.ListItem, null, IssueItemState.NewAssigned);
        }

        /// <summary>
        /// An item was updated.
        /// </summary>
        public override void ItemUpdated(SPItemEventProperties properties)
        {
            base.ItemUpdated(properties);
        }

        protected void SendNotification(SPListItem item, SPUser newAssigneeUser, IssueItemState issueItemState)
        {
            SPUser user;
            string subject = string.Empty;
            string from = "donotreply@myEmail.co.in";
            string sendTo = string.Empty;
            string cc = string.Empty;
            string header = string.Empty;
            string bodyText = string.Empty;
            string body = string.Empty;
            string footer = string.Empty;
           var itemDispFormUrl = item.ParentList.ParentWeb.Site.MakeFullUrl(item.ParentList.DefaultDisplayFormUrl) + "?ID=" + item.ID;
            string link = @"<a href=" + itemDispFormUrl + ">Click here</a>";

            if (newAssigneeUser != null)
            {
                user = newAssigneeUser;
            }
            else
            {
                user = item.ParentList.ParentWeb.EnsureUser(item["AssignedTo"].ToString().Split('#')[1]);
            }
            sendTo = user.Email;
            SPWeb thisWeb = item.ParentList.ParentWeb;

            switch (issueItemState)
            {
                case IssueItemState.NewAssigned:

                    //Set email subject
                    subject = "New Task is assigned to you.";
                    header = "Hi " + user.Name + ",";
                    bodyText = "A new Task has been assigned to you.<br /><b>Issue Title:</b> " + item["Title"].ToString() + "<br /><b>Issue ID:</b> " + item.ID + "<br /><b>Status:</b> " + item["Issue Status"] + "<br />" + link + "to view."+ "<br />";
                    footer = "Thank you" + "<br />" + "\n (It is an auto generated email. Please do not reply.)";

                    break;

                case IssueItemState.StatusChanged:

                    subject = "Task Status of '" + item["Title"] + "' has been changed.";
                    header = "Hi " + user.Name + ",";
                    bodyText = "Task Status has been changed.<br /><b>Issue Title:</b> " + item["Title"].ToString() + "<br /><b>Issue ID:</b> " + item.ID + "<br /><b>Status:</b> " + item["Issue Status"] + "<br />" + link  +"to view." + "<br />";
                    footer = "Thanks you" + "<br />" + "\n (Note: It is an auto generated email. Please do not reply.)";

                    break;

                default:
                    break;
            }

            body = string.Format("{0} <br /><br /><{1}<br /><br />{2}", header, bodyText, footer);
            bool success = SPUtility.SendEmail(thisWeb, true, false, sendTo, subject, body);

        }


-----------------------------------Event end---------------------------------------------


Register event to the Issue Trackers list:  



Deploy Feature scoped to Web
Open an element.xml file under your projects event receiver.
Replace/Change ListUrl instead of TemplateID

<Receivers ListUrl="Lists/DevIssues">


No comments:

Post a Comment