Wednesday, July 23, 2008

How access SharePoint (MOSS) User Profile Web Service using JavaScript in SharePoint (MOSS) Content Editor Web Part:

How access SharePoint (MOSS) User Profile Web Service using JavaScript in SharePoint (MOSS) Content Editor Web Part:
I tried this when I had the requirements in which we need to pass the user profile information let’s say Account Name (User NTID), First Name and Last Name to the some other function in JAVA SCRIPT. For accessing current logged in user information you need to call GetUserProfileByName function of userprofileservice web service.
I have written a JavaScript code for accessing the GetUserProfileByName function of userprofileservice web service:

<script type="text/javascript" language="javascript">

//Complete URL of the page in which you write this code
var siteCompleteURL=document.URL;

//Split the complete URL from ‘//’
// componentList[0]=’http:’
// componentList[1]= ‘’

//Split the URL (without //) with ‘/’
// siteURL[0]= ‘’
// siteURL[1]=’ sites’
// siteURL[2]=’ MYSITE’
// siteURL[3]=’ AMIT.aspx’
var baseURL = componentList[0] + "//" + siteURL[0] + "/";
// baseURL=’http:’ + "//" + ‘’ + "/";

var siteURL = componentList[0] + "//" + siteURL[0] + "/" + "sites/ MYSITE /";
// siteURL =’http:’ + "//" + ‘’ + "/" + ‘sites/MYSITE/’

var strAccountName="";
var strFirstName=""
var strLastName=""
var str = '';

function GetRootUrl()
return siteURL;

function fetchUserData()
var a = new ActiveXObject("Microsoft.XMLHTTP");

if(a == null) return null;

a.Open("POST", GetRootUrl() + _vti_bin/userprofileservice.asmx", false);
a.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
a.setRequestHeader("SOAPAction", "");

var d =
"<?xml version=\"1.0\" encoding=\"utf-8\"?><soap:Envelope xmlns:soap=\"\" xmlns:xsi=\"\" xmlns:xsd=\"\" xmlns:tns=\"\"><soap:Body>< GetUserProfileByName xmlns=\"\"> <AccountName></AccountName></ GetUserProfileByName></soap:Body></soap:Envelope>";


var xmlDoc=a.responseXML;

else if(xmlDoc.getElementsByTagName("Name")[i].childNodes[0].nodeValue=='FirstName')
else if(xmlDoc.getElementsByTagName("Name")[i].childNodes[0].nodeValue=='LastName')



fetchUserData ();
document.write(“Account Name=” + strAccountName);
document.write(“First Name=” + strFirstName);
document.write(“Last Name=” + strLastName);
In the soap of this web service, when you are not passing any value for the tag <AccountName></AccountName>, then it will return you to the details of current logged in user, if you pass any value between this tag then it will return you to the details of the user for which you have passed the value. This value for this tag would be the NTID of the user e.g. MYCOMPUTER\AMITKUMAR.

Happy Coding :).


Anonymous said...


I am trying to use this code in to my master page.The code works successfully when I login as an administrator.The code is not working when I login as any other user.The code gets blocked after executing the line below


Note: I have published the master page.

Please help.

Sitecore® Technology MVP 2019

Featured Post

Sitecore Error Troubleshooting: Sitecore Solr Connectivity Issue:: Part-1

Sitecore Error Troubleshooting: Sitecore Solr Connectivity Issue:: Part-1 How to verify Sitecore 9.1 (IaaS and Azure PaaS) + Solr (IaaS) C...

Total Pageviews

State Counder


About Me

My photo
Based in Bangalore, INDIA, Amit is a Sitecore MVP and MCC. He has an MCA from the U.P.T.U., INDIA and holds certifications including Sitecore and SharePoint 2007/2010. He is an contributor to Sitecore/MSDN communities and Amit have experience in Sitecore, ASP.Net with C#, MVC, SharePoint, Classic ASP, VB,Macros,HTML,JavaScript,JSON,BootStrap and AngularJS.


Follow me on Twitter