using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Workflow;
using Microsoft.SharePoint.Utilities;
using System.Collections;
using System.Globalization;
namespace WSPBuilderCOA
{
public partial class EditTask : System.Web.UI.UserControl
{
private string url;
public string Url
{
set
{
url = value;
}
get
{
return url;
}
}
protected void Page_Load(object sender, EventArgs e)
{
//this.lblMensaje = "";
}
protected void btnAprobar_Click(object sender, EventArgs e)
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite site = new SPSite(url))
{
using (SPWeb web = site.OpenWeb())
{
web.AllowUnsafeUpdates = true;
try
{
SPList task = web.Lists["Tareas"];
SPListItem item = task.Items.GetItemById(Convert.ToInt32(Request.Params["ID"]));
if (item["WorkflowListId"] != null)
{
Guid sourceListID = new Guid(item["WorkflowListId"].ToString());
SPList sourceList = web.Lists.GetList(sourceListID, true);
int sourceListItemID = Convert.ToInt32(item["WorkflowItemId"]);
SPListItem sourceListItem = sourceList.GetItemById(sourceListItemID);
//El primer aprobador realiza la tarea y cambia el estado del ítem a Aprobado en Primera Instancia
if (sourceListItem["Aprobador"].ToString() == "Aprobador 1" && Convert.ToString(sourceListItem["Estado"]) == "En Revisión")
{
sourceListItem["Estado"] = "Aprobado en Primera Instancia";
sourceListItem["Aprobador"] = "Aprobador 1";
sourceListItem.Update();
//Get the workflow instance id from Task item
Guid taskWorkflowInstanceID = new Guid(item["WorkflowInstanceID"].ToString());
SPWorkflow workflow = item.Workflows[taskWorkflowInstanceID];
SPWorkflowTask wfTask = workflow.Tasks[item.UniqueId];
Hashtable ht = new Hashtable();
ht[SPBuiltInFieldId.Completed] = "TRUE";
ht["Completed"] = "TRUE";
ht[SPBuiltInFieldId.PercentComplete] = 1.0f;
ht["PercentComplete"] = 1.0f;
ht["Status"] = "Completed";
ht[SPBuiltInFieldId.TaskStatus] = SPResource.GetString(new CultureInfo((int)wfTask.Web.Language, false), Strings.WorkflowStatusInProgress, new object[0]);
ht[SPBuiltInFieldId.WorkflowOutcome] = "Approved";
ht["TaskStatus"] = "Approved";
ht["FormData"] = SPWorkflowStatus.InProgress;
SPWorkflowTask.AlterTask((wfTask as SPListItem), ht, true);
Response.Redirect(Request.Params["Source"]);
}
else
{
if (sourceListItem["Aprobador"].ToString() == "Aprobador 1" && Convert.ToString(sourceListItem["Estado"]) == "Aprobado en Primera Instancia")
{
sourceListItem["Estado"] = "Aprobado en Segunda Instancia";
sourceListItem["Aprobador"] = "Aprobador 2";
sourceListItem.Update();
//Get the workflow instance id from Task item
Guid taskWorkflowInstanceID = new Guid(item["WorkflowInstanceID"].ToString());
SPWorkflow workflow = item.Workflows[taskWorkflowInstanceID];
SPWorkflowTask wfTask = workflow.Tasks[item.UniqueId];
Hashtable ht = new Hashtable();
ht[SPBuiltInFieldId.Completed] = "TRUE";
ht["Completed"] = "TRUE";
ht[SPBuiltInFieldId.PercentComplete] = 1.0f;
ht["PercentComplete"] = 1.0f;
ht["Status"] = "Completed";
ht[SPBuiltInFieldId.TaskStatus] = SPResource.GetString(new CultureInfo((int)wfTask.Web.Language, false), Strings.WorkflowStatusInProgress, new object[0]);
ht[SPBuiltInFieldId.WorkflowOutcome] = "Approved";
ht["TaskStatus"] = "Approved";
ht["FormData"] = SPWorkflowStatus.InProgress;
SPWorkflowTask.AlterTask((wfTask as SPListItem), ht, true);
Response.Redirect(Request.Params["Source"]);
}
}
}
}
catch (Exception ex)
{
//lblMensaje.Text = "Ha ocurrido un error aprobando: " + ex.Message + " - " + ex.Source;
}
finally
{
web.AllowUnsafeUpdates = false;
}
}
}
});
}
protected void btnRechazar_Click(object sender, EventArgs e)
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite site = new SPSite(url))
{
using (SPWeb web = site.OpenWeb())
{
web.AllowUnsafeUpdates = true;
try
{
SPList task = web.Lists["Tareas"];
SPListItem item = task.Items.GetItemById(Convert.ToInt32(Request.Params["ID"]));
if (item["WorkflowListId"] != null)
{
Guid sourceListID = new Guid(item["WorkflowListId"].ToString());
SPList sourceList = web.Lists.GetList(sourceListID, true);
int sourceListItemID = Convert.ToInt32(item["WorkflowItemId"]);
SPListItem sourceListItem = sourceList.GetItemById(sourceListItemID);
//El primer aprobador realiza la tarea y cambia el estado del ítem a Aprobado en Primera Instancia
if (sourceListItem["Aprobador"].ToString() == "Aprobador 1" && Convert.ToString(sourceListItem["Estado"]) == "En Revisión")
{
sourceListItem["Estado"] = "Rechazado";
sourceListItem["Aprobador"] = "Aprobador 1";
sourceListItem.Update();
//Get the workflow instance id from Task item
Guid taskWorkflowInstanceID = new Guid(item["WorkflowInstanceID"].ToString());
SPWorkflow workflow = item.Workflows[taskWorkflowInstanceID];
SPWorkflowTask wfTask = workflow.Tasks[item.UniqueId];
Hashtable ht = new Hashtable();
ht[SPBuiltInFieldId.Completed] = "TRUE";
ht["Completed"] = "TRUE";
ht[SPBuiltInFieldId.PercentComplete] = 1.0f;
ht["PercentComplete"] = 1.0f;
ht["Status"] = "Completed";
ht[SPBuiltInFieldId.TaskStatus] = SPResource.GetString(new CultureInfo((int)wfTask.Web.Language, false), Strings.WorkflowStatusInProgress, new object[0]);
ht[SPBuiltInFieldId.WorkflowOutcome] = "Approved";
ht["TaskStatus"] = "Approved";
ht["FormData"] = SPWorkflowStatus.InProgress;
SPWorkflowTask.AlterTask((wfTask as SPListItem), ht, true);
Response.Redirect(Request.Params["Source"]);
}
else
{
if (sourceListItem["Aprobador"].ToString() == "Aprobador 1" && Convert.ToString(sourceListItem["Estado"]) == "Aprobado en Primera Instancia")
{
sourceListItem["Estado"] = "Rechazado";
sourceListItem["Aprobador"] = "Aprobador 2";
sourceListItem.Update();
//Get the workflow instance id from Task item
Guid taskWorkflowInstanceID = new Guid(item["WorkflowInstanceID"].ToString());
SPWorkflow workflow = item.Workflows[taskWorkflowInstanceID];
SPWorkflowTask wfTask = workflow.Tasks[item.UniqueId];
Hashtable ht = new Hashtable();
ht[SPBuiltInFieldId.Completed] = "TRUE";
ht["Completed"] = "TRUE";
ht[SPBuiltInFieldId.PercentComplete] = 1.0f;
ht["PercentComplete"] = 1.0f;
ht["Status"] = "Completed";
ht[SPBuiltInFieldId.TaskStatus] = SPResource.GetString(new CultureInfo((int)wfTask.Web.Language, false), Strings.WorkflowStatusInProgress, new object[0]);
ht[SPBuiltInFieldId.WorkflowOutcome] = "Approved";
ht["TaskStatus"] = "Approved";
ht["FormData"] = SPWorkflowStatus.InProgress;
SPWorkflowTask.AlterTask((wfTask as SPListItem), ht, true);
Response.Redirect(Request.Params["Source"]);
}
}
}
}
catch (Exception ex)
{
//lblMensaje.Text = "Ha ocurrido un error aprobando: " + ex.Message + " - " + ex.Source;
}
finally
{
web.AllowUnsafeUpdates = false;
}
}
}
});
}
}
}