使用REST API获取SharePoint Sitefeed中的所有回复

时间:2015-11-21 22:08:35

标签: rest sharepoint sharepoint-2013

我尝试使用RESTful服务返回SharePoint sitefeed中的所有回复。目前,我已成功使用此代码检索sitefeed的帖子:

function getFeed(){
var feed;
var reply;
var rCounter;
$.ajax({
    url: "https://<mysite>.sharepoint.com/<sitename>/_api/social.feed/actor(item=@v)/feed?@v=%27https://<mysite>.sharepoint.com/<sitename>/newsfeed.aspx%27",
    method: "GET",
    headers: { "Accept": "application/json; odata=verbose" },
    success: function (data) {
        // Returning the results
        myFeed = $(data);
        console.log(myFeed);
        for (i = 0; i < myFeed[0].d.SocialFeed.Threads.results.length; i++) {
             feed = (myFeed[0].d.SocialFeed.Threads.results[i].RootPost.Text);
             console.log(myFeed[0].d.SocialFeed.Threads.results[0].Actors.results[1].Name + ": " + feed);
                if (myFeed[0].d.SocialFeed.Threads.results[i].Replies.results.length >0){
                    rCounter = myFeed[0].d.SocialFeed.Threads.results[i].Replies.results.length;
                    for (j = 0; j < myFeed[0].d.SocialFeed.Threads.results[i].Replies.results.length; j++) {
                        rCounter--;
                        reply = myFeed[0].d.SocialFeed.Threads.results[i].Replies.results[rCounter].Text;
                        console.log(reply);
                    }
                 }
            console.log("* * * * * * * * *");    
        }
    },
    error: function (data) {
        console.log("ERROR - SEE CODE");
    }
    });

}

然而,这给了我帖子,但只有两个最新的回复。根据{{​​3}},我需要使用POST方法获取所有回复并传入线程ID。所以我做了一个新功能:

function getPost(){
$.ajax({
    url: "https://<mysite>.sharepoint.com/<sitename>/_api/social.feed/post(ID=ai)/?@ai='8.211b75cd6dc84fe4bc6c3e9f46971f51.97717348cd3048768103d55751dc0e2d.211b75cd6dc84fe4bc6c3e9f46971f51.819bde2276b948a8a120964289476489.17c08f26b90a4b659ff1fcfb0ede4025.5.5.1'",
    method: "POST",
    headers: { "Accept": "application/json; odata=verbose" },
    success: function (data) {
        // Returning the results
        console.log($(data));
        },
    error: function (data) {
        console.log("ERROR - SEE CODE");
    }
    });
}

当我运行这个新函数时,我收到403(FORBIDDEN)错误。有人能告诉我我做错了什么吗?

1 个答案:

答案 0 :(得分:1)

尝试传递如下所示的标题。

function getPost(){

$。AJAX({

url: "https://<mysite>.sharepoint.com/<sitename>/_api/social.feed/post(ID=ai)/?@ai='8.211b75cd6dc84fe4bc6c3e9f46971f51.97717348cd3048768103d55751dc0e2d.211b75cd6dc84fe4bc6c3e9f46971f51.819bde2276b948a8a120964289476489.17c08f26b90a4b659ff1fcfb0ede4025.5.5.1'",
method: "POST",
headers: { "Accept": "application/json;odata=verbose",
        "X-RequestDigest": $("#__REQUESTDIGEST").val() },
success: function (data) {
    // Returning the results
    console.log($(data));
    },
error: function (data) {
    console.log("ERROR - SEE CODE");
}
});

}