Saturday, June 18, 2016


In Office 365 i have found issue and struggled to get out put due to incorrect calling of script tags

1. script tags should be loaded based on the sequence
2. Initially need to add jquery reference
3. loading sp.runtime.js referecnce
4. loading sp.js reference
5. finally custom javascripts


Sample html
---------------------
<!DOCTYPE html>

<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8" />
    <title></title>
    <script type="text/javascript"src="//code.jquery.com/jquery-1.11.1.min.js"></script>
    <script type="text/javascript" src="/sites/dev/Webparts/Chapter 8/JSOMGetSocialData.js"></script>
    <script type="text/javascript" src="/_layouts/15/sp.runtime.js"></script>
    <script type="text/javascript" src="/_layouts/15/sp.js"></script>
 
    <script type="text/javascript" src="/_layouts/15/sp.userprofiles.js"></script>
    <link rel="stylesheet" type="text/css" href="/sites/dev/Webparts/Chapter 8/JSOMGetSocialData.css" />


</head>
<body>
    <div id="WelcomeMessage">
        Welcome back <span id="WelcomeMessageUserName"></span>
    </div>
    <div id="UserFollows" />

</body>
</html>


Sample Javascript
--------------------------------


jQuery(document).ready(function () {
    SP.SOD.executeFunc('sp.js', 'SP.ClientContext', runthiscode);
 
});
function runthiscode() {

    // Ensure that the SP.UserProfiles.js file is loaded before the custom code runs.  
    SP.SOD.executeFunc('SP.UserProfiles.js', 'SP.UserProfiles', getUserProfileProperties);
}


function getUserProfileProperties() {
    var clientContext = new SP.ClientContext.get_current();

    var peopleManager = new SP.UserProfiles.PeopleManager(clientContext);
    this.userProperties = peopleManager.getMyProperties();
    clientContext.load(userProperties);

    var followingManager = new SP.Social.SocialFollowingManager(clientContext);
    this.following = followingManager.getFollowed(15);

    clientContext.executeQueryAsync(
    Function.createDelegate(this, this.onQuerySucceeded),
    Function.createDelegate(this, this.onQueryFailed)
    );
}

function onQuerySucceeded() {
    jQuery("#WelcomeMessageUserName").text(userProperties.get_displayName());

    var followedItems = "Items you are following:<br />";
    jQuery.each(following, function (index, value) {
        followedItems += "<a href='" + value.get_uri() + "'>" + value.get_name() +
                         "</a><br />";
    });
    jQuery("#UserFollows").append(followedItems);
}

function onQueryFailed(sender, args) {
    alert('Request failed. ' + args.get_message() +
        '\n' + args.get_stackTrace());
}

No comments:

Post a Comment