Sunday, April 7, 2013

Share Feedback WebPart


Hi,

There was a requirement on sharing the feedback on the current page. For this web part has been developed and placed on the master page as below

For this, list has been setup  where Email Subject, To Field will be taken as inputs to compose the mail as below.

In the list, Email Subject field just hold 0.this has to been replaced with Current Page Name.
Below is the code that has been written so that user clicks on ‘ShareFeedBack’ link, new email will be opened in the outlook to share feedback on current page.

using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;

using System.Globalization;
using AZ.SharePoint.Common.Logging;
using Microsoft.Practices.SharePoint.Common.Logging;
using Microsoft.Practices.SharePoint.Common.ServiceLocation;


namespace SharePoint.MLZ.ShareFeedback.ShareFeedback
{
    public partial class ShareFeedbackUserControl : UserControl
    {
        #region Private Variables       
        string strEmailAddress = string.Empty;
        string strSubject = string.Empty;
        string strTo = string.Empty;
        /// <summary>
        /// Initialises logger object
        /// </summary>
        private Microsoft.Practices.SharePoint.Common.Logging.ILogger logger = SharePointServiceLocator.GetCurrent().GetInstance<Microsoft.Practices.SharePoint.Common.Logging.ILogger>();
        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            SendSupportEmail();
        }

        #region Private Methods
        /// <summary>
        /// Create an Email to share FeedBack on current page
        /// </summary>
        /// <returns>DataTable</returns>
        private void SendSupportEmail()
        {
            try
            {
                string pageUrl = this.Page.Request.Url.ToString();
                string[] pageStrings = pageUrl.Split('/');
                using (SPSite site = new SPSite(SPContext.Current.Site.Url))
                {
                    using (SPWeb web = site.OpenWeb())
                    {
                        SPList objlist = web.Lists.TryGetList("EmailConfigurations");
                        SPQuery query = new SPQuery();
                        query.Query = "<Where><Eq><FieldRef Name='ComponentName'/><Value Type='Choice'>Share Feedback</Value></Eq></Where>";

                        SPListItemCollection itemcoll = objlist.GetItems(query);                       

                        foreach (SPListItem item in itemcoll)                      
                        {
                            if (item["EmailSubject"].ToString() != null && item["To"].ToString()!=null)                            
                            {
                                string subject = item["EmailSubject"].ToString();
                                strSubject = subject.Replace("{0}", pageStrings[pageStrings.Length - 1].Split('.')[0].ToString());
                                strTo = item["To"].ToString();
                            }
                        }
                        SPGroup grp = web.SiteGroups[strTo];
                        foreach (SPUser user in grp.Users)
                        {
                            strEmailAddress = string.Concat(strEmailAddress, user.Email, ";");
                        }
                        AnchorShareFeedback.HRef = string.Concat("mailto:", strEmailAddress, "?subject=", strSubject);
                    }
                }
            }
            catch (ArgumentException ex)
            {
                ////Log to ULS and Windows Event Logs
                this.logger.LogToOperations(ex, "Failed to Send an Email"Convert.ToInt32(EventLogEventId.TimerJobEvent, CultureInfo.CurrentCulture), Microsoft.SharePoint.Administration.EventSeverity.Error, null);
            }
            catch (InvalidOperationException ex)
            {
                ////Log to ULS and Windows Event Logs
                this.logger.LogToOperations(ex, "Failed to Send an Email"Convert.ToInt32(EventLogEventId.TimerJobEvent, CultureInfo.CurrentCulture), Microsoft.SharePoint.Administration.EventSeverity.Error, null);
            }
            catch (Exception ex)
            {
                ////Log to ULS and Windows Event Logs
                this.logger.LogToOperations(ex, "Failed to Send an Email"Convert.ToInt32(EventLogEventId.TimerJobEvent, CultureInfo.CurrentCulture), Microsoft.SharePoint.Administration.EventSeverity.Error, null);
            }



        }
        #endregion
    }
}



No comments:

Post a Comment