locked
in Item added event recver how to read or fetch column values RRS feed

  • Question

  • Hello All,

    in Item added event recver how to read or  fetch column values

    using (SPSite spSite = new SPSite("http://172.20/Collaboration/Lists"))
                                      {
                                          using (SPWeb spWeb = spSite.OpenWeb())
                                          {
                                              SPList spList = spWeb.Lists["SOR Response_SCG"];

    After this how to save column value in variable?

    Plz let me know.

    Regards,


    sudhanshu sharma Do good and cast it into river :)
    Monday, January 23, 2012 9:14 AM

Answers

  • Ok..

    I think you have a people picker field.. for "Mail To" right?

    You need to do the following to get the people picker value..

     if(item["Mail To"] != null)
    {
    SPFieldUserValue userValue = new SPFieldUserValue(web,
    item["Mail To"].ToString()); var = userValue.User.Email; }

    If you are expecting more than one users in the "Mail To" field then you need to use SPFieldUserValueCollection.

    SPFieldUserValueCollection  userValues = new SPFieldUserValueCollection
    (web, item["Mail To"].ToString()); foreach(SPFieldUserValue userVal in userValues ) mailMsg.CC.Add(userVal.User.Email);

     


    Priyanka
    Monday, January 23, 2012 12:59 PM

All replies

  • Hi,

    I assume you have attached the event handler on the required list using Feature. For more details see this.

    To read the column value in ItemAdded , you can simply use the below code  

    	SPListItem item = properties.ListItem;
     	if(item["Column Name"] != null)
        		string testvalue = item["Column Name"].ToString();<br/>
    

    If you just want to read the values of the fields and then do something then you can do it in ItemAdded. However, if you want to change the field value while adding the item, i suggest you do it in ItemAdding Event. ItemAdding Event will allow you to change the field value before it is saved to the database.

     

    Regards,


    Priyanka
    • Proposed as answer by Sandip Patil Monday, January 23, 2012 10:38 AM
    • Unproposed as answer by sudhanshuSharma Monday, January 23, 2012 10:53 AM
    Monday, January 23, 2012 9:46 AM
  • HI Priyanka,

    Thanks for your help see i have fetched these list values by-

      SPListItem item = properties.ListItem;
                                      string var=null;
                                       if(item["Mail To"] != null)
                                       var = item["Mail To"].ToString();

    after that when i use this var in next lines i got exception-

      mailMsg.To.Add("sudhanshu.sharma@lntinfotech.com");
                                                  mailMsg.CC.Add(var);//Getting exception here?

    I have already try by this code but problem is i get some garbage values with email ids.

     

     


    sudhanshu sharma Do good and cast it into river :)
    Monday, January 23, 2012 11:00 AM
  • Hi,

     

    MailMessage.CC.Add expects a input parameter of type MailAddress.

    Thus, add the below code instead of just  mailMsg.CC.Add(var); line

    MailAddress copyMailTo = new MailAddress(var);
    mailMsg.CC.Add(copyMailTo);
    
    

    Refer http://msdn.microsoft.com/en-us/library/system.net.mail.mailmessage.cc.aspx 


    Priyanka
    Monday, January 23, 2012 11:45 AM
  • Hi Priyanka,

    I am getting build success

    Please see complete code-

     

    SPListItem item = properties.ListItem;
                                      string var=null;
     	                              if(item["Mail To"] != null)
        		                       var = item["Mail To"].ToString();
                                    
    
                                           
                                            MailMessage mailMsg = new MailMessage();
                                         
                                                mailMsg.From = new MailAddress("temg-helpdesk@lntinfotech.com");
                                                 mailMsg.To.Add("sudhanshu.sharma@lntinfotech.com");
                                                  mailMsg.CC.Add(var);
                                                  mailMsg.Body = "New Item Created ....";
                                            SmtpClient client = new SmtpClient("112.22.8.102", 95);
                                            client.Send(mailMsg);
    


    Only for CC i want that value which i fill in list Mail To(people and group) column i am getting all values but like this-

    "5253;#Sudhanshu Sharma;#5264;#Amruta Jamdade;#5257;#Harikasai Pothamsetty"

    these are cuming with sharepoint Ids

    Now let me know for more suggestions.

    Thanks:)

     

     


    sudhanshu sharma Do good and cast it into river :)
    Monday, January 23, 2012 12:02 PM
  • Ok..

    I think you have a people picker field.. for "Mail To" right?

    You need to do the following to get the people picker value..

     if(item["Mail To"] != null)
    {
    SPFieldUserValue userValue = new SPFieldUserValue(web,
    item["Mail To"].ToString()); var = userValue.User.Email; }

    If you are expecting more than one users in the "Mail To" field then you need to use SPFieldUserValueCollection.

    SPFieldUserValueCollection  userValues = new SPFieldUserValueCollection
    (web, item["Mail To"].ToString()); foreach(SPFieldUserValue userVal in userValues ) mailMsg.CC.Add(userVal.User.Email);

     


    Priyanka
    Monday, January 23, 2012 12:59 PM
  • Thanks Priyanka Great Help:)

     


    sudhanshu sharma Do good and cast it into river :)
    Monday, January 23, 2012 1:50 PM