Rest Api in Sharepoint for Special Fields like Assignedto in TaskList

Sharepoint updates issue

You can update the “Assigned To” field of a SharePoint Task List item using the REST API by sending a POST request with a JSON payload to the item’s endpoint.

Since “Assigned To” is a Person or Group field, you need to use the User ID (the ID from the site’s User Information List) for the new assignee.

 

📝 REST API JSON Payload Structure

 

The key to updating the “Assigned To” field is the specific property name and value format in the JSON body:

Field Required Value Notes
__metadata Type object Specifies the entity type (SP.Data.[ListName]ListItem).
AssignedToId Integer or Array of Integers Crucial: You must use the internal field name appended with Id. The value is the target user’s ID.

 

💻 Example C# Implementation (Using HttpClient)

 

Here is how you would construct the JSON and the POST request using C#’s HttpClient class.

 

1. Find the User ID

 

Before running this, you need the Site User ID for the user you want to assign the task to. You can get this by using the _api/web/ensureuser endpoint:

HTTP

 
POST /_api/web/ensureuser('i:0#.f|membership|user@tenant.onmicrosoft.com')

The response will contain the Id you need.

 

2. C# Code to Update the Task

 

C#

 
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;

public async Task UpdateTaskAssignedTo(string siteUrl, string accessToken, string listName, int itemId, int newAssigneeId)
{
    // 1. Define the item type name.
    // The convention is SP.Data.[ListName]ListItem (e.g., SP.Data.TasksListItem)
    string itemEntityType = $"SP.Data.{listName.Replace(" ", string.Empty)}ListItem";

    // 2. Construct the REST API endpoint URL.
    string apiUrl = $"{siteUrl}/_api/web/lists/getbytitle('{listName}')/items({itemId})";

    // 3. Construct the JSON payload.
    // Use 'AssignedToId' with the user's ID.
    string jsonBody = $@"{{
        ""__metadata"": {{ ""type"": ""{itemEntityType}"" }},
        ""AssignedToId"": {newAssigneeId}
    }}";

    using (var client = new HttpClient())
    {
        // Set the Authorization header (for SharePoint Online/Add-in Authentication)
        client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
        
        // Indicate the request is an update (MERGE method)
        client.DefaultRequestHeaders.Add("X-HTTP-Method", "MERGE"); 
        
        // Pass the ETag header to avoid concurrency issues (Use * to ignore checks for simple updates)
        client.DefaultRequestHeaders.Add("IF-MATCH", "*"); 

        // Set the content type
        var content = new StringContent(jsonBody, Encoding.UTF8, "application/json");

        // Execute the request
        HttpResponseMessage response = await client.PostAsync(apiUrl, content);

        if (response.IsSuccessStatusCode)
        {
            Console.WriteLine($"Task {itemId} in {listName} successfully updated and assigned to User ID: {newAssigneeId}.");
        }
        else
        {
            // Log or handle the error response
            string error = await response.Content.ReadAsStringAsync();
            Console.WriteLine($"Error updating task: {response.StatusCode} - {error}");
        }
    }
}

 

Multiple Users

 

If the “Assigned To” field allows multiple users, you must format the AssignedToId value as an array of integers:

JSON

 
// JSON Payload for multiple assignment
string jsonBodyMulti = $@"{{
    ""__metadata"": {{ ""type"": ""{itemEntityType}"" }},
    ""AssignedToId"": {{
        ""__metadata"": {{ ""type"": ""Collection(Edm.Int32)"" }},
        ""results"": [ 12, 15, 20 ] // Array of User IDs
    }}
}}";

You would replace the simple integer newAssigneeId in the C# code with this more complex JSON structure for multiple users.