In sandbox solution SPUtility.SendMail method has been blocked explicitly. So there is no way to send email using code in Sandbox solution.
- What I did was i created Custom Content Type using Visual Studio.
- Then created a Send Email Workflow Activity on my custom Content Type using Sharepoint Designer.
- I saved that workflow as template and I exported that workflow to local Drive.
- And then created a new Project using template "Import SharePoint Solution Package".
- Then created another list programmatically using my custom content type.
- Then associated my workflow with my custom list.
- And then when ever I require to send an email. I just add records in my list. Workflow triggers and sends an email.
- Create Empty SharePoint Project in Visual Studio. Create custom content type "EmailHelperContentType" with following custom FieldsEmailTo (single line)EmailCC (Single Line)EmailBodyText (MultiLine)EmailSubjectText (Single Line)
- Deploy this project.
- Open SharePoint Designer and open sharePoint Root web.Note: Always create Reusable Workflow at top level site.
- Click Reusable Workflow.
- On click will open up another window. Give Name and in Content type select your content type deployed in step 1.
- Under Actions > Core Actions > Send an Email
- Click "these users"
- For To field value click lookup > double click "Workflow Lookup for a user"
- Select Datasource = "Current Item"Field from source = "EmailTo"
- Similarly define CC, Subject and body as shown below.
- Click Save.
- Click workflow under Navigation. Select your Resuable Workflow. And in Ribbon click "Save as Template". The template will be saved in Site Assets". Refresh if you can't see it there.
- Select file and in Ribbon click "Export File". And save this file to desktop.
- Now Open Visual Studio and create new Project using template "Import SharePoint Solution Package"
- On next screen enter your site url and deploy as Sandbox as shown below. Click Next.
- Select the template created in step 12. And click Next.
- On next screen two items will shown up select both and click finish.
- You will notice one feature create for you.
- Add new Event receiver to feature
- In feature Activated Create new list " EmailHelper" and add Workflow association with list "EmailHelper" as show below.
string contentTypeName = "EmailHelperContentType";
string workflowName = "Email Sender";string workflowTemplateName = "EmailHelperWorkflow";
string listTaskName = "Tasks";
string listHistoryName = "WorkflowHistory";
string listEmailName = "EmailHelper";
SPContentType siteContentType = web.Site.RootWeb.ContentTypes[contentTypeName];
SPList historylist = web.Lists.TryGetList(listHistoryName);
if (historylist == null)
{
web.Lists.Add(listHistoryName, listHistoryName, SPListTemplateType.WorkflowHistory);
historylist = web.Lists.TryGetList(listHistoryName);
}
SPList tasklist = web.Lists.TryGetList(listTaskName);
if (tasklist == null)
{
web.Lists.Add(listTaskName, listTaskName, SPListTemplateType.Tasks);
tasklist = web.Lists.TryGetList(listTaskName);
}
SPList mainlist = web.Lists.TryGetList(listEmailName);
if (mainlist == null)
{
web.Lists.Add(listEmailName, listEmailName, SPListTemplateType.GenericList);
mainlist = web.Lists.TryGetList(listEmailName);
mainlist.Hidden = true;
mainlist.ContentTypesEnabled = true;
mainlist.ContentTypes.Add(siteContentType);
mainlist.ContentTypes.Delete(mainlist.ContentTypes["Item"].Id);
SPWorkflowTemplate workflowTemplate = null;
foreach (SPWorkflowTemplate template in web.WorkflowTemplates)
{
workflowTemplate = template;
// We'll take a template everyone has.
if (workflowTemplate.Name == workflowTemplateName) break;
}
SPWorkflowAssociation association =
SPWorkflowAssociation.CreateListContentTypeAssociation(workflowTemplate,
workflowName,
tasklist,
historylist);
association.AllowManual = true;
association.AutoStartCreate = true;
association.AutoStartChange = false;
mainlist.WorkflowAssociations.Add(association);
mainlist.Update();
For more detail on Sandbox Solution Visit Here
Comments