Search code examples
sharepointdynamics-crmsharepoint-apidynamics-365

SharePoint REST - Why update Metadata returns 204 instead of 200?


Using below endpoint we can update the metadata for specific file: https://domain.example.com/_api/web/GetFileByServerRelativeUrl(URL)/ListItemAllFields

Why after updating metadata successfully API returns 204 instead of 200. Is it expected or there is any other way of updating metadata.


Solution

  • According to Dynamics Web API Documentation, the default is to update the data without returning the updated results. This is why you are getting a Status Code of 204. In order to get data returned, you will need to add prefer:return=representation header to your Web API call. Adding the prefer header will return a Status Code of 200.

    C# Example

    HttpClient client = new HttpClient();
    client.BaseAddress = new Uri(Helpers.GetSystemUrl(APIConnector.Application.Dynamics));
    client.DefaultRequestHeaders.Clear();
    client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
    client.DefaultRequestHeaders.Add("OData-MaxVersion", "4.0");
    client.DefaultRequestHeaders.Add("OData-Version", "4.0");
    client.DefaultRequestHeaders.Add("prefer", "return=representation");
    

    Typescript Example

    let xhr = new XMLHttpRequest();
    xhr.open("PATCH", CONNECTION_URL, true);
    xhr.setRequestHeader("Accept", "application/json");
    xhr.setRequestHeader("OData-MaxVersion", "4.0");
    xhr.setRequestHeader("OData-Version", "4.0");
    xhr.setRequestHeader("Content-Type", "application/json");          
    xhr.setRequestHeader("prefer", "return=representation");