Sending email when user click a specified button
Question
I'm a newbie of SharePoint 2010.
I would like to send an email when a user click on a specified custom button relative to an element in a list.
I would like to add into the body of the email some informations about the selected element like the column "Title" or the "Author".
I've found this code to send email and I've copied it into an .aspx page. This page also receive one parameter by querystring. The parameter is "list_name" that specifies the name of the list where button is clicked. I think this could be used to specify the SP list somewhere in the code, but I don't know exactly where and how do it.
StringDictionary headers = new StringDictionary();
string bodyTxt = "<b>Test Mail Bold</b>"; //for html mail body, you can send unformatted text too…
headers.Add("to", "mailadressfor_cc@domainname.com");
headers.Add("from", "mailadress_from@domainname.com");
headers.Add("subject", "Richiesta scambio prenotazione");
headers.Add("content-type", "text/html"); //for html mail body
SPUtility.SendEmail(properties.web, headers, bodyTxt);
But my (probably stupid) question is: How can I retrieve data from the list and use it in this .aspx page that sends the email?
Thanks!
Solution
Since you have the list name in the query string use it to get SPList as below
SPWeb web = SPContext.Current.Web;
SPList list = web.GetListFromUrl(web.Url + “/Lists/ListName/Forms/AllItems.aspx“);
Then you can get properties from list.
Example:
String listTitle = list.Title;
If you want to get properties from a list item, you can pass item’s ID in the query string too, and get the item as below:
SPListItem item = list.GetItemByID(id);
String itemTitle = item.Title;
String itemFieldValue = item[“fieldname”] == null ? null : item[“fieldname”].ToString();