Monday, 1 July 2013

send mail workflow


string formLink = default(string);


string strSubject=string.Empty; // mail Subject


string strBody = string.Empty; ;//Mail body


string strToMailIDs = string.Empty; ;//mail To ID's


string strfrom = string.Empty; ;//Mail From ID's


int itaskID = Convert.ToInt32(onTaskCreated_BranchManger.AfterProperties.TaskItemId);//Getting current task ID


int iUserID = Convert.ToInt32(workflowProperties.Item["ID"]); // Getting the UserID behind the task ID

formLink = this.workflowProperties.Item.Web.Url + "/Lists/Task/DispForm.aspx?ID="+itaskID;

formLink = "<a href='" + formLink + "'>Click here</a>"; // Item approve/reject link




SPWeb web = this.workflowProperties.Item.Web;




//Updating the Task list title with task list ID


SPListItem lstItem = workflowProperties.TaskList.Items.GetItemById(itaskID);

web.AllowUnsafeUpdates = true;

lstItem["Title"] = "Branch Manager Aproval Request for ID " + itaskID;

lstItem[SPBuiltInFieldId.WorkflowVersion] = 1;

lstItem.SystemUpdate();

web.AllowUnsafeUpdates =
false;


//End Updating the Task list title with task list ID






//getting the Email Template from the list, ET001 code is for Branch manager


SPList lstEmailTemplpate = web.Lists[emailTemplateList];


SPList lstManageCustomers = web.Lists[manageCustomerList];


SPQuery queryMailTemplate = new SPQuery();

queryMailTemplate.Query = "<Where><Eq><FieldRef Name='EmailCode'/><Value Type='Text'>ET001</Value></Eq></Where>";


DataTable dtTemplate = lstEmailTemplpate.GetItems(queryMailTemplate).GetDataTable();


//End of getting the Email Template from the list, ET001 is for Branch manager






//Getting the User Information from manage customer list to fill in email body and subject


SPQuery queryCustomerInfo = new SPQuery();

queryCustomerInfo.Query = "<Where><Eq><FieldRef Name='ID'/><Value Type='Counter'>" + iUserID + "</Value></Eq></Where>";


DataTable dtCustomerInfo = lstManageCustomers.GetItems(queryCustomerInfo).GetDataTable();






if (dtTemplate != null && dtTemplate.Rows.Count > 0)

{

strSubject = dtTemplate.Rows[0][
"EmailSub"].ToString();

strSubject=strSubject.Replace("[ID]", itaskID.ToString());

strBody = dtTemplate.Rows[0]["EmailBody"].ToString();


if (dtCustomerInfo != null && dtCustomerInfo.Rows.Count > 0)

{



strBody = strBody.Replace(
"[CIFNum]", dtCustomerInfo.Rows[0]["CustomerCIF"].ToString());

strBody = strBody.Replace("[CName]", dtCustomerInfo.Rows[0]["FirstName"].ToString()+" "+dtCustomerInfo.Rows[0]["LastName"].ToString());

strBody = strBody.Replace("[TransitNum]", dtCustomerInfo.Rows[0]["CustomerTransit_x0023_"].ToString());

strBody = strBody.Replace("[CreatedBy]", dtCustomerInfo.Rows[0]["Author"].ToString());

strBody = strBody.Replace("[Clickhere]", formLink);


if (dtCustomerInfo.Rows[0]["AddCustomerFlag"].ToString()== "0")

{

strBody = strBody.Replace(
"[AddRemove]", dtCustomerInfo.Rows[0]["AddCustomer"].ToString());

strBody = strBody.Replace("[ReasonText]", dtCustomerInfo.Rows[0]["ReasonforAddingCustomer"].ToString());

}


else if (dtCustomerInfo.Rows[0]["AddCustomerFlag"].ToString()== "1")

{

strBody = strBody.Replace(
"[AddRemove]", dtCustomerInfo.Rows[0]["RemoveCustomer"].ToString());

strBody = strBody.Replace("[ReasonText]", dtCustomerInfo.Rows[0]["ReasonforRemovingCustomer"].ToString());

}



}



}


//End of Getting the User Information from manage customer list to fill in email body



strToMailIDs = BranchmangerworkflowtaskProperties.ExtendedProperties[
"ApprovalManager"].ToString();

strfrom = BranchmangerworkflowtaskProperties.ExtendedProperties["ApprovalManager"].ToString();



SendMail(web, strSubject, strBody, strToMailIDs, strfrom);
//sending email from branch manager

web.Dispose();

}

No comments:

Post a Comment