Articles about European Sharepoint Hosting Service
SharePoint 2013 Hosting – HostForLIFE.eu :: Custom Like And Comment Functionality For SharePoint Online Custom Pages
Today we will see how to add custom like and comment functionality in SharePoint custom pages. For that I have developed one common web part/txt file using JQuery and REST api. Currently SharePoint is providing likes and rating functionality for the list items. I tried to use this but this was not required so I tried to develop this custom functionality for SharePoint online pages.

You need to just add one content editor web part and add the below code into that web part. Below are the overall steps.Step 1
Create custom list in SharePoint having name “LikesList” to store all the likes given by the user. Add the below columns into it,

You need to just add one content editor web part and add the below code into that web part. Below are the overall steps.Step 1
Create custom list in SharePoint having name “LikesList” to store all the likes given by the user. Add the below columns into it,
- column name : LikeBy type : Person or Group
- column name : CommentID type : Number
- column name : PageURL type : Single line of text
Step 2
Create custom list in SharePoint having the name “CommentsList” to store all the comments added by user. Add the below columns into it,
Create custom list in SharePoint having the name “CommentsList” to store all the comments added by user. Add the below columns into it,
- column name : ParentCommentID type : Number
- column name : CommentText type : Multiple lines of text
- column name : PageURL type : Single line of text
- column name : CommentBy type : Person or Group
Step 3
Now create one text file and add the below code into it and save that file.
Step 4
Create one web part page add your contents in to it. Add one more content editor web part -> edit web part -> give path of the txt file-> click on apply -> click on ok-> save the page -> publish it.
Now create one text file and add the below code into it and save that file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 |
<div style="width: 70%;background-color: white;color: black; border-radius: 5px; padding:10px;"> <table style="width:100%"> <tbody> <tr> <td style="text-align: right;"> <span id="lblPageComments">0 Comments | </span> <span id="spLikes" title="" class="LikeClass"> <img id="imgLike" src="../_layouts/15/images/LikeFull.11x11x32.png?rev=23"> <span id="lblPageLikes">0</span> </span> <a id="lnkBtnPageLike" class="links">Like</a> <span id="LikeID" style="display:none;"></span> </td> </tr> <tr> <td style="padding-left: 5px;"> <h3>Comment : </h3> </td> </tr> <tr> <td style="padding: 25px;"> <textarea name="txtComments" rows="3" cols="20" id="txtComments" placeholder="Comment..." style="width:95%;"></textarea><br><br> <input type="button" name="btnPost" value="Post" id="btnPost"> <input type="button" name="btnReset" value="Reset" id="btnReset"> </td> </tr> </tbody> </table> <br> Comments... <table id="tblComments" style="width:100%"> </table> </div> <style type="text/css"> a { cursor: pointer; } </style> <script src="/JS/jquery.min.js"></script> <script src="/JS/jquery-ui.js"></script> <link rel="stylesheet" href="/CSS/jquery-ui.css"> <script src="JS/jquery-dateFormat.min.js"></script> <script type="text/javascript"> var PageURL = window.location.href; var UserID = _spPageContextInfo.userId; $(document).ready(function() { getSetPageDet(); getAllComments(); $("#btnPost").click(function() { AddComment(); }); $("#btnReset").click(function() { $("#txtComments").val(""); }); $("#lnkBtnPageLike").click(function() { if ($("#lnkBtnPageLike")[0].innerText == "Like") { LikePage(); } else { UnLikePage(); } }); }); // This function will reset the value of comment box function reset(textbox) { $("#txtReply_" + textbox).val(""); } // This function is used to get all the comments from comments list for that particular page having type as comment. function getAllComments() { $.ajax({ url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/GetByTitle('CommentsList')/items?$expand=CommentBy&$select=*,CommentBy/Title&$filter=PageURL eq '" + PageURL + "' and Title eq 'Comment'&$orderby=ID desc", type: "GET", headers: { "accept": "application/json;odata=verbose", "content-Type": "application/json;odata=verbose" }, success: function(data) { var commentsHTML = ""; if (data.d.results.length > 0) { $.each(data.d.results, function(index, value) { var CreatedDate = jQuery.format.date(value.Created, "ddd, MMM d, yyyy h:mm a"); commentsHTML += "<tr><td><span id='CommentsID_" + value.Id + "' style='display:none;'>" + value.Id + "</span><br><span>" + value.CommentText + "</span><br><a class='linksAuthor' href='/../../_layouts/15/userdisp.aspx?ID=" + value.CommentById + "' target='_blank'>" + value.CommentBy.Title + "</a><br><span>" + CreatedDate + "</spam> <span id='lblCommentReplies_" + value.Id + "'>0 Replies | </span><span id='spLikes_" + value.Id + "' title='' class='LikeClass'> <img src='../_layouts/15/images/LikeFull.11x11x32.png?rev=23'> <span id='lblLikes_" + value.Id + "' class='LikeClass'>0</span></span> <a id='lnkBtnLike_" + value.Id + "' class='links' onClick='LikeComment(" + value.Id + ")'>Like</a><span id='LikeID_" + value.Id + "' style='display:none;'></span>"; if (UserID == value.CommentById) { commentsHTML += " <a id='lnkBtnDelete_" + value.Id + "' class='links' onClick='DeleteComment(" + value.Id + ")'>Delete</a><br><br>Replies...<br><div style='margin-left: 10%;'><table style='width: 90%'><tbody><tr><td colspan='2'><textarea name='txtReply_" + value.Id + "' rows='1' cols='20' id='txtReply_" + value.Id + "' placeholder='Reply...' style='width:100%;'></textarea><br><br><input type='button' name='btnPostReply_" + value.Id + "' value='Post' id='btnPostReply_" + value.Id + "' onClick='AddReply(" + value.Id + ")'><input type='button' name='btnResetReply_" + value.Id + "' value='Reset' id='btnResetReply_" + value.Id + "' onClick='reset(" + value.Id + ")'></td></tr></tbody></table><div><table cellspacing='0' id='tblReply_" + value.Id + "' style='border-collapse:collapse;'></table></div></div><br><hr></td></tr>"; } else { commentsHTML += "<br><br>Replies...<br><div style='margin-left: 10%;'><table style='width: 90%'><tbody><tr><td colspan='2'><textarea name='txtReply_" + value.Id + "' rows='1' cols='20' id='txtReply_" + value.Id + "' placeholder='Reply...' style='width:100%;'></textarea><br><br><input type='button' name='btnPostReply_" + value.Id + "' value='Post' id='btnPostReply_" + value.Id + "' onClick='AddReply(" + value.Id + ")'><input type='button' name='btnResetReply_" + value.Id + "' value='Reset' id='btnResetReply_" + value.Id + "' onClick='reset(" + value.Id + ")'></td></tr></tbody></table><div><table cellspacing='0' id='tblReply_" + value.Id + "' style='border-collapse:collapse;'></table></div></div><br><hr></td></tr>"; } }); $("#tblComments")[0].innerHTML = commentsHTML; getSetCommentDet(); } }, error: function(error) { alert(JSON.stringify(error)); } }); } // This function is used to get all the likes from like list for that particular page. function LikeReply(CommentID) { var title = "Reply"; if ($("#lnkBtnReplyLike_" + CommentID)[0].innerText == "Like") { $.ajax({ url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/GetByTitle('LikesList')/items", type: "POST", data: JSON.stringify({ '__metadata': { 'type': 'SP.Data.LikesListListItem' }, 'LikeById': UserID, 'Title': title, 'CommentID': CommentID }), headers: { "accept": "application/json;odata=verbose", "X-RequestDigest": $("#__REQUESTDIGEST").val(), "content-Type": "application/json;odata=verbose" }, success: function(data) { $("#lnkBtnReplyLike_" + CommentID)[0].innerText = "Unlike"; }, error: function(error) { console.log(JSON.stringify(error)); } }); } else { $.ajax({ url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/GetByTitle('LikesList')/items(" + $("#ReplyLikeID_" + CommentID)[0].innerText + ")", type: "POST", async: false, headers: { "Accept": "application/json;odata=verbose", "X-Http-Method": "DELETE", "X-RequestDigest": $("#__REQUESTDIGEST").val(), "If-Match": "*" }, success: function(data) { $("#lnkBtnReplyLike_" + CommentID)[0].innerText = "Like"; getSetPageDet(); getAllComments(); }, error: function(data) { console.log(JSON.stringify(error)); } }); } } function LikeComment(CommentID) { var title = "Comment"; if ($("#lnkBtnLike_" + CommentID)[0].innerText == "Like") { $.ajax({ url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/GetByTitle('LikesList')/items", type: "POST", data: JSON.stringify({ '__metadata': { 'type': 'SP.Data.LikesListListItem' }, 'LikeById': UserID, 'Title': title, 'CommentID': CommentID }), headers: { "accept": "application/json;odata=verbose", "X-RequestDigest": $("#__REQUESTDIGEST").val(), "content-Type": "application/json;odata=verbose" }, success: function(data) { $("#lnkBtnLike_" + CommentID)[0].innerText = "Unlike"; }, error: function(error) { console.log(JSON.stringify(error)); } }); } else { $.ajax({ url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/GetByTitle('LikesList')/items(" + $("#LikeID_" + CommentID)[0].innerText + ")", type: "POST", async: false, headers: { "Accept": "application/json;odata=verbose", "X-Http-Method": "DELETE", "X-RequestDigest": $("#__REQUESTDIGEST").val(), "If-Match": "*" }, success: function(data) { $("#lnkBtnLike_" + CommentID)[0].innerText = "Like"; getSetPageDet(); getAllComments(); }, error: function(data) { console.log(JSON.stringify(error)); } }); } } function getSetCommentDet() { var commentIDColl = $('span[id^="CommentsID_"]'); $.each(commentIDColl, function(index, e) { var commentID = e.innerText; $.ajax({ url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/GetByTitle('LikesList')/items?$filter=LikeById eq " + UserID + " and CommentID eq '" + commentID + "'", type: "GET", headers: { "accept": "application/json;odata=verbose", "content-Type": "application/json;odata=verbose" }, success: function(data) { if (data.d.results.length > 0) { $("#lnkBtnLike_" + commentID)[0].innerText = "Unlike"; $("#LikeID_" + commentID)[0].innerText = data.d.results[0].Id; } else { $("#lnkBtnLike_" + commentID)[0].innerText = "Like"; } }, error: function(error) { alert(JSON.stringify(error)); } }); $.ajax({ url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/GetByTitle('LikesList')/items?$expand=LikeBy&$select=*,LikeBy/Title&$filter=CommentID eq '" + commentID + "' and Title eq 'Comment'&$orderby=ID desc", type: "GET", headers: { "accept": "application/json;odata=verbose", "content-Type": "application/json;odata=verbose" }, success: function(data) { if (data.d.results.length > 0) { var likeToolTip = ""; var me = false; $("#lblLikes_" + commentID)[0].innerText = data.d.results.length; $.each(data.d.results, function(index, value) { if (value.LikeById == UserID) { me = true; } else { likeToolTip += value.LikeBy.Title + "; "; } }); if (me == true) { likeToolTip = "You; " + likeToolTip; } $("span#spLikes_" + commentID).attr("title", likeToolTip); } else { $("#lblLikes_" + commentID)[0].innerText = "0"; } }, error: function(error) { alert(JSON.stringify(error)); } }); $.ajax({ url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/GetByTitle('CommentsList')/items?$expand=CommentBy&$select=*,CommentBy/Title&$filter=ParentCommentID eq '" + commentID + "' and Title eq 'Reply'&$orderby=ID desc", type: "GET", headers: { "accept": "application/json;odata=verbose", "content-Type": "application/json;odata=verbose" }, success: function(data) { if (data.d.results.length > 0) { $("#lblCommentReplies_" + commentID)[0].innerText = data.d.results.length + " Replies |"; var replyHTML = ""; if (data.d.results.length > 0) { $.each(data.d.results, function(index, value) { var CreatedDate = jQuery.format.date(value.Created, "ddd, MMM d, yyyy h:mm a"); replyHTML += "<tr><td><br><span id='ReplyID_" + value.Id + "' style='display:none;'>" + value.Id + "</span><br><span id='lblReply_" + value.Id + "' class='comments'>" + value.CommentText + "</span><br><a id='hrfReplyAuthor_" + value.Id + "' class='linksAuthor' href='/../../_layouts/15/userdisp.aspx?ID=" + value.CommentById + "' target='_blank'>" + value.CommentBy.Title + "</a><br><span id='lblReplydate_" + value.Id + "'>" + CreatedDate + "</span> <span id='spReplyLikes_" + value.Id + "' title='' class='LikeClass'> <img id='imgLike_" + value.Id + "' src='../_layouts/15/images/LikeFull.11x11x32.png?rev=23'> <span id='lblReplyLikes_" + value.Id + "' title='' class='LikeClass' ></span></span> <a id='lnkBtnReplyLike_" + value.Id + "' class='links' onClick='LikeReply(" + value.Id + ")'>Like</a><span id='ReplyLikeID_" + value.Id + "' style='display:none;'></span>"; if (UserID == value.CommentById) { replyHTML += " <a id='lnkBtnDelete_" + value.Id + "' class='links' onClick='DeleteReply(" + value.Id + ")'>Delete</a></td></tr>"; } else { replyHTML += "</td></tr>"; } }); $("#tblReply_" + commentID)[0].innerHTML = replyHTML; getSetReplyDet(); } } else { $("#lblCommentReplies_" + commentID)[0].innerText = "0 Replies |"; } }, error: function(error) { alert(JSON.stringify(error)); } }); }) } function getSetPageDet() { $.ajax({ url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/GetByTitle('LikesList')/items?$filter=LikeById eq " + UserID + " and PageURL eq '" + PageURL + "'", type: "GET", headers: { "accept": "application/json;odata=verbose", "content-Type": "application/json;odata=verbose" }, success: function(data) { if (data.d.results.length > 0) { $("#lnkBtnPageLike")[0].innerText = "Unlike"; $("#LikeID")[0].innerText = data.d.results[0].Id; } else { $("#lnkBtnPageLike")[0].innerText = "Like"; } }, error: function(error) { alert(JSON.stringify(error)); } }); $.ajax({ url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/GetByTitle('LikesList')/items?$expand=LikeBy&$select=*,LikeBy/Title&$filter=PageURL eq '" + PageURL + "' and Title eq 'Page'", type: "GET", headers: { "accept": "application/json;odata=verbose", "content-Type": "application/json;odata=verbose" }, success: function(data) { if (data.d.results.length > 0) { var likeToolTip = ""; var me = false; $("#lblPageLikes")[0].innerText = data.d.results.length; $.each(data.d.results, function(index, value) { if (value.LikeById == UserID) { me = true; } else { likeToolTip += value.LikeBy.Title + "; "; } }); if (me == true) { likeToolTip = "You; " + likeToolTip; } $("span#spLikes").attr("title", likeToolTip); } else { $("#lblPageLikes")[0].innerText = "0"; } }, error: function(error) { alert(JSON.stringify(error)); } }); $.ajax({ url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/GetByTitle('CommentsList')/items?$filter=PageURL eq '" + PageURL + "' and Title eq 'Comment'", type: "GET", headers: { "accept": "application/json;odata=verbose", "content-Type": "application/json;odata=verbose" }, success: function(data) { if (data.d.results.length > 0) { $("#lblPageComments")[0].innerText = data.d.results.length + " Comments |"; } else { $("#lblPageComments")[0].innerText = "0 Comments |"; } }, error: function(error) { alert(JSON.stringify(error)); } }); } function AddComment() { var title = "Comment"; var ParentCommentID = 0; var CommentText = $("#txtComments").val(); $.ajax({ url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/GetByTitle('CommentsList')/items", type: "POST", data: JSON.stringify({ '__metadata': { 'type': 'SP.Data.CommentsListListItem' }, 'PageURL': PageURL, 'CommentById': UserID, 'Title': title, 'ParentCommentID': ParentCommentID, 'CommentText': CommentText }), headers: { "accept": "application/json;odata=verbose", "X-RequestDigest": $("#__REQUESTDIGEST").val(), "content-Type": "application/json;odata=verbose" }, success: function(data) { $("#txtComments").val(""); getSetPageDet(); getAllComments(); }, error: function(error) { console.log(JSON.stringify(error)); } }); } function AddReply(CommentID) { var title = "Reply"; var CommentText = $("#txtReply_" + CommentID).val(); $.ajax({ url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/GetByTitle('CommentsList')/items", type: "POST", data: JSON.stringify({ '__metadata': { 'type': 'SP.Data.CommentsListListItem' }, 'CommentById': UserID, 'Title': title, 'ParentCommentID': CommentID, 'CommentText': CommentText }), headers: { "accept": "application/json;odata=verbose", "X-RequestDigest": $("#__REQUESTDIGEST").val(), "content-Type": "application/json;odata=verbose" }, success: function(data) { getSetPageDet(); getAllComments(); }, error: function(error) { console.log(JSON.stringify(error)); } }); } function LikePage() { var title = "Page"; var CommentID = 0; $.ajax({ url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/GetByTitle('LikesList')/items", type: "POST", data: JSON.stringify({ '__metadata': { 'type': 'SP.Data.LikesListListItem' }, 'PageURL': PageURL, 'LikeById': UserID, 'Title': title, 'CommentID': CommentID }), headers: { "accept": "application/json;odata=verbose", "X-RequestDigest": $("#__REQUESTDIGEST").val(), "content-Type": "application/json;odata=verbose" }, success: function(data) { $("#lnkBtnPageLike")[0].innerText = "Unlike"; getSetPageDet(); getAllComments(); }, error: function(error) { console.log(JSON.stringify(error)); } }); } function UnLikePage() { var title = "Page"; $.ajax({ url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/GetByTitle('LikesList')/items(" + $("#LikeID")[0].innerText + ")", type: "POST", async: false, headers: { "Accept": "application/json;odata=verbose", "X-Http-Method": "DELETE", "X-RequestDigest": $("#__REQUESTDIGEST").val(), "If-Match": "*" }, success: function(data) { $("#lnkBtnPageLike")[0].innerText = "Like"; }, error: function(data) { console.log(JSON.stringify(error)); getSetPageDet(); getAllComments(); } }); } function DeleteReply(CommentID) { $.ajax({ url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/GetByTitle('CommentsList')/items(" + CommentID + ")", type: "POST", async: false, headers: { "Accept": "application/json;odata=verbose", "X-Http-Method": "DELETE", "X-RequestDigest": $("#__REQUESTDIGEST").val(), "If-Match": "*" }, success: function(data) { DeleteChildItems(CommentID); console.log("Comment deleted successfully!"); getSetPageDet(); getAllComments(); }, error: function(data) { console.log(JSON.stringify(error)); } }); } function DeleteComment(CommentID) { $.ajax({ url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/GetByTitle('CommentsList')/items(" + CommentID + ")", type: "POST", async: false, headers: { "Accept": "application/json;odata=verbose", "X-Http-Method": "DELETE", "X-RequestDigest": $("#__REQUESTDIGEST").val(), "If-Match": "*" }, success: function(data) { DeleteChildItems(CommentID); console.log("Comment deleted successfully!"); getSetPageDet(); getAllComments(); }, error: function(data) { console.log(JSON.stringify(error)); } }); } function DeleteChildItems(parentId) { getItemsfromURL("/_api/web/lists/GetByTitle('LikesList')/items?$filter=CommentID eq " + parentId).done(function(data) { data.d.results.forEach(function(item) { var childId = item.ID; deleteItem("/_api/web/lists/GetByTitle('LikesList')/getItemById(" + childId + ")", item).done(function(d_data) { //deleted child item. }); }); }); getItemsfromURL("/_api/web/lists/GetByTitle('CommentsList')/items?$filter=ParentCommentID eq " + parentId).done(function(data) { data.d.results.forEach(function(item) { var childReplyId = item.ID; deleteItem("/_api/web/lists/GetByTitle('CommentsList')/getItemById(" + childReplyId + ")", item).done(function(d_data) { //deleted child item. }); getItemsfromURL("/_api/web/lists/GetByTitle('LikesList')/items?$filter=CommentID eq " + childReplyId).done(function(data) { data.d.results.forEach(function(item) { var childLikeId = item.ID; deleteItem("/_api/web/lists/GetByTitle('LikesList')/getItemById(" + childLikeId + ")", item).done(function(d_data) { //deleted child item. }); }); }); }); }); } //Get items function getItemsfromURL(url) { return $.ajax({ url: _spPageContextInfo.webAbsoluteUrl + url, type: "GET", headers: { "accept": "application/json;odata=verbose", } }); } //Delete Item function deleteItem(url, oldItem) { return $.ajax({ url: _spPageContextInfo.webAbsoluteUrl + url, type: "DELETE", headers: { "accept": "application/json;odata=verbose", "X-RequestDigest": $("#__REQUESTDIGEST").val(), "If-Match": oldItem.__metadata.etag } }); } function getSetReplyDet() { var commentIDColl = $('span[id^="ReplyID_"]'); $.each(commentIDColl, function(index, e) { var commentID = e.innerText; $.ajax({ url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/GetByTitle('LikesList')/items?$filter=LikeById eq " + UserID + " and CommentID eq '" + commentID + "'", type: "GET", headers: { "accept": "application/json;odata=verbose", "content-Type": "application/json;odata=verbose" }, success: function(data) { if (data.d.results.length > 0) { $("#lnkBtnReplyLike_" + commentID)[0].innerText = "Unlike"; $("#ReplyLikeID_" + commentID)[0].innerText = data.d.results[0].Id; } else { $("#lnkBtnReplyLike_" + commentID)[0].innerText = "Like"; } }, error: function(error) { alert(JSON.stringify(error)); } }); $.ajax({ url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/GetByTitle('LikesList')/items?$expand=LikeBy&$select=*,LikeBy/Title&$filter=CommentID eq '" + commentID + "' and Title eq 'Reply'", type: "GET", headers: { "accept": "application/json;odata=verbose", "content-Type": "application/json;odata=verbose" }, success: function(data) { if (data.d.results.length > 0) { var likeToolTip = ""; var me = false; $("#lblReplyLikes_" + commentID)[0].innerText = data.d.results.length; $.each(data.d.results, function(index, value) { if (value.LikeById == UserID) { me = true; } else { likeToolTip += value.LikeBy.Title + "; "; } }); if (me == true) { likeToolTip = "You; " + likeToolTip; } $("span#spReplyLikes_" + commentID).attr("title", likeToolTip); } else { $("#lblReplyLikes_" + commentID)[0].innerText = "0"; } }, error: function(error) { alert(JSON.stringify(error)); } }); }) } </script> |
Step 4
Create one web part page add your contents in to it. Add one more content editor web part -> edit web part -> give path of the txt file-> click on apply -> click on ok-> save the page -> publish it.
Step 5
After publishing the page you will see a comment box to add the comments and a like button to like the particular page.
Step 6
You can add the above code into any web part page using content editor and you will see the above functionality is working.
Print article | This entry was posted by Peter on September 27, 2018 at 8:41 am, and is filed under European SharePoint 2013 Hosting. Follow any responses to this post through RSS 2.0. You can leave a response or trackback from your own site. |