Wednesday, 21 October 2015

Add Key Value into Dictionary and get value based on key

Note: agilityItemDetailDto.OwnershipRoles // it's custom collection

//Add Key and value into dictionary
Dictionary<string, string> OwnershipRoleHolders = new Dictionary<string, string>();
            if (agilityItemDetailDto.OwnershipRoles != null)
            {
                OwnershipRoleHolders =
                agilityItemDetailDto.OwnershipRoles.Select(
                                r =>
                                    new
                                    {
                                        Key = r.OwnershipRoleSingleDisplayText,
                                        Value = String.Join(";", r.RoleHolders.Select(h => h.Title).ToArray())
                                    }).ToDictionary(d => d.Key, d => d.Value);

            }
           // string val2;
  //get Value based on key . For eg -OwnershipRoleHolders.ContainsKey("<Pass key name>")
            if (OwnershipRoleHolders.ContainsKey("Approver"))
            {
                string val1;
                OwnershipRoleHolders.TryGetValue("Approver", out val1);
                ApproverRoleTitle = val1;
            }

Friday, 16 October 2015

Send mail in Sharepoint.

using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Text;

namespace MultipleTasksSolution
{
    public class EmailUtility
    {
        public const String Gmail_SMTP_HostAddress = "smtp.gmail.com";
        public const int Gmail_SMTP_Port = 587;
        public const string Gmail_UserName = "*******@gmail.com"; // provide gmail user id
        public const string Gmail_ToAddress = "*******gmail.com";
        public const string Gmail_FromAddress = "*******@gmail.com";
        public const string Gmail_UserPassword = "********"; // provide your gmail password

       //Send mail by Gmail
        public static bool SendEmailUsingNet(string subject,string body)
        {
            MailMessage message = new MailMessage(Gmail_FromAddress, Gmail_ToAddress);
            message.Subject = subject;
            message.Body = body;
            message.IsBodyHtml = true;

            SmtpClient smtp = new SmtpClient();
            smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
            smtp.UseDefaultCredentials = false;
            smtp.EnableSsl = true;
            smtp.Host = Gmail_SMTP_HostAddress;
            smtp.Port = Gmail_SMTP_Port;

            NetworkCredential nc = new NetworkCredential(Gmail_UserName, Gmail_UserPassword);
            smtp.Credentials = nc;

            try
            {
                smtp.Send(message);
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }


        //Send mail Using Sharepoint
        public static bool ProcessRequest(string receiver, string sender, string subject, string body,string cc,string siteurl)
        {
            bool IsMailSent = false;
            if (string.IsNullOrEmpty(siteurl))
                siteurl = SPContext.Current.Web.Url;

            try
            {
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    using (SPSite currentSite = new SPSite(siteurl))
                    {
                        using (SPWeb currentWeb = currentSite.RootWeb)
                        {

                            StringDictionary headers = new StringDictionary();
                            headers.Add("to", receiver);
                            headers.Add("from", sender);
                            headers.Add("subject", subject);
                            headers.Add("content-type", "text/html");
                            headers.Add("cc", cc);
                            string bodyText = body;
                            SmtpClient smtp = new SmtpClient();
                            smtp.Host = "************";
                            IsMailSent = SPUtility.SendEmail(currentWeb, headers, bodyText);

                        }
                    }
                });
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return IsMailSent;
        }

    }
}

Refer Link

Tuesday, 6 October 2015

Get Users Email ID from people picker Using ECMA, ArrayOfDictionaryEntry XML

 var creatorclass = "PPlEditorCreatorsClass_" + listnametrim + "_" + idOnReject;
    var creatorval = $('.' + creatorclass).find('.ms-entity-resolved').attr('title');
    if (creatorval !== undefined && creatorval != "") {
        creatorval = creatorval.split('\\')[0] + '\\' + creatorval.split('\\')[1];
        var creator = SP.FieldUserValue.fromUser(creatorval);
        if ($('.' + creatorclass).find('.ms-entity-resolved').find('div').length > 1) {

            var Xml = $('.' + creatorclass).find('.ms-entity-resolved').find('div')[1].getAttribute('data');
            var doc = $.parseXML(Xml.replace(/\t/gi, '')),
            $xml = $(doc);
            var email = $xml.find('Key:contains(Email) + Value').text();
            creatoremail = email;

            var xxmmll = $('.' + creatorclass).find('.ms-entity-resolved').find('div')[0];
            creatorDisplayTextOnReject= $(xxmmll).attr("displaytext");
        }
    }



Refer link 1

Refer Link 2

Refer Link3