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
// http://amitkumarmca04.blogspot.com/sites/MYSITE/AMIT.aspx
var siteCompleteURL=document.URL;

//Split the complete URL from ‘//’
// componentList[0]=’http:’
// componentList[1]= ‘amitkumarmca04.blogspot.com/sites/MYSITE/AMIT.aspx’
componentList=siteCompleteURL.split('//');

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



var siteURL = componentList[0] + "//" + siteURL[0] + "/" + "sites/ MYSITE /";
// siteURL =’http:’ + "//" + ‘amitkumarmca04.blogspot.com’ + "/" + ‘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", " http://microsoft.com/webservices/SharePointPortalServer/UserProfileService/GetUserProfileByName");


var d =
"<?xml version=\"1.0\" encoding=\"utf-8\"?><soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:tns=\"http://schemas.microsoft.com/sharepoint/soap/\"><soap:Body>< GetUserProfileByName xmlns=\"http://microsoft.com/webservices/SharePointPortalServer/UserProfileService\"> <AccountName></AccountName></ GetUserProfileByName></soap:Body></soap:Envelope>";

a.Send(d);

var xmlDoc=a.responseXML;

for(i=0;i<4;i++)
{
if(xmlDoc.getElementsByTagName("Name")[i].childNodes[0].nodeValue=='AccountName')
{
strAccountName=xmlDoc.getElementsByTagName("Value")[i].childNodes[0].nodeValue;
}
else if(xmlDoc.getElementsByTagName("Name")[i].childNodes[0].nodeValue=='FirstName')
{
strFirstName=xmlDoc.getElementsByTagName("Value")[i].childNodes[0].nodeValue;
}
else if(xmlDoc.getElementsByTagName("Name")[i].childNodes[0].nodeValue=='LastName')
{
strLastName=xmlDoc.getElementsByTagName("Value")[i].childNodes[0].nodeValue;
}

}


}

fetchUserData ();
document.write(“Account Name=” + strAccountName);
document.write(“First Name=” + strFirstName);
document.write(“Last Name=” + strLastName);
</script>
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 :).
Share:

1 comments:

Anonymous said...

Hi,

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

xmlDoc.getElementsByTagName("Name")[i].childNodes[0].nodeValue

Note: I have published the master page.

Please help.
Thanks,
Sam

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

Popular

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 http://amitkumarmca04.blogspot.com/. Amit have experience in Sitecore, ASP.Net with C#, MVC, SharePoint, Classic ASP, VB,Macros,HTML,JavaScript,JSON,BootStrap and AngularJS.

Followers

Follow me on Twitter

Labels