1

I am trying to update custom metadata fields in sharepoint 2013 document library. File is uploaded in first REST call successfully but when trying to update metadata I am getting 403 forbidden error.

See my code below:

string siteURL = "http://<somesite.com>/sites/AJG/_api/web/GetFileByServerRelativeUrl(" + "'/sites/AJG/Client/FRED/test.txt'" + ")/ListItemAllFields";

It is giving result when I am putting it in browser.

HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(siteURL);

System.Net.NetworkCredential credentials = new System.Net.NetworkCredential(
                                                              "<username>",
                                                               "<pwd>",
                                                               "<domain>");

request.Credentials = credentials;
request.Method = "POST";
request.Accept = "application/json; odata=verbose";
request.ContentType = "application/json; odata=verbose";
request.Headers.Add("X-HTTP-Method", "MERGE");
request.Headers.Add("IF-MATCH", "*");
string formDigest = GetFormDigestValue("http://<somesite.com>/sites/AJG", credentials);
request.Headers.Add("X-RequestDigest", formDigest);
string stringData = "{ '__metadata': { 'type': 'SP.ListItem' }, 'Code': 'FieldValue' }";
request.ContentLength = stringData.Length;
StreamWriter writer = new StreamWriter(request.GetRequestStream());
writer.Write(stringData);
writer.Flush();
using (var response = (HttpWebResponse)request.GetResponse())
{
    using (var reader = new StreamReader(response.GetResponseStream()))
    {
        reader.ReadToEnd();
    }
}

Please let me know where I am getting wrong

1

Try to specify X-FORMS_BASED_AUTH_ACCEPTED request header:

request.Headers.Add("X-FORMS_BASED_AUTH_ACCEPTED", "f");

This error could occur when you set your Windows credentials on the request but still get an access denied error when requesting the data, see Retrieving Data from a Multi-Authentication Site by Using the Client Object Model and Web Services in SharePoint 2010 section for a more details.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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