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

2

18

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 ?

Regenerator answered 13/10, 2011 at 10:9 Comment(1)
That code definitely helped me on a really important project. Now I'll be working on comments. If I find anything useful, I will definitely let you know. Thanks!Awestricken
E
1

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!

Electrocorticogram answered 6/9, 2012 at 13:33 Comment(1)
Hi Gonçalo Leote, thank you for your answer. that is what i have expected. and it works fine, thanks alot again!Regenerator
Y
2

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

Yuri answered 23/10, 2011 at 14:25 Comment(1)
Guess you are talking about the moderation comments.. i was reffering task comments in a approval workflow which will be available in the email as well.Regenerator
E
1

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!

Electrocorticogram answered 6/9, 2012 at 13:33 Comment(1)
Hi Gonçalo Leote, thank you for your answer. that is what i have expected. and it works fine, thanks alot again!Regenerator

© 2022 - 2024 — McMap. All rights reserved.