Good Morning,
I am using VS2013 to create simple web part to display latest listitems from different list and folders.I have added button to send same context in email.on button click i get below error
Web Part Error: Unhandled exception was thrown by the sandboxed code wrapper's Execute method in the partial trust app domain
here is the code
namespace test_Updates.test_Updates{
[ToolboxItemAttribute(false)]
public class test_Updates : WebPart
{
public Button btnSearch = new Button();
string txt1;
protected override void CreateChildControls()
{
base.CreateChildControls();
string currentSite = SPContext.Current.Web.Url;
//SPUserToken systemToken = new SPSite(currentSite).SystemAccount.UserToken;
using (SPSite site = new SPSite(currentSite))
{
using (SPWeb web = site.OpenWeb())
{
DateTime endDate = DateTime.Today;
DateTime startDate = endDate.AddDays(-7);
SPList list = web.Lists.TryGetList("Calendar");
SPQuery query = new SPQuery();
query.Query = "<OrderBy><FieldRef Name='Modified' Type='DateTime' IncludeTimeValue='TRUE' Ascending='False'/></OrderBy>";
query.RowLimit = 7;
SPListItemCollection itemCollection = list.GetItems(query);
if (itemCollection != null)
{
if (itemCollection.Count > 0)
{
txt1 = "<table><tr bgcolor=#0D8DD3><td><font color='#FFFFFF'>Title</td><td><font color='#FFFFFF'>Time</td></tr>";
foreach (SPListItem item in itemCollection)
{
txt1 = txt1 + "<tr><td>" + Convert.ToString(item["Title"]) + "</td><td> Start : "+ Convert.ToString(item["Start Time"]) + " - End : " + Convert.ToString(item["End Time"]) + " </td></tr>";
}
}
}
}
}
Controls.Add(new Label() { Text = txt1 });
btnSearch.Text = "Email test";
btnSearch.Click += new System.EventHandler(this.btnSearch_Click);
this.Controls.Add(btnSearch);
}
void btnSearch_Click(object sender, EventArgs e)
{
SPWeb web = SPContext.Current.Web;
bool appendHtmlTag = false;
bool htmlEncode = false;
string toAddress = "mike@mike.com";
string subject = "Subject";
string message = "Message text";
//bool result = SPUtility.SendEmail(web, appendHtmlTag, htmlEncode, toAddress, subject, message);
// SPSecurity.RunWithElevatedPrivileges(delegate()
// {
bool result = SPUtility.SendEmail(web, appendHtmlTag, htmlEncode, toAddress, subject, message);
// });
}
protected override void Render(HtmlTextWriter writer)
{
base.Render(writer);
}
}
}