Post/reply a post by social feeds REST API in SharePoint 2013

Source: Internet
Author: User

Label:

As we know, we can post/reply a post in Newsfeed for my site, but how can we apply by JS? SharePoint provide the REST API for developer to apply it.

You can use the SharePoint representational state Transfer (REST) service to does the same things that's can do with The. NET client object models and the JavaScript object model. The REST service exposes resources, correspond to SharePoint objects, properties, and methods. To use the REST service, your build and send HTTPGET andPOST requests to the resource endpoints that repr Esent the tasks you want to do

The endpoint URIs for most feeds tasks begin with the Socialrestfeedmanager resource (social.feed), Follo Wed by themy resource or thepost resource:

    • The my resource represents the current user. When used an inline in the endpoint URI, it sets the context of the request to the current user. For example,http://contoso.com/_api/social.feed/my/news Gets the newsfeed for the current user.


This is the result:a. Create UI via HTMLB. New and Reply a post via my JS code


C. Verify the result in Newsfeed

A. You can new A post (POST) B. You can reply a specify post via getting the Post ID if click any post title (reply) c. Can view all post by current user (all) this is the rest API in Detail:new a post:

$.ajax ({url:weburl + "/_api/social.feed/my/feed/post", type: "Post", Data:JSON.stringify ({ ' Restcreationdata ': {' __metadata ': {' type ': ' SP '.                     Social.socialrestpostcreationdata '}, ' ID ': null, ' Creationdata ': { ' __metadata ': {' type ': ' SP '.                    Social.socialpostcreationdata '}, ' ContentText ': "The Post content text", ' Updatestatustext ': false}}), headers: {"Accept": "Appl Ication/json;odata=verbose "," Content-type ":" Application/json;odata=verbose "," X-requestdigest ": $ (" #__REQUESTDIGEST "). Val ()}, Success:function () {Console.log (" success to Post ");}, Error:function (x        HR, Ajaxoptions, thrownerror) {alert ("POST error:\n" + xhr.status + "\ n" + thrownerror); }    });


Reply a post:

    $.ajax ({url:weburl + "/_api/social.feed/post/reply", type: "Post", Data:JSON.stringify ({ ' Restcreationdata ': {' __metadata ': {' type ': ' SP '.                     Social.socialrestpostcreationdata '}, ' ID ': PostID ' creationdata ': { ' __metadata ': {' type ': ' SP '.                    Social.socialpostcreationdata '}, ' ContentText ': "The Reply content text", ' Updatestatustext ': false}}), headers: {"Accept": "App Lication/json;odata=verbose "," Content-type ":" Application/json;odata=verbose "," X-requestdigest ": $ ( "#__REQUESTDIGEST"). Val ()}, Success:function () {Console.log ("Reply to Success");}, Error:function (        XHR, Ajaxoptions, thrownerror) {alert ("POST error:\n" + xhr.status + "\ n" + thrownerror);    }}); 

The complete code:

Note:just Add the code inside the Script Editor Web Part in page

Post: <input type= "text" class= "postcontent"/> <div class= "newpost" style= "width:40px; Background-color:gray; padding-left:10px "> Post </div>reply: <input type=" text "class=" replycontent "/> <div class=" Replypost "style=" width:40px; Background-color:gray; padding-left:10px;margin-top:10px "> Reply </div> <div class=" Allpost "style=" width:40px; Background-color:gray; padding-left:10px;margin-top:10px "> All </div><div class=" message "></div><script src="// Code.jquery.com/jquery-1.11.2.min.js "></script><script>var feedmanagerendpoint;//Get the Spappweburl parameter from the query string and build//the Feed manager endpoint.$ (document). Ready (function () {var a        Ppweburl= "http://tristan-02";    Feedmanagerendpoint = decodeURIComponent (appweburl) + "/_api/social.feed";    $ (". Newpost"). On ("click", Function () {posttomyfeed ();    });    $ (". Replypost"). On ("click", Function () {Reply (); });       $ (". Allpost"). On ("click", Function () {getmyfeed (); });});/ /reply postfunction reply () {$.ajax ({url:feedmanagerendpoint + "/post/reply", type: "Post", Da Ta:JSON.stringify ({' restcreationdata ': {' __metadata ': {' type ': ' SP '). Social.socialrestpostcreationdata '}, ' ID ': $ (". Postcontent"). Val (), ' Creati OnData ': {' __metadata ': {' type ': ' SP '.                    Social.socialpostcreationdata '}, ' ContentText ': $ (". Replycontent"). Val (), ' Updatestatustext ': false}}), headers: {"Accept": "Appl Ication/json;odata=verbose "," Content-type ":" Application/json;odata=verbose "," X-requestdigest ": $ (" #__REQUESTDIGEST "). Val ()}, Success:function () {Console.log (" Reply to Success ");}, Error:function (XHR, ajaxoptions, thrownerror) {alert ("POST error:\n" + xhr.status + "\ n" + thrownerror); }    });} Publish a post to the current user's feed by using the//"<app Web Url>/_api/social.feed/my/feed/post" endpoint. function Posttomyfeed () {$.ajax ({url:feedmanagerendpoint + "/my/feed/post", type: "Post", data : Json.stringify ({' restcreationdata ': {' __metadata ': {' type ': ' SP ').                     Social.socialrestpostcreationdata '}, ' ID ': null, ' Creationdata ': { ' __metadata ': {' type ': ' SP '.                    Social.socialpostcreationdata '}, ' ContentText ': $ (". Postcontent"). Val (), ' Updatestatustext ': false}}), headers: {"Accept": "Appli   Cation/json;odata=verbose "," Content-type ":" Application/json;odata=verbose ",         "X-requestdigest": $ ("#__REQUESTDIGEST"). Val ()}, Success:function () {Console.log ("success to post "), Error:function (XHR, ajaxoptions, thrownerror) {alert (" POST error:\n "+ xhr.status +" \ n "+ THR        OWNERROR); }    });} Get the current user's feed by using the//"<app Web Url>/_api/social.feed/my/feed" Endpoint.function getmyfeed ( {$.ajax ({url:feedmanagerendpoint + "/my/feed", headers: {"Accept": "Application/json;o             Data=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) {var stringdata = json.stringify (dat    a);      var jsonobject = Json.parse (StringData);     var feed = jsonobject.d.socialfeed.threads;    var threads = feed.results;    var feedcontent = ""; for (vAr i = 0; i < threads.length;        i++) {var thread = Threads[i]; var participants = thread.        Actors; var owner = Participants.results[thread. Ownerindex].        Name;  Feedcontent + = "<div class= ' PostItem ' >" + "<span class= ' owner ' >" + owner + ": </span>" + "<span class = ' PostText ' > ' +thread. Rootpost.text + "</span>" + "<div class= ' PostID ' >" + Thread.    Rootpost.id+ "</div>" + "</div>";     } $ (". Message"). HTML (feedcontent);        $ (". PostItem"). "Click", Function () {var id = $ (this). Find (". PostID"). Text ();        $ (". Postcontent"). Val (id); });} </script><style>.postitem {margin-top:10px;}. PostItem > owner{color:green;}. PostItem > postid{display:none;}. Postitme > posytext{//}</style>





Post/reply a post by social feeds REST API in SharePoint 2013

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

Tags Index: