Thursday, January 26, 2012

Use email in SharePoint 2010 Sandbox

In SharePoint 2010 Sandbox. In Sandbox Microsoft.SharePoint.Utilitiesis not avaliable. When tried to use System.Net.Mail methods available to us. SmtpClient constructor give me error.
I found another way using SharePoint Designer workflow. You can trigger a workflow to run programmatically and also supply information to the workflow and set the initiation values. Here's the code to start a workflow:


///
/// Method to handle starting the appropriate Workflow as specified by the Site Collection Workflow Name property
///

private void StartWorkflow()
{
SPWorkflowAssociation wfa = SPContext.Current.Web.WorkflowAssociations.GetAssociationByName(this.SiteCollectionWorkflowName, CultureInfo.InvariantCulture);
wfa.AssociationData = this.GetWorkflowInitiationData();
SPContext.Current.Site.WorkflowManager.StartWorkflow(null, wfa, this.GetWorkflowInitiationData(), SPWorkflowRunOptions.Asynchronous);
}


///
/// Method to construct the Workflow Initiation/Association Data
///

/// A string containing the XML schema and values necessary for the Association Data
private String GetWorkflowInitiationData()
{
String schema = "http://www.w3.org/2001/XMLSchema\" " +
"xmlns:dms=\"http://schemas.microsoft.com/office/2009/documentManagement/types\" " +
"xmlns:dfs=\"http://schemas.microsoft.com/office/infopath/2003/dataFormSolution\" " +
"xmlns:q=\"http://schemas.microsoft.com/office/infopath/2009/WSSList/queryFields\" " +
"xmlns:d=\"http://schemas.microsoft.com/office/infopath/2009/WSSList/dataFields\" " +
"xmlns:ma=\"http://schemas.microsoft.com/office/2009/metadata/properties/metaAttributes\" " +
"xmlns:pc=\"http://schemas.microsoft.com/office/infopath/2007/PartnerControls\" " +
"xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">" +
"{0}" +
"
";



StringBuilder sb = new StringBuilder();
sb.AppendFormat("{0}", this.SendToEmailAddress);
sb.AppendFormat("{0}", this._subject.Text);
sb.AppendFormat("{0}", this._comments.Text);
sb.AppendFormat("{0}", this._email.Text);
sb.AppendFormat("{0}", this.AutoResponseSubject);
sb.AppendFormat("{0}", this.AutoResponseText);
return String.Format(schema, sb.ToString());
}




1 comment:

Julia said...

Nice code sample, thank you for posting.