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

Sathish Nadarajan
 
Solution Architect
March 16, 2015
 
Rate this article
 
Views
13199

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.

Author Info

Sathish Nadarajan
 
Solution Architect
 
Rate this article
 
Sathish is a Microsoft MVP for SharePoint (Office Servers and Services) having 15+ years of experience in Microsoft Technologies. He holds a Masters Degree in Computer Aided Design and Business ...read more
 

Leave a comment