1

First: I am a noob and I use the REST API of SP server with Java, so no SDK involved here.

I uploaded a file to a Document set. Now I have to modify the ContentType and some other custom fields (properties) of the file. I used this kind of endpoint to upload it:

_api/web/getfolderbyserverrelativeurl(''{0}/{1}'')/files/add(overwrite=true, url=''{2}'')?$expand=ListItemAllFields

Where the placeholders will be replaced with:

  • serverRelativeUrl
  • folder name (docset title)
  • file name

I get a response like this:

{
  "ListItemAllFields": {
    "FileSystemObjectType": 0,
    "Id": 488,
    "ServerRedirectedEmbedUri": null,
    "ServerRedirectedEmbedUrl": "",
    "ID": 488,
    "ContentTypeId": "0x0101000CAB0B3907F52E45B48A63D0764D7D4E",
    "Created": "2021-10-21T11:52:06",
    "AuthorId": 11,
    "Modified": "2021-10-21T11:52:06",
    "EditorId": 11,
    "OData__CopySource": null,
    "CheckoutUserId": null,
    "OData__UIVersionString": "1.0",
    "GUID": "bde8b1ba-b7dd-4797-99c7-0e4e8b3cd6ef",
    "ComplianceAssetId": null,
    "DocumentSetDescription": null,
    "TaskItId": null,
    "TaskItPriority": null,
    "TaskItComments": null,
    "TaskItStartDate": null,
    "TaskItDueDate": null,
    "TaskItInternStatus": null,
    "TaskItInternAssignToId": null,
    "TaskItInternAssignToStringId": null,
    "TaskItInternAssignCcId": [],
    "TaskItInternAssignCcStringId": [],
    "TaskItPlannedSteps": null,
    "TaskItContactNotes": null,
    "TaskItResults": null,
    "TaskItExternStatus": null,
    "TaskItExternAssignTo": null,
    "TaskItExternAssignCc": null,
    "TaskItMailSubject": null,
    "TaskItMailSender": null,
    "TaskItMailReceiver": null,
    "TaskItMailReceiptDate": "2021-10-21T11:52:06"
  },
  "CheckInComment": "",
  "CheckOutType": 2,
  "ContentTag": "{62DFBCF6-7236-4A5E-A39E-BEA836D26E92},1,1",
  "CustomizedPageStatus": 0,
  "ETag": "\"{62DFBCF6-7236-4A5E-A39E-BEA836D26E92},1\"",
  "Exists": true,
  "IrmEnabled": false,
  "Length": "2400",
  "Level": 1,
  "LinkingUri": null,
  "LinkingUrl": "",
  "MajorVersion": 1,
  "MinorVersion": 0,
  "Name": "mail ne mail mit zeug und so_f.eml",
  "ServerRelativeUrl": "/websites/A0000023/TaskItLibrary/neue Aufgabe mit Attachments/mail ne mail mit zeug und so_f.eml",
  "TimeCreated": "2021-10-21T09:52:06Z",
  "TimeLastModified": "2021-10-21T09:52:06Z",
  "Title": null,
  "UIVersion": 512,
  "UIVersionLabel": "1.0",
  "UniqueId": "62dfbcf6-7236-4a5e-a39e-bea836d26e92"
}

The fields starting with "TaskIt" are the custom fields I want to change and also the content type of the element itself by using a certain content type ID.

Now I am trying to modify the data using this endpoint

_api/Web/GetFileByServerRelativePath(decodedurl='/websites/A0000023/TaskItLibrary/neue%20Aufgabe%20mit%20Attachments/mail%20ne%20mail%20mit%20zeug%20und%20so_f.eml')

Method is POST, the header contains a valid request digest etc. I am sending JSON data like this:

{
    "foo":"bar"
}

but whatever I try to modify (not "foo" of course) will result in the error which says:

"the given parameter does not exist in the method "getfilebyserverrelativepath" " (translated from German here, so I hope you recognize this error). It's a Microsoft.SharePoint.Client.InvalidClientQueryException

There must be some voodoo on how to modify a file in a Document set so I hope for a solution here.

1 Answer 1

1
+50

As you are getting item ID in response, you can update file properties using /items endpoint like:

function UpdateDocProperties(itemId) {
    var siteUrl = "https://tenant.sharepoint.com/sites/siteName/";
    var updateDocPropertiesEndpoint = siteUrl + "/_api/web/lists/getbytitle('DocumentSets')/items(" + itemId + ")";
    var payload = {
        "__metadata": { "type": "SP.Data.DocumentSetsItem" },
        "Title": "My custom Title"
    }

    $.ajax({
        url: updateDocPropertiesEndpoint,
        type: "POST",
        data: JSON.stringify(payload),
        async: false,
        headers: {
            "X-RequestDigest": $("#__REQUESTDIGEST").val(),
            "accept": "application/json;odata=verbose",
            "content-type": "application/json;odata=verbose",
            "X-HTTP-Method":"MERGE",
            "If-Match": "*"
        },
        success: function (data) {
            console.log("Properties updated successfully");
        },
        error: function (error) {
            console.log(error);
        }
    });
}

Usage:

UpdateDocProperties(4)

Where DocumentSets is the name of document library & 4 is item ID (show below images).

Output:

Before:

enter image description here

After:

enter image description here

Similarly, you can update your other fields by adding those to payload variable.

4
  • Thanks, but this doesn't work: if I only refer to "DocumentSets" what happens to the part where the doc set is referenced? Shouldn't the URL contain this, too? Like: "getByTitle('DocumentSets')/My Document Set/items(123)" ? Nov 9, 2021 at 9:12
  • Not required to pass the URL of document set. Every document (may it be inside folder/document set/or directly uploaded inside library) has unique ID at "document library level" >> So it is enough to pass the name of library & document item ID. I have tested above code with document sets only & it is working for me. Are you getting any error? if yes, what's the error message? Are you passing correct __metadata type in payload? Nov 9, 2021 at 9:52
  • No worries, somehow I managed to work around my issues. I changed the URI to something like this though: _api/Web/Lists(@v0)/Items(@v1)?&@v0=guid'f87588a7-cdfe-46ac-aefa-78dd278fdc8a'&@v1=528 Nov 9, 2021 at 10:43
  • Great, glad it worked for you. and yes, there are multiple ways to get lists like /Lists(<guid>) OR /Lists/getbytitle('<display name of list>') OR web/getList("/sites/dev/Lists/Addresses"), etc. Nov 9, 2021 at 10:55

Your Answer

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

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