SharePoint 2016 provides better social experience features. We can follow the users and the sites, which we can see in the My Site personal page. In order to work with the social features, SharePoint has provided the SP.UserProfiles.js file. Before programmatically following a user or a site, we should ensure that SP.UserProfiles.js is loaded in the page.
In this blog, we will see how to programmatically follow a site, using JavaScript Object Model.
- <script language="javascript" type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>
- <script language="javascript" type="text/javascript">
- $(document).ready(function() {
- var scriptbase = _spPageContextInfo.webServerRelativeUrl + "/_layouts/15/";
- $.getScript(scriptbase + "SP.Runtime.js", function () {
- $.getScript(scriptbase + "SP.js", function(){
- $.getScript(scriptbase + "SP.UserProfiles.js", followSite);
- });
- });
- });
- var followingManager,actorInfo;
- function followSite() {
-
-
-
- var clientContext = new SP.ClientContext();
- followingManager = new SP.Social.SocialFollowingManager(clientContext);
-
-
- siteActorInfo = new SP.Social.SocialActorInfo();
- siteActorInfo.set_contentUri("http://sharepoint2016/sites/HOL");
- siteActorInfo.set_actorType(SP.Social.SocialActorType.site);
- followingManager.follow(siteActorInfo);
-
-
- clientContext.executeQueryAsync(QuerySuccess, QueryFailure);
- }
-
- function QuerySuccess() {
- console.log("The site has been followed.");
- }
-
- function QueryFailure(sender,args) {
- console.log('Request failed'+ args.get_message());
- }
- </script>
We can add the code, given above to Content Editor Web part and see it in action. Going to the My Sites page, we can see the list of the sites, given below.
Summary
Thus, we saw how to follow a site in SharePoint Server 2016, using JavaScript Object Model.