Monday 22 June 2015

As we are all SharePoint developers , we might get the requirement to get the user country who logged in to the site. We all know that SharePoint having the user profile data in AD. It has properties of the user like name, country,email, etc.,

Here i am posting now that how we can query the User Profile of the SharePoint to get the Country name of the logged-in user.

Refer the below Code:

This is the ready function where we call out function while page load:

jquery(document).ready(function ()
{
var thisUser = $().SPServices.SPGetCurrentUser();
var userLocation;
$().SPServices(
{
   operation: "GetUserProfileByName",
   async: false,
   AccountName: thisUser,
   completefunc: function (xData, Status)
    {
                  userCountry = getUserProfileValue(xData.responseXML, "country");
     alert(userCountry);
   
     }

    });
});

In this function we are querying the user profile by passing the parameters:

function getUserProfileValue(x, p) {

    var thisValue = $(x).SPFilterNode("PropertyData").filter(function() {
    return $(this).find("Name").text() == p;
  }).find("Value").text();
  return thisValue;
}                                           


Note that here i am using SPServices, so please do not forget to add the script tags of SPServices as well Jquery also.

Cheers.