Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sharepoint Out of the box Approval Workflow Approve/Reject comments issue when programmatically altering it

I have a re-usable out of the box sharepoint approval workflow 2010, and I am programmatically approve/reject workflow using the below code snippet inside infopath formcode (2010).

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)task.Web.Language, false),
    Strings.WorkflowStatusCompleted, new object[0]);
if (isApprove)
{
    ht[SPBuiltInFieldId.WorkflowOutcome] = "Approved";
    ht["TaskStatus"] = "Approved";
    ht[SPBuiltInFieldId.Comments] = "Approved by Manager";
    ht["Comments"] = "Approved by Manager";
}
else
{
    XPathNavigator navigatorRejectComments
        = this.MainDataSource.CreateNavigator().SelectSingleNode
           (XPATH_REJECT_COMMENTS, this.NamespaceManager);

    ht[SPBuiltInFieldId.WorkflowOutcome] = "Rejected";
    ht["TaskStatus"] = "Rejected";
    ht[SPBuiltInFieldId.Comments] = navigatorRejectComments.Value.Trim();
    ht["Comments"] = navigatorRejectComments.Value.Trim();
}
ht["FormData"] = SPWorkflowStatus.Completed;
web.AllowUnsafeUpdates = true;                                                            
isApproveReject = AlterTask(task, ht, true, 5, 100);                           
web.AllowUnsafeUpdates = false;

Task Alter method

private static bool AlterTask(SPListItem task, Hashtable htData, bool fSynchronous, int attempts, int  millisecondsTimeout)
{
if ((int)task[SPBuiltInFieldId.WorkflowVersion] != 1)
    {
        SPList parentList = task.ParentList.ParentWeb.Lists[new Guid(task[SPBuiltInFieldId.WorkflowListId].ToString())];
        SPListItem parentItem = parentList.Items.GetItemById((int)task[SPBuiltInFieldId.WorkflowItemId]);
        for (int i = 0; i < attempts; i++)
        {
            SPWorkflow workflow = parentItem.Workflows[new Guid(task[SPBuiltInFieldId.WorkflowInstanceID].ToString())];
            if (!workflow.IsLocked)
            {
                task[SPBuiltInFieldId.WorkflowVersion] = 1;
                task.SystemUpdate();
                break;
            }
            if (i != attempts - 1)
                Thread.Sleep(millisecondsTimeout);
        }
    }
    return SPWorkflowTask.AlterTask(task, htData, fSynchronous);
}

This code works fine, but the comments of the task is not getting altered and it is not included in the e-mail as well. Is there anything wrong with ht["Comments"] that I use? It is not getting altered in task comments. This is SharePoint 2010 workflows and infopath form i am using is also 2010.

Anyone faced similar problem ?

like image 242
SharePointError Avatar asked Oct 13 '11 10:10

SharePointError


People also ask

How do I enable approve reject in SharePoint online?

Navigate to the library or list where the Pending item or file is located. Select the item or items and select Approve/Reject from the command bar. In the Approve/Reject options for Approval Status, select Approved or Rejected. In the Comment section, you may enter a comment about why the item was approved.

Does SharePoint have approval workflow work?

In SharePoint products, you can use an Approval workflow to make the content approval process even easier and more efficient. For more information, see the Control content approval segment of this article.

How do I approve a SharePoint Workflow task?

To approve a taskOn the Workflow Tasks list, click All Tasks. Click Assign an approval task. On the ribbon, click Edit. Click Approved.


2 Answers

If you need code only for Lists (not for DocLibs) you can use item.ModerationInformation.Status property. Like the the following example:

var url = @"http://server/Lists/ContentApList";
var web = new SPSite(url).OpenWeb();
var list = web.GetList(url);
var item = list.GetItemById(1);
item["MyCheck"] = "test23";
item.ModerationInformation.Status = SPModerationStatusType.Pending;
item.ModerationInformation.Comment = "my coment";
item.SystemUpdate();

But if you want to do it for all list types, you can use internal method UpdateInternal, with the following parameters:

static void UpdateMigrate(SPListItem item)
{
  UpdateInternal(item, true, false, Guid.Empty, true, false,false, false, false, false);
}

static void CheckList5()
{
    var url = @"http://server/Lists/ContentApList";
    var web = new SPSite(url).OpenWeb();
    var file = web.GetFile("CheckDocLib/logo.gif");
    var item = file.ListItemAllFields;
    item["MyComments"] = "test23ddd";
    item.ModerationInformation.Status = SPModerationStatusType.Approved;
    item.ModerationInformation.Comment = "my coment";
    UpdateMigrate(item);
}

You can use examples from this russian blog Item ModerationInformation and SPListItem.UpdateInternal()

like image 150
Eduard Kibort Avatar answered Oct 19 '22 03:10

Eduard Kibort


To add a comment to a task when you Approve/Reject it, you just need to use the line before AlterTask:

ht["ows_FieldName_Comments"] = comments;

After the task is approved you can see the comments in the Workflow History List.

You can also get all the consolidated comments from a task with:

Hashtable extProperties = SPWorkflowTask.GetExtendedPropertiesAsHashtable(currentTask);

string consolidatedComments = extProperties["FieldName_ConsolidatedComments"].ToString();

Good luck!

like image 24
Gonçalo Leote Avatar answered Oct 19 '22 04:10

Gonçalo Leote