sharepointmicrosoft-graph-api

Get the full/absolute URL of an image webpart retrieved via the REST API


I'm using the Graph REST API to retrieve the contents of a Sharepoint page by going through the different webparts. Some webparts contain images (in the data/serverProcessedContent/imageSources), but the URL's are just relative paths.

Here's a snippet of the JSON I need to work with (from https://graph.microsoft.com/v1.0/sites/{site-id}/pages/{page-id}/microsoft.graph.sitePage/webParts):

{
    "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#sites('acme.sharepoint.com%2Cd93aedd6-665d-4e6f-92fd-d90bcfbe9412%2C0031999b-fe7a-4cf5-ad23-117a08b54fae')/pages('e4e3b229-e8e7-4a0a-a8f1-ae10b19bb0b1')/microsoft.graph.sitePage/webParts",
    "value": [
        {
            "@odata.type": "#microsoft.graph.standardWebPart",
            "id": "1a2d6313-b608-4569-a143-e236b7fcea4b",
            "webPartType": "cbe7b0a9-3504-44dd-a3a3-0e5cacd07788",
            "data": {
                "dataVersion": "1.6",
                "description": "Show a banner with title and author information",
                "title": "Banner",
                "properties": {
                    "imageSourceType": 2,
                    "title": "My title!",
                    "textAlignment": "Left",
                    "showPublishDate": false,
                    "showTopicHeader": false,
                    "layoutType": "CutInShape",
                    "topicHeader": "",
                    "enableGradientEffect": true,
                    "isDecorative": true,
                    "customContentDropSupport": "externallink",
                    "showTimeToRead": true,
                    "authorByline@odata.type": "#Collection(String)",
                    "authorByline": [
                        "man@acmegroep.com"
                    ],
                    "altText": "",
                    "webId": "0031999b-fe7a-4cf5-ad23-117a08b54fae",
                    "siteId": "d93aedd6-665d-4e6f-92fd-d90bcfbe9412",
                    "listId": "e7c6f1c9-0621-4474-b07d-3ca626bb1b65",
                    "uniqueId": "78802453-5351-479a-ac3d-c1017d651a37",
                    "imgHeight": 2832,
                    "imgWidth": 4240,
                    "authors@odata.type": "#Collection(graph.Json)",
                    "authors": [
                        {
                            "id": "i:0#.f|membership|man@acmegroep.com",
                            "upn": "man@acmegroep.com",
                            "email": "man@acmegroep.com",
                            "name": "Mark Man",
                            "role": "Marketing specialist"
                        }
                    ]
                },
                "serverProcessedContent": {
                    "htmlStrings": [],
                    "searchablePlainTexts": [],
                    "links": [],
                    "imageSources": [
                        {
                            "key": "imageSource",
                            "value": "/sites/Markets/SiteAssets/SitePages/image-of-%21/96119-new-colleague.jpg"
                        }
                    ]
                }
            }
        },
        ...

I've tried everything I could find only to recreate the correct URL to get the listed image, but nothing works. I always get some variant of The resource could not be found.

And simply prepending the path with https://<tenant_name>.sharepoint.com doesn't work, because all assets are secured. If I use that URL in a browser, it takes me to the login page. So I assume we need to go through the authenticated REST API.

Can anyone tell me how to assemble the correct URL from the above data? Or if there are additional/diferent requests requires, I'd love to hear that too.


Solution

  • After much digging and experimenting, I got on the right track by this post. Translating that answer to the current Graph API, I found that you can construct the correct URL for retrieving / downloading a Sharepoint image via the Graph REST API using this URL template:

    https://graph.microsoft.com/v1.0/sites/{siteId}/lists/{listId}/items/{itemId}/driveItem/content
    

    It is important to note that the name of itemId is different with different types of API responses: