0

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

enter image description here

1 Answer 1

0

You typically have to update Editor and Modified at the same time in a pair, even if you are not changing both values. I have even run into cases where you also have to update Author and Created at the same time, even if three of those values are being set to their already existing values, for example:

listitem["Editor"] = Editor;
listitem["Modified"] = listitem["Modified"];
//listitem["Author"] = listitem["Author"];
//listitem["Created"] = listitem["Created"];
listitem.Update();
ctx.ExecuteQuery();

The same holds true for REST:

inputData = 
"{'__metadata': { 'type': 'SP.Data.TasksListItem' }," +
  "'Status': 'Approved'," +
  "'FormData': 'Completed',"+
  "'WorkflowOutcome': 'Approved'," +
  "'Completed': true," +
  "'PercentComplete': 1,"+
  "'ExtendedProperties':'" + request.Comment.Replace("'", @"\'") + "'" +
  "'EditorId': " + request.assignedToId + 
  "'Modified': '" + originalItemModifiedDate + "'" + 
  //"'AuthorId': " + originalItemAuthor  + 
  //"'Created': '" + originalItemCreatedDate + "'" + 
"}";
1
  • I am trying to update the task list, i guess its something different there. It always saves as System Account. The problem is when approved from custom API the email gets triggered with System Account. image updated
    – Ishan
    Commented Mar 24, 2020 at 9:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.