使用Microsoft Graph从Sharepoint帖子获取图像

时间:2018-09-17 14:09:44

标签: image sharepoint microsoft-graph sharepoint-api

我的公司已为其新闻在Sharepoint中创建了一个自定义照片字段。我正在尝试使用Microsoft Graph来获取图像,但是没有成功。

这是列说明:

{ 
        "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
    },

Microsoft Graph的文档中,您可以提出这样的请求

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

尽管-不管我如何写请求,我都无法获得图像字段。

我最近的尝试是此请求:

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

我从微软那里得到的答复是:

{
"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"
    }
}

}

在请求之后,我应该使用.value还是.data.ImageUrl之类的东西?如果我得到一个链接或数据值并不重要。在Microsoft用户的/me/通话中,有一个$value属性用于获取用户个人资料照片。是这样吗?

1 个答案:

答案 0 :(得分:2)

这取决于图像的存储方式。 如果是附件,则可以执行以下操作:

  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();
    }

重要!图尚不支持此功能,但是您可以使用SharePoint Rest API

如果图片存储在文档库中,则需要使用Drive对象 https://developer.microsoft.com/en-us/graph/docs/api-reference/beta/resources/drive