Pages

Sunday, June 21, 2015

Get Current Loggin User in SharePoint Programmatically

Get current user using SharePoint SPControl class.

private SPUser GetLoginUser()
{
    SPUser currentUser = null;
    try
    {
        currentUser = SPControl.GetContextWeb(Context).CurrentUser;
    }
    catch (Exception)
    {
        throw;
    }
    return currentUser;
}

Saturday, June 13, 2015

Programmatically Send Mails using SPUtility SharePoint

public bool SendEmail(string To, string cc, string subject, string msg)
{
            bool status = true;

            string sendTo = To;
            string CCTo = cc;

            try
            {
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    using (SPSite oSpSite = new SPSite(SPContext.Current.Web.Url))
                    {
                        oSpSite.AllowUnsafeUpdates = true;
                        using (SPWeb oSPWeb = oSpSite.OpenWeb())
                        {
                            oSPWeb.AllowUnsafeUpdates = true;
                            if (SPUtility.IsEmailServerSet(oSPWeb))
                            {
                                StringDictionary headers = new StringDictionary();

                                headers.Add("to", sendTo);
                                headers.Add("cc", CCTo);
                                headers.Add("subject", subject);
                                headers.Add("fAppendHtmlTag", "True");

                                System.Text.StringBuilder strMessage = new System.Text.StringBuilder();

                                strMessage.Append(msg);
                                status = SPUtility.SendEmail(oSPWeb, headers, strMessage.ToString());
                            }
                            oSPWeb.AllowUnsafeUpdates = false;
                        }
                        oSpSite.AllowUnsafeUpdates = false;
                    }
                });
                return status;
            }
            catch (Exception ex)
            {
                this.Print("SendBulkEmail", ex.Message);
                return false;
            }
}