Friday, April 6, 2012

Send Email in Sandbox Solution

In sandbox solution SPUtility.SendMail method has been blocked explicitly. So there is no way to send email using code in Sandbox solution.

I googled alot but not able to find any solution. Then I decided to try something and it worked :)

  • 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.

Steps in detail are as below:
  1. 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)
  2. Deploy this project.
  3. Open SharePoint Designer and open sharePoint Root web.Note: Always create Reusable Workflow at top level site.
  4. Click Reusable Workflow.
  5. On click will open up another window. Give Name and in Content type select your content type deployed in step 1.
  6. Under Actions > Core Actions > Send an Email
  7. Click "these users"
  8. For To field value click lookup > double click "Workflow Lookup for a user"
  9. Select Datasource = "Current Item"Field from source = "EmailTo"

  10. Similarly define CC, Subject and body as shown below.
  11. Click Save.
  12. 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.
  13. Select file and in Ribbon click "Export File". And save this file to desktop.
  14. Now Open Visual Studio and create new Project using template "Import SharePoint Solution Package"
  15. On next screen enter your site url and deploy as Sandbox as shown below. Click Next.
  16. Select the template created in step 12. And click Next.
  17. On next screen two items will shown up select both and click finish.
  18. You will notice one feature create for you.
  19. Add new Event receiver to feature
  20. 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;
                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 =
                association.AllowManual = true;
                association.AutoStartCreate = true;
                association.AutoStartChange = false;

For more detail on Sandbox Solution Visit Here


  1. Hi Anmol, is it possible to also send attachments using this method ?

    thank you.

  2. hi,

    When I have tried to deploy the sandbox solution which we have created in last step it is giving me error:

    Error occurred in deployment step 'Add Solution': Sandboxed code execution request failed.

    Please help to resolve this.

    1. Can you please provide some more detail about the error

  3. Is this solution going to work with office 365-P1 plan? Within your code I don't see event receiver code you wrote in above. So where does this code go?

  4. Thanks for taking the time to discuss this, I feel strongly that love and read more on this topic. If possible, such as gain knowledge, would you mind updating your blog with additional information? It is very useful for me. seo expert


Popular Posts