Thursday, 6 August 2015

Working with REST api for Online SharePoint 2013 - Get User Profile Properties

Get User Profile Properties using REST api:

Script:
<script src="/sites/ithub/Style%20Library/js/jquery-1.10.1.min.js"></script>

<script>
$.ajax({ url: "SiteURL/_api/SP.UserProfiles.PeopleManager/GetMyProperties",
                    headers: { Accept: "application/json;odata=verbose" },
                    success: function (data) {
                        try {
                            //Get properties from user profile Json response
                            if (data.d.AccountName) {
                                $('#AccountName').text(data.d.AccountName);
                                userProfilepercent += incrmntValue;
                            }
                            if (data.d.DisplayName) {
                                $('#userDisplayName').text(data.d.DisplayName);
                                userProfilepercent += incrmntValue;
                            }
                            if (data.d.Email) {
                                $('#Email').text(data.d.Email);
                                userProfilepercent += incrmntValue;
                            }
                            for (var i = 0; i < data.d.UserProfileProperties.results.length; i++) {
                                if (data.d.UserProfileProperties.results[i].Key == "Department") {
                                    var Department = data.d.UserProfileProperties.results[i].Value;
                                    if (Department) {
                                        $('#Department').text(Department);
                                        userProfilepercent += incrmntValue;
                                    }
                                }
                                if (data.d.UserProfileProperties.results[i].Key == "WorkPhone") {
                                    var workPhone = data.d.UserProfileProperties.results[i].Value;
                                    if (workPhone) {
                                        $('#WorkPhone').text(workPhone);
                                        userProfilepercent += incrmntValue;
                                    }
                                }
                                if (data.d.UserProfileProperties.results[i].Key == "UserName") {
                                    var userName = data.d.UserProfileProperties.results[i].Value;
                                    if (userName) {
                                        $('#UserName').text(userName);
                                        userProfilepercent += incrmntValue;
                                    }
                                }
                                if (data.d.UserProfileProperties.results[i].Key == "AboutMe") {
                                    var aboutMe = data.d.UserProfileProperties.results[i].Value;
                                    if (aboutMe) {
                                        $('#AboutMe').text(aboutMe);
                                        userProfilepercent += incrmntValue;
                                    }
                                }
                                if (data.d.UserProfileProperties.results[i].Key == "Manager") {
                                    var manager = data.d.UserProfileProperties.results[i].Value;
                                    if (manager) {
                                        $('#Manager').text(manager);
                                        userProfilepercent += incrmntValue;
                                    }
                                }
                                if (data.d.UserProfileProperties.results[i].Key == "CellPhone") {
                                    var cellPhone = data.d.UserProfileProperties.results[i].Value;
                                    if (cellPhone) {
                                        $('#CellPhone').text(cellPhone);
                                        userProfilepercent += incrmntValue;
                                    }
                                }
                                if (data.d.UserProfileProperties.results[i].Key == "WebSite") {
                                    var webSite = data.d.UserProfileProperties.results[i].Value;
                                    if (webSite) {
                                        $('#WebSite').text(webSite);
                                        userProfilepercent += incrmntValue;
                                    }
                                }
                                if (data.d.UserProfileProperties.results[i].Key == "PictureURL") {
                                    pictureURL = data.d.UserProfileProperties.results[i].Value;                                  
                                }
                                //option.text = data.d.UserProfileProperties.results[i].Key;
                                //option.value = data.d.UserProfileProperties.results[i].Value;
                               
                            }
                            //alert(userProfilepercent);
                            changeProfilepercent(userProfilepercent);
                        } catch (err2) {
                            alert(JSON.stringify(err2));                           
                        }
                    },
                    error: function (jQxhr, errorCode, errorThrown) {
                        alert(errorThrown);                       
                    }
                });
</script>


HTML:
<h2><strong>Employee Details</strong></h2>
        <br />
        AccountName:            <span id="AccountName"></span><br />
        User Name:              <span id="UserName"></span><br />
        DisplayName:            <span id="userDisplayName"></span><br />
        About Me:               <span id="AboutMe"></span><br />
        Manager:                <span id="Manager"></span><br />
        Email Address:          <span id="Email"></span><br />
        Department:             <span id="Department"></span><br />
        Work Phone Number:      <span id="WorkPhone"></span><br />
        Cell Phone Number:      <span id="CellPhone"></span><br />
        Web Site:               <span id="WebSite"></span><br />
 

Working with REST api for Online SharePoint 2013 - Get All List Items From a Single List

Get All List Items From a Single List:

Script:
 <script src="/sites/ithub/Style%20Library/js/jquery-1.10.1.min.js"></script>
var listurlitem = "
SiteURL/_api/lists/getbytitle('ListName')/items";
                var html = "<br/><b><h2>Getting Items from List</h2><b/><br/>";
                $.ajax({
                    url: listurlitem,
                    type: "GET",
                    headers: {
                        "accept": "application/json;odata=verbose",
                    },
                    success: function (data) {
                        $.each(data.d.results, function (i, result) {
                            html  += result.Title +"<br/>";
                        });
                        $('#listitems').append($(html));
                    },
                    error: function (error) {
                        alert(JSON.stringify(error));
                        html += error;
                        $('#listitems').append($(html));
                    }
                });
Html:
<div id="listitems">
</div>
 

Happy coding :)