I want to share document in page which does not have document library web part (hence I can't get renderCtx).

I need to use this function:


All what I have is files (list items) url eg. /sites/default/subsite/Shared Documents/MTT (Training) Application and User Location Diagram.vsdx

Can I get list id and item id from document path ?

Starting my jurney with this:

    type: "GET",
    url: "http://spdev/sites/default/_api/web/getfilebyserverrelativeurl('/sites/default/subsite/Shared Documents/MTT (Training) Application and User Location Diagram.vsdx')",
    headers: { "accept": "application/json; odata=verbose" },
    success: function (response) {
        if (response.d.Exists) {
            var responseIds = response.d.ContentTag.split(',');
            EnsureScriptFunc('sharing.js', 'DisplaySharingDialog', function () {
        else {
            alert('Error occured...');

But this call returns wrong data...


The getfilebyserverrelativeurl endpoint returns an SP.File object, so you need to select and expand the "ListItemAllFields" object with your document request to get the list item (SP.ListItem) that corresponds to your document, then you can expand to get the ParentList (SP.List) item for that document

Append this select and expand statement to your list item request:


Which produces a request URL like this:

"http://spdev/sites/default/_api/web/getfilebyserverrelativeurl('/sites/default/subsite/Shared Documents/MTT (Training) Application and User Location Diagram.vsdx')?$select=parentList,listItemAllFields/Id,listItemAllFields/ParentList&$expand=listItemAllFields,listItemAllFields/ParentList"

Then you'll have a ListItemAllFields Object that includes a ParentList Object with your response data.

The id can be found in data.ListItemAllFields.Id and the parent list id will be in data.ListItemAllFields.ParentList.Id

You can use the chrome dev tools to explore the other properties in REST returns like this by performing an ajax request in the console and inspecting the result element, I usually use something like the following (a jquery example)

$.getJSON(reqURI, function(data) { window.myJsonData = data });

Then I can just bring up the myJsonData variable in the window and inspect the object in a fairly user-friendly way.

| improve this answer | |

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.