1

J'essaie d'approuver/de rejeter par programme les flux de travail dans SharePoint. Je peux le faire avec succès, mais je ne peux pas ajouter de commentaires. J'ai eu mon code d'une année old question, qui n'a pas été répondu non plus, donc j'ai pensé que je commencerais une nouvelle question.Approbation/rejet de flux de travaux par programme et ajout de commentaires

Mon code:

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 (param == "Approved") 
{ 
    ht[SPBuiltInFieldId.WorkflowOutcome] = "Approved"; 
    ht["TaskStatus"] = "Approved"; 
    if (!string.IsNullOrEmpty(comments)) 
    { 
     ht[SPBuiltInFieldId.Comments] = comments; 
     ht["Comments"] = comments; 
     ht[SPBuiltInFieldId.Comment] = comments; 
    } 
} 
else 
{ 

    ht[SPBuiltInFieldId.WorkflowOutcome] = "Rejected"; 
    ht["TaskStatus"] = "Rejected"; 
    if (!string.IsNullOrEmpty(comments)) 
    { 
     ht[SPBuiltInFieldId.Comments] = comments; 
     ht["Comments"] = comments; 
     ht[SPBuiltInFieldId.Comment] = comments; 
    } 
} 
ht["FormData"] = SPWorkflowStatus.Completed; 
bool isApproveReject = AlterTask(task, ht, true, 5, 100); 

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

Répondre

2

Pour ajouter un commentaire à une tâche lorsque vous approuvez/Rejeter, vous avez juste besoin d'utiliser la ligne avant AlterTask:

ht["ows_FieldName_Comments"] = comments; 

Une fois la tâche est approuvée vous pouvez voir les commentaires dans la liste d'historique de workflow.

Vous pouvez également obtenir tous les commentaires consolidés d'une tâche:

Hashtable extProperties = SPWorkflowTask.GetExtendedPropertiesAsHashtable(currentTask); 

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

Il est également possible d'approuver/Rejeter les flux de travail par côté client modèle objet

Code pour l'approbation du flux de travail Tâche

 ClientContext ctx = new ClientContext("http://SiteUrl"); 
     Web web = ctx.Web; 
     List list = web.Lists.GetByTitle("My Task List"); 
     ListItem listitem = list.GetItemById(1); 
     listitem["Completed"] = true; 
     listitem["PercentComplete"] = 1; 
     listitem["Status"] = "Approved"; 
     listitem["WorkflowOutcome"] = "Approved"; 
     listitem.Update(); 
     ctx.ExecuteQuery(); 
code

pour le rejet de workflow Tâche

 ClientContext ctx = new ClientContext("http://SiteUrl"); 
     Web web = ctx.Web; 
     List list = web.Lists.GetByTitle("My Task List"); 
     ListItem listitem = list.GetItemById(1); 
     listitem["Completed"] = false; 
     listitem["PercentComplete"] = 1; 
     listitem["Status"] = "Rejected"; 
     listitem["WorkflowOutcome"] = "Rejected"; 
     listitem.Update(); 
     ctx.ExecuteQuery();