0
votes

My company has made a custom photo-field in Sharepoint for it's news. I'm trying to use Microsoft Graph to fetch the images, but with no success.

This is the columns description:

{ 
        "columnGroup": "Page Layout Columns",
        "description": "",
        "displayName": "Thumbnail image",
        "enforceUniqueValues": false,
        "hidden": false,
        "id": "XXXXXXX-XXXX-XXXX-XXX-XXXXXXXXXXXX",
        "indexed": false,
        "name": "PublishingPageImage",
        "readOnly": false,
        "required": false
    },

In the documentation for Microsoft Graph it is written that you can make a request like this

GET https://graph.microsoft.com/beta/sites/{site-id}/lists/{list-id}/items?expand=fields(select=Column1,Column2)

Although - no matter how I seem to write the request, i can't get the image field.

My most recent try has been this request:

https://graph.microsoft.com/beta/sites/knowit.sharepoint.com/lists/posts/items?expand=fields(select=PublishingPageImage)

The respons I got from Microsoft was this:

{
"error": {
    "code": "-1, Microsoft.SharePoint.Client.ClientServiceException",
    "message": "Cannot serialize data for type Microsoft.SharePoint.Publishing.Fields.ImageFieldValue.",
    "innerError": {
        "request-id": "f25e4851-0c1b-4061-ad6a-948d38004046",
        "date": "2018-09-17T14:03:01"
    }
}

}

Should I use something like .value or .data or .ImageUrl after the request? If i get a link or a data-value doesn't really matter. In the call for /me/ for Microsoft users there is a $value property for getting the user profile photo. Is it something like this?

1
Based on the test on C#, it should be have a property ImageUrl, but i have not confirmed if it works well on GraphSeiya Su

1 Answers

2
votes

It depends how image is stored. If it is attachment you can do following:

  string baseURL = $"https://{spTenant}/_api/web/lists('{ AA.config.listID}')/items({ siteData.Id})/AttachmentFiles";
        string fileName = await GetFileNameAsync(baseURL);
        string getPictureReqUrl = $"{baseURL}('{fileName}')/$value";
        Stream responseStream = await GetPictureAsync(getPictureReqUrl);

     private static async Task<Stream> GetPictureAsync(string reqUrl)
    {
        client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", SPOToken);
        HttpResponseMessage response = await client.GetAsync(reqUrl);
        return await response.Content.ReadAsStreamAsync();
    }

Important! This is not supported by graph yet, but you can use SharePoint Rest API

If picture is stored in document library you need to use Drive object instead https://developer.microsoft.com/en-us/graph/docs/api-reference/beta/resources/drive