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();

}

Send email 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




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

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



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

web.Dispose();

}

send mail code

#region
sendmail

public
static bool SendMail(SPWeb web, string Subject, string Body, string To, string strFrom)

{

bool mailSent = false;

try

{

string smtpServer = SPAdministrationWebApplication.Local.OutboundMailServiceInstance.Server.Address;

MailMessage mailMessage = new MailMessage(strFrom, To, Subject, Body);

mailMessage.IsBodyHtml = true;

SmtpClient smtpClient = new SmtpClient(smtpServer);

smtpClient.Send(mailMessage);

}

catch (Exception)

{

return mailSent;

}

return mailSent;

}

#endregion


 
 

code with Form digest enabled for adding users to groups

#region
add permission to GRM and Audit Users





//Guid SiteId = SPContext.Current.Site.ID;

//Guid WebId = SPContext.Current.Web.ID;



//SPSecurity.RunWithElevatedPrivileges(delegate()

//{






// //using (SPSite ElevatedSite = new SPSite("http://autosdr-ist.bns/sdr/13/default.aspx"))

// using (SPSite ElevatedSite = new SPSite(_sFilePath))

//// using (SPSite ElevatedSite = new SPSite(SiteId))

//{

// using (SPWeb ElevatedWeb = ElevatedSite.OpenWeb())

// //using (SPWeb ElevatedWeb = ElevatedSite.OpenWeb(WebId))

// {






// SPGroup ngroup = ElevatedWeb.SiteGroups[System.Configuration.ConfigurationSettings.AppSettings["GRMgroup"].ToString()];

// SPGroup auditgroup = ElevatedWeb.SiteGroups[System.Configuration.ConfigurationSettings.AppSettings["AuditUsersgroup"].ToString()];

// ElevatedWeb.AllowUnsafeUpdates = true;

// ElevatedSite.AllowUnsafeUpdates = true;

// ElevatedWeb.Update();

// //SPUtility.ValidateFormDigest();

// SPFolder ofolder = null;

// SPFolder seconsignaturefolder09 = OtherCreaditFolder.SubFolders.Add("1-Signatures");

// SPFolder thirdsignaturefolder09 = overrunfolder.SubFolders.Add("1-Signatures");//added by christine//added by christine

// ofolder = firstsignaturefolder.SubFolders.Add("1-Signatures");

// ofolder.Item.BreakRoleInheritance(false);

// //ElevatedWeb.AllowUnsafeUpdates = true;

// seconsignaturefolder09.Item.BreakRoleInheritance(false);

// //ElevatedWeb.AllowUnsafeUpdates = true;

// thirdsignaturefolder09.Item.BreakRoleInheritance(false);

// ElevatedWeb.AllowUnsafeUpdates = true;

// //Role Assignments and Definition order changed by srikar 13-June-2013

// SPRoleAssignment roleAssignmentaudituserss = new SPRoleAssignment(auditgroup);

// SPRoleDefinition roleDefination = ElevatedWeb.RoleDefinitions["Read"];

// SPRoleAssignment roleAssignment = new SPRoleAssignment(ngroup);

// SPRoleDefinition grmroleDefination = ElevatedWeb.RoleDefinitions["Contribute"];

// roleAssignmentaudituserss.RoleDefinitionBindings.Add(roleDefination);

// roleAssignment.RoleDefinitionBindings.Add(grmroleDefination);

// Microsoft.SharePoint.Administration.SPWebApplication webApp = ElevatedWeb.Site.WebApplication;

// webApp.FormDigestSettings.Enabled = false;

// //roleAssignmentaudituserss.RoleDefinitionBindings.Add(roleDefination);

// //roleAssignment.RoleDefinitionBindings.Add(grmroleDefination);

// //int i = ofolder.Item.RoleAssignments.Count;

// //if(i>0)

// //{




// ofolder.Item.RoleAssignments.Add(roleAssignmentaudituserss);

// ofolder.Item.RoleAssignments.Add(roleAssignment);

// seconsignaturefolder09.Item.RoleAssignments.Add(roleAssignmentaudituserss);

// seconsignaturefolder09.Item.RoleAssignments.Add(roleAssignment);

// thirdsignaturefolder09.Item.RoleAssignments.Add(roleAssignmentaudituserss);

// thirdsignaturefolder09.Item.RoleAssignments.Add(roleAssignment);



// //}

// ofolder.Update();

// seconsignaturefolder09.Update();

// thirdsignaturefolder09.Update();

//ElevatedWeb.AllowUnsafeUpdates = false;

//webApp.FormDigestSettings.Enabled = true;

// }

// }

//});




#endregion

Assign permission to groups programtically

#region
add signaturefolder

//Buttonimg_Click

protected void Buttonimg_Click(object sender, EventArgs e)



{

SPWeb oWeb1 = SPContext.Current.Web;

SPSecurity.RunWithElevatedPrivileges(delegate()

{

using (SPSite oSite = new SPSite(oWeb1.Site.Url))

{



using (SPWeb oWeb = oSite.OpenWeb())

{

//get the curent folder where search is happening

string siteUrl = Request.Url.ToString();

string region = SPContext.Current.List.Title;

//"Atlantic Region Client Portfolios";

SPDocumentLibrary docLib = (SPDocumentLibrary)oWeb.Lists[region];



foreach (SPFolder subfoldername in docLib.RootFolder.SubFolders)

{

foreach (SPFolder nsub in subfoldername.SubFolders)

{

if ((nsub.Name == foldername) || (nsub.Name == alternateurl) || (nsub.Name == foldernameaftertrim) || (nsub.Name == foldernameaftertrimurl))

{

foreach (SPFolder clientfolder in nsub.SubFolders)

{

string names = clientfolder.Name.ToString();

string name = "1-Annual Review and New Deals";

string folder2 = "2-Credit Correspondence";

String folder3 = "3-Overruns";

string s = clientfolder.Item.Name;

if ((name == names) || (folder2 == names) || (folder3 == names))

{

SPFolder ofolder = null;

oWeb.AllowUnsafeUpdates = true;

ofolder = clientfolder.SubFolders.Add("1-Signatures");

//modified by christine on 11 June 2013

#region
code for adding permission

//clientfolder.SubFolders.Delete("1-Signatures");

SPGroup ngroup = oWeb1.SiteGroups[System.Configuration.ConfigurationSettings.AppSettings["GRMgroup"].ToString()];

SPGroup auditgroup = oWeb1.SiteGroups[System.Configuration.ConfigurationSettings.AppSettings["AuditUsersgroup"].ToString()];



oWeb.AllowUnsafeUpdates =
true;

ofolder.Item.BreakRoleInheritance(false);

oWeb.AllowUnsafeUpdates = true;



//Role Assignments and Definition order changed by srikar 13-June-2013

SPRoleAssignment roleAssignmentGRM = new SPRoleAssignment(ngroup);

SPRoleAssignment roleAssignmentaudituserss = new SPRoleAssignment(auditgroup);

SPRoleDefinition roleDefinationAudit = oWeb.RoleDefinitions["Read"];

SPRoleDefinition roleDefinationGRM = oWeb.RoleDefinitions["Contribute"];

roleAssignmentGRM.RoleDefinitionBindings.Add(roleDefinationGRM);

roleAssignmentaudituserss.RoleDefinitionBindings.Add(roleDefinationAudit);

ofolder.Item.RoleAssignments.Add(roleAssignmentGRM);

ofolder.Item.RoleAssignments.Add(roleAssignmentaudituserss);

oWeb.AllowUnsafeUpdates =
true;

ofolder.Update();

SPGroupCollection gc = oWeb.SiteGroups;

foreach (SPGroup sgrop in gc)

{

string gname = sgrop.ToString();

string grname = ngroup.ToString();

string aname = auditgroup.ToString();

if ((gname == grname) || (gname == aname))

{





}

else

{

ofolder.Item.RoleAssignments.Remove((
SPPrincipal)sgrop);

}

}

#endregion


docLib.Update();



//Page.Response.Redirect(HttpContext.Current.Request.Url.ToString(),true);




}//1st if

}//3rd

Page.Response.Redirect(HttpContext.Current.Request.Url.ToString(),true);

}//2nd

}



}

}
//forst for llop

}

});

}

# endregion