1

I am developing news feed as an app part in SharePoint Online. This is my code to get social feed in my timeline using REST API.

 function getTimeline() {

            $.ajax({
                method: 'GET',
                url:hostweburl + "/_api/social.feed/my/timelinefeed",
                headers: {
                    "accept": "application/json;odata=verbose",
                    "X-RequestDigest": $("#__REQUESTDIGEST").val()
                },
                success: function (data) {
                    console.log(data)
                },
                error: function (xhr, ajaxOptions, thrownError) {
                    alert("GET error:\n" + xhr.status + "\n" + thrownError);
                }
            });
} 

When I am access this URL directly in browser it successfully gets the list items but this code gets forbidden error(403).

enter image description here

I searched about this issue and tried of using executor with the same error.And the another problem is when deploying the app when I give News Feed (Social) permission level to Read or anything, trusting the app(trust it) results in error.Anyone help me on this.

UPDATE: I have tried the answer by @gautham and I think permission level issue is resolved but when accessing URL(appweburl + "/_api/SP.AppContextSite(@target)/social.feed/my/timelinefeed?@target='" + hostweburl + "'") leads to error(Resource not found)

enter image description here

Help me to get out of this..

  • You put both 2013 and sharepoint-online tags. Is that Online or on-premise? – Sergei Sergeev Nov 30 '16 at 12:31
  • Sharepoint Online – Kumaraguru Nov 30 '16 at 13:00
1

You need to provide correct permission to read/write data from social feed.

In the AppManifest.xml, provide the permission as below and then you will be able to read/write data from social feed.

enter image description here

Try it with sp request executor js as below:

'use strict';
    var hostweburl;
    var appweburl;
    // Load the required SharePoint libraries.
    $(document).ready(function () {
        //Get the URI decoded URLs.
        hostweburl = decodeURIComponent(getQueryStringParameter("SPHostUrl"));

        // Resources are in URLs in the form:
        // web_url/_layouts/15/resource
        var scriptbase = hostweburl + "/_layouts/15/";
        // Load the js file and continue to load the page with information about the list items.
        // SP.RequestExecutor.js to make cross-domain requests
        $.getScript(scriptbase + "SP.RequestExecutor.js", getTimelineItems);
    });
    // Utilities
    // Retrieve a query string value.
    // For production purposes you may want to use a library to handle the query string.
    function getQueryStringParameter(paramToRetrieve) {
        var params = document.URL.split("?")[1].split("&");
        for (var i = 0; i < params.length; i = i + 1) {
            var singleParam = params[i].split("=");
            if (singleParam[0] == paramToRetrieve) return singleParam[1];
        }
    }    
    //Retrieve all of the list items
    function getTimelineItems() {
        var executor;
        // Initialize the RequestExecutor with the app web URL.
        executor = new SP.RequestExecutor(appweburl);
        executor.executeAsync({
            url: appweburl + "/_api/SP.AppContextSite(@target)/social.feed/my/timelinefeed?@target='" + hostweburl + "'",
            method: "GET",
            headers: {
                "Accept": "application/json; odata=verbose"
            },
            success: getListItemsSuccessHandler,
            error: getListItemsErrorHandler
        });
    }
    //Populate the selectListItems control after retrieving all of the list items.
    function getListItemsSuccessHandler(data) {
        console.log(data)
    }
    function getListItemsErrorHandler(data, errorCode, errorMessage) {
        console.log("Could not get list items: " + errorMessage);
    }

Since its a SharePoint hosted addin, you can also try the below code:

function getTimeline() {

            $.ajax({
                method: 'GET',
                url: "https://sitecollectionurl/_api/social.feed/my/timelinefeed",
                headers: {
                    "accept": "application/json;odata=verbose",
                    "X-RequestDigest": $("#__REQUESTDIGEST").val()
                },
                success: function (data) {
                    console.log(data)
                },
                error: function (xhr, ajaxOptions, thrownError) {
                    alert("GET error:\n" + xhr.status + "\n" + thrownError);
                }
            });
}

Another code, similar to that used in MSDN:

var feedManagerEndpoint;  

// Get the SPAppWebUrl parameter from the query string and build  
// the feed manager endpoint.  

$(document).ready(function () {  
    var appweburl;  
    var params = document.URL.split("?")[1].split("&");  
    for (var i = 0; i < params.length; i = i + 1) {  
        var param = params[i].split("=");  
        if (param[0] === "SPAppWebUrl") appweburl = param[1];  
    }  
    feedManagerEndpoint = decodeURIComponent(appweburl)+ "/_api/social.feed/my/timelinefeed";  
    getMyFeed();  
});  


// Get the current user's feed by using the   
// "<app web URL>/_api/social.feed/my/timelinefeed" endpoint.  

function getMyFeed() {  
    $.ajax( {  
        url: feedManagerEndpoint ,  
        headers: {   
            "accept": "application/json;odata=verbose"  
        },  
        success: feedRetrieved,  
        error: function (xhr, ajaxOptions, thrownError) {   
            alert("GET error:\n" + xhr.status + "\n" + thrownError);  
        }  
    });      
}  

// Parse the JSON data and iterate through the feed.  

function feedRetrieved(data) {  
    console.log(data);
}  

Also, try with below permission level:

enter image description here

  • Given the Permission Level but after giving the permission, trusting app leads to error and also the same permission error results for retrieving feed lists – Kumaraguru Nov 24 '16 at 14:31
  • What's the error? Same as current error or different? – Gautam Sheth Nov 24 '16 at 14:53
  • Trusting the app leads to error(Something went wrong) and same error for retrieving feed list – Kumaraguru Nov 25 '16 at 4:50
  • Can try creating a new app with above permission and then trying your code ? Also try with the updated code i posted – Gautam Sheth Nov 25 '16 at 5:50
  • ok.. I will try with above code – Kumaraguru Nov 25 '16 at 5:57

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.