Monday, 10 November 2014

Sharepoint Custom Workflow

// Method used to get the SharePoint List that the workflow is attached to and the list item that we are working on
private void getTaskListInformation()
{
// Get the Task List that we are attached to, by converting the Guid Parameter into a Guid
this._TaskListAttachedTo = this._myTeamSite.Lists[new Guid(this._paramSPListGuid)];
// Get the Task Item Object that the workflow has created
this._TaskListItem = this._TaskListAttachedTo.GetItemById(System.Convert.ToInt16(this._paramTaskListItemID));
string[] passedLink = _TaskListItem["Link"].ToString().Split(',');
listID = Convert.ToInt32(passedLink[0].Split('=')[1]); // Get the application request ID.
// Get the ID of the Workflow Instance that we are running with in
this._workflowInstanceGuid = new Guid(Convert.ToString(this._TaskListItem["WorkflowInstanceID"]));
// Instantiate the workflow
this._activeWorkflow = new SPWorkflow(this._TaskListItem, this._workflowInstanceGuid);
}
=============================
private void getTaskListInformationByListItem()
{
listID = Convert.ToInt32(Request.Params["ID"]);
SPSite site = SPContext.Current.Site;
using (SPWeb web = site.OpenWeb())
{
SPList JobRequest = this._myTeamSite.Lists["Request"];
SPListItem JobRequestItem = null;
if (listID > 0)
{
JobRequestItem = JobRequest.GetItemById(listID);
}
if (JobRequestItem != null)
{
SPListItemCollection taskListItems = JobRequestItem.Workflows[0].TaskList.Items;
foreach (SPListItem item in taskListItems)
{
string[] passedLink = item["Link"].ToString().Split(',');
if (Convert.ToString(item["Status"]) == "Not Started") // Get the Application request ID.
{
// Get the Task List that we are attached to, by converting the Guid Parameter into a Guid
this._TaskListAttachedTo = this._myTeamSite.Lists["Tasks"];
// Get the Task Item Object that the workflow has created
this._TaskListItem = this._TaskListAttachedTo.GetItemById(item.ID);
// Get the ID of the Workflow Instance that we are running with in
this._workflowInstanceGuid = new Guid(Convert.ToString(this._TaskListItem["WorkflowInstanceID"]));
// Instantiate the workflow
this._activeWorkflow = new SPWorkflow(this._TaskListItem, this._workflowInstanceGuid);
}
}
}
}
}
==================================================================
// Code that responds to the Reject Button Click event.
protected void btnReject_Click(object sender, EventArgs e)
{
if (isValid())
{
Hashtable taskHash = new Hashtable(); // Task data is sent via a Hash Table
taskHash["_ApporvalStatus"] = false;
taskHash["Status"] = "Complete";
taskHash["PercentComplete"] = "0";
taskHash["Action"] = "Rejected";
SPSite site = SPContext.Current.Site;
using (SPWeb web = site.OpenWeb())
{
web.AllowUnsafeUpdates = true;
SPSecurity.RunWithElevatedPrivileges(delegate()
{
SPWorkflowTask.AlterTask(this._TaskListItem, taskHash, true); // Send the data to the task list, by altering the tasks value
});
web.AllowUnsafeUpdates = false;
}
string strRequestorRole = this._TaskListItem.Title;
UpdateAuditTrail("Rejected", strRequestorRole.Replace(" Task", ""));
SPUtility.Redirect(this._TaskListAttachedTo.DefaultViewUrl, // Redirect the UI back to the Task List. You could chose
SPRedirectFlags.UseSource,
HttpContext.Current);
}
}
==============================================================================
// Code that responds to the Cancel Button Click event.
protected void btnCancel_Click(object sender, EventArgs e)
{
// If the user cancels out on the Workflow Task Edit Form, we'll take them back to the Document Library
if (this._TaskListAttachedTo == null)
{
SPUtility.Redirect(this._myTeamSite.Url,
SPRedirectFlags.UseSource,
HttpContext.Current);
}
else
{
SPUtility.Redirect(this._TaskListAttachedTo.DefaultViewUrl,
SPRedirectFlags.UseSource,
HttpContext.Current);
}
}
=========================================================================
// Code that responds to the Accept Button Click event.
protected void btnAccept_Click(object sender, EventArgs e)
{
SPSite site = SPContext.Current.Site;
using (SPWeb web = site.OpenWeb())
{
web.AllowUnsafeUpdates = true;
int statusID = GetLookupId("Accepted", "Title", "JobRequestStatus");
SPList JobRequestListItems = web.Lists["Request"];
SPListItem requestItem = JobRequestListItems.GetItemById(listID);
requestItem["Status"] = new SPFieldLookupValue(statusID, "Accepted");
requestItem["Scheduler"] = Request.ServerVariables["LOGON_USER"].ToString();
requestItem.Update();
web.AllowUnsafeUpdates = false;
}
// If the user cancels out on the Workflow Task Edit Form, we'll take them back to the Document Library
SPUtility.Redirect(this._TaskListAttachedTo.DefaultViewUrl,
SPRedirectFlags.UseSource,
HttpContext.Current);
}