Retrieve Publishing image field with Sharepoint 2013 REST Api / CSOM

后端 未结 4 821
忘掉有多难
忘掉有多难 2021-02-06 01:25

We\'re using the Sharepoint 2013 REST API to get all news items from the Sharepoint. We made a custom ContentType \'Newsitem\' with several properties including a Publishing Ima

4条回答
  •  生来不讨喜
    2021-02-06 02:01

    Unfortunately, the Publishing Image field is not technically returnable via REST (at least, according to this article).

    However I found (using the Advanced REST client) that you can actually retrieve the html for the Publishing Image field by making two requests. One to retrieve the list item for which you are attempting to get the Publishing Image, and another to retrieve the Publishing Image html via the FieldValuesAsHtml property of the returned results.

    getPublishingImage(listname, id){
        var publishingImage = '';
    
        $.ajax({
            url:        _spPageContextInfowebroot.webAbsoluteUrl + '/_api/web/lists/getbytitle(\'' + listname + '\')/items(' + id + ')',
            method:     'GET',
            headers:    {
                Accept:     'application/json; odata=verbose'
            },
            success:    function(data, request){
                // List item retrieved. Fetch the Publishing Image.
                var publishingImageUri = data.d.0.FieldValuesAsHtml.__deferred.uri;
    
                // Query SharePoint
                $.ajax({
                    url:        publishingImageUri,
                    method:     'GET',
                    headers:    {
                        Accept:     'application/json; odata=verbose'
                    },
                    success:    function(data, request){
                        publishingImage = data.d.Image;
                    }
                });
            }
        });
    
        // Return the Publishing Image html
        return publishingImage;
    };
    

    While not ideal, at least with the html you can use jQuery or another method to extract the image uri from the html element. Or you can simply insert the element as is into the DOM.

    Hope this helps!

提交回复
热议问题