I want to update a task to approved using REST API and C#, I am able to approve the task successfully but since its a C# solution deployed on IIS, it gets updated to System Account.
I am trying to update the Modified By field but its getting update. Any idea why?
The code runs fine without any errors but fails to update the Modified By column.
Input data :
inputData = "{'__metadata': { 'type': 'SP.Data.TasksListItem' }," +
"'Status': 'Approved'," +
"'FormData': 'Completed',"+
"'WorkflowOutcome': 'Approved'," +
"'Completed': true," +
"'PercentComplete': 1,"+
"'ExtendedProperties':'" + request.Comment.Replace("'", @"\'") + "'" +
"'EditorId': " + request.assignedToId + "}";
Code below :
string result = string.Empty;
HttpWebRequest wreq = (HttpWebRequest)WebRequest.Create(uri);
//wreq.Headers.Add("Authorization", "Bearer " + accessToken);
wreq.Method = "POST";
wreq.Accept = "application/json; odata=verbose";
wreq.ContentType = "application/json; odata=verbose";
wreq.Headers.Add("X-RequestDigest", formDigest);
if (operationType == "update")
{
wreq.Headers.Add("IF-MATCH", "*");
wreq.Headers.Add("X-HTTP-Method", "MERGE");
}
wreq.UseDefaultCredentials = true;
wreq.ContentLength = inputData.Length;
StreamWriter writer = new StreamWriter(wreq.GetRequestStream());
writer.Write(inputData);
writer.Flush();
WebResponse wresp = wreq.GetResponse();
using (StreamReader sr = new StreamReader(wresp.GetResponseStream()))
{
result = sr.ReadToEnd();
}
Tried updating using CSOM as well but doesn't work.
using (ClientContext ctx = new ClientContext(siteURL))
{
FieldUserValue Editor = new FieldUserValue();
Editor.LookupId = Convert.ToInt32(request.assignedToId);
Web web = ctx.Web;
List oList = web.Lists.GetByTitle(listName);
ListItem listitem = oList.GetItemById(request.TaskID);
ctx.Load(listitem);
ctx.ExecuteQuery();
listitem["Completed"] = true;
listitem["PercentComplete"] = 1;
listitem["FormData"] = "Completed";
if (request.ActionType.ToLower() == "approve")
{
listitem["Status"] = "Approved";
listitem["WorkflowOutcome"] = "Approved";
}
else if (request.ActionType.ToLower() == "reject")
{
listitem["Status"] = "Rejected";
listitem["WorkflowOutcome"] = "Rejected";
}
listitem["ExtendedProperties"] = request.Comment;
listitem["Editor"] = Editor;
listitem.Update();
ctx.ExecuteQuery();
}
If I run a console job just to update the Editor field, it works fine :
public static string ApproveTask()
{
string fieldnames = string.Empty;
try
{
using (ClientContext ctx = new ClientContext("http://sdsd/sites/sd/sd"))
{
Web web = ctx.Web;
List oList = web.Lists.GetByTitle("Tasks");
ListItem listitem = oList.GetItemById(801);
ctx.Load(listitem);
ctx.ExecuteQuery();
FieldUserValue Editor = new FieldUserValue();
Editor.LookupId = 1164;
listitem["Editor"] = Editor;
listitem.Update();
ctx.ExecuteQuery();
}
}
catch (Exception)
{
throw;
}
return fieldnames;
}
UPDATE : I am trying to implement this because of the email body i get below, once approved from custom API