How to read the User Profile Property in SharePoint 2013 using JQuery Ajax.


Sathish Nadarajan
SharePoint MVP
Published On :   16 Mar 2015
Visit Count
Today :  3    Total :   9716
Plan, Migrate, Secure, Report
SharePoint & Office 365 Tool. Simple & Easy to Use. 15-Day Trial!

SharePoint Office 365 Tool
Simple & Powerful Tool for Migration, Security & Reporting. Free Trial


I met with an interesting requirement like, on the EditForm.aspx, we need to update some of the values based on the User Profile Property. As all of us know that the EditForm.aspx is a SitePage, from which we need to retrieve the value using the Javascript alone. Hence, I added a script editor webpart on the page and kept the below code over there.

 function GetProfileProperty() {
     $.ajax({
         url: "/_api/sp.userprofiles.peoplemanager/getmyproperties",
         type: "GET",
         headers: { "accept": "application/json;odata=verbose" },
         success: function (msg) {
             var x = msg.d.UserProfileProperties.results;
             for (var i = 0; i < x.length; i++) {
                          
                 if (x[i].Key == "SPS-School") {
                     alert(x[i].Value);
                     var inDesignMode = document.forms[MSOWebPartPageFormName].MSOLayout_InDesignMode.value;
                     if (x[i].Value == "TVS") {
                        if (inDesignMode == "1") {
                             // page is in edit mode
 
 // Do the Logics Here.
                         }
                         else {
                             // page is in browse mode
                         }
                     }
                     else {
                         if (inDesignMode == "1") {
                             // page is in edit mode
                             // Do the Logics here.
                         }
                         else {
                             // page is in browse mode
                         }
                     }
                 }
             }
         },
         error: function (msg) {
             alert(msg.responseText);
         } });
 }
 
 

In this example, I took the Property “SPS-School” and compared against the School “TVS” – The one where I studied..

Happy Coding.

Sathish Nadarajan.

SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Categories

Migratiin Tools for SharePoint