2

I am trying to update the Metadata information for a file in Document Library. I am following this article.

The REST End point used in given article is

var restSource = appweburl +"/_api/SP.AppContextSite(@target)/web/lists/getbytitle('Documents')/rootfolder/files/getbyurl(url='" + fileUrl + "')/listitemallfields?@target='" + hostweburl + "'";

But I am working on Premises. So I customized this end point as:

var url = siteUrl + "_api/Web/GetFolderByServerRelativeUrl('libraryName/FodlerName/documentSetName')/Files/getbyurl(url='FileName')/listitemallfields";

My full code which I am using is

var digest = GetFormDigest();

    if (!string.IsNullOrEmpty(digest))
    {
      var url = siteUrl + "_api/Web/GetFolderByServerRelativeUrl('" + libraryName + "/Tss_RootFolder/" + documentSetName + "')/Files/getbyurl(url='" + fileName + "')/listitemallfields";
      var body = "{ '__metadata': {'type':'SP.ListItem'},'Title': 'My Title 3'}";
      var client = new WebClient();
      client.Credentials = creds;
      client.Headers[HttpRequestHeader.Accept] = "application/json;odata=verbose";
      client.Headers[HttpRequestHeader.ContentType] = "application/json;odata=verbose";
      client.Headers["X-RequestDigest"] = digest;
      client.Headers["If-Match"] = "*";
      client.Headers["X-Http-Method"] = "PATCH";
      var json = client.UploadString(url, body); }

My file is located in Document Library >> Root Folder >> Document Set >> File.

Am I using correct REST end point? Because I am always getting 400 (Bad Request) error.

I already gone through several similar question on SE but not able to find any proper solution.

I am not working on SharePoint Environment so No CSOM and SSOM codes will work for me.

Edit: I changed my REST Endpoint to:

var url = siteUrl + "_api/Web/GetFolderByServerRelativeUrl('" + libraryName + "/Tss_RootFolder/" + documentSetName + "')/Files/getbyurl(url='" + fileName + "')/listitemallfields/Title";

No I am not getting 400 (Bad Request) Error but the fields also not get updated.

Any help or suggestion will be appreciated.

4

Finally after working more than 2 days I found the solution. Actually I was making a mistake in using GetFileByServerRelativeUrl() method.

I was using url as:

var url = siteUrl + "_api/Web/GetFolderByServerRelativeUrl('" + libraryName + "/Tss_RootFolder/" + documentSetName + "')/Files/getbyurl(url='" + fileName + "')/listitemallfields";

I just changed the url as

var url = siteUrl + "_api/Web/GetFileByServerRelativeUrl('/sites/subSiteName/" + libraryName + "/Tss_RootFolder/" + documentSetName + "/" + fileName + "')/ListItemAllFields";

And the Metadata was updated.

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.