Query user profile using JavaScript
I got a requirement where when the user selected any person in people picker we are required to show user information by querying user profile. Here the challenge is the people picker control does not have any post back event except one client side event i.e., AfterCallbackClientScript.
Now one more challenge is we have to get user profile information in JavaScript then I found web service option. Here is the code snippet for this.
First in the page load method we have to set JavaScript method to the client side event.
Below is the definition for above JavaScript method. Here getProfileInfo method by default expect two arguments.
senderId - people picker control ID
data - people picker control entities data
Now one more challenge is we have to get user profile information in JavaScript then I found web service option. Here is the code snippet for this.
First in the page load method we have to set JavaScript method to the client side event.
peAuthorName.AfterCallbackClientScript
= "getProfileInfo";
Below is the definition for above JavaScript method. Here getProfileInfo method by default expect two arguments.
senderId - people picker control ID
data - people picker control entities data
//get the
profile information of selected user.
function getProfileInfo(senderId, data) {
var xmlDoc;
var userName;
if (window.DOMParser) {
parser = new DOMParser();
xmlDoc = parser.parseFromString(data, "text/xml");
}
else // Internet
Explorer
{
xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async = "false";
xmlDoc.loadXML(data);
}
var elmEntry = xmlDoc.getElementsByTagName("DictionaryEntry");
for (i = 0; i < elmEntry.length; i++) {
if (elmEntry[i].childNodes[0].text ==
"AccountName") {
userName = elmEntry[i].childNodes[1].text;
break;
}
}
fetchUserData(userName);
}
//fetch and populate the profile data using web
service.
function fetchUserData(username) {
try {
var a = null;
//IE
if (window.ActiveXObject) {
a = new ActiveXObject("Microsoft.XMLHTTP");
}//Mozilla, chrome, etc.,
else if
(window.XMLHttpRequest) {
a = new XMLHttpRequest();
}
else {
throw new Error("XMLHttpRequest not supported");
}
if (a == null) return null;
a.Open("POST", "/_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>"
+ username + "</AccountName></GetUserProfileByName></soap:Body></soap:Envelope>";
a.Send(d);
var xmlDoc = a.responseXML;
var strAccountName = "";
var strAuthorID = "";
var strExtn = "";
var strGroupCode = "";
var strPBG =
"";
var
strManager1 = "";
var strManagerName = "";
var strLocation = "";
var elmValue =
xmlDoc.getElementsByTagName("Value");
for (i = 0; i < elmValue.length; i++)
{
if
(elmValue[i].parentNode.parentNode.parentNode.childNodes[2].text == 'AccountName') {
strAccountName = elmValue[i].childNodes[0].nodeValue;
}
else if
(elmValue[i].parentNode.parentNode.parentNode.childNodes[2].text == 'PSID') {
strAuthorID = elmValue[i].childNodes[0].nodeValue;
}
else if
(elmValue[i].parentNode.parentNode.parentNode.childNodes[2].text == 'WorkPhone') {
strExtn = elmValue[i].childNodes[0].nodeValue;
}
else if
(elmValue[i].parentNode.parentNode.parentNode.childNodes[2].text == 'GroupCode') {
strGroupCode = elmValue[i].childNodes[0].nodeValue;
}
else if
(elmValue[i].parentNode.parentNode.parentNode.childNodes[2].text == 'PBG') {
strPBG = elmValue[i].childNodes[0].nodeValue;
}
else if
(elmValue[i].parentNode.parentNode.parentNode.childNodes[2].text == 'Manager') {
strManager1 = elmValue[i].childNodes[0].nodeValue;
}
else if
(elmValue[i].parentNode.parentNode.parentNode.childNodes[2].text == 'Office') {
strLocation = elmValue[i].childNodes[0].nodeValue;
}
}
//querying manager's manager
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>"
+ strManager1 + "</AccountName></GetUserProfileByName></soap:Body></soap:Envelope>";
var b = null;
if (window.ActiveXObject) {
b = new ActiveXObject("Microsoft.XMLHTTP");
}
else if
(window.XMLHttpRequest) {
b = new XMLHttpRequest();
}
else {
throw new Error("XMLHttpRequest not supported");
}
if (b == null) return null;
b.Open("POST", "/_vti_bin/userprofileservice.asmx", false);
b.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
b.setRequestHeader("SOAPAction",
"http://microsoft.com/webservices/SharePointPortalServer/UserProfileService/GetUserProfileByName");
b.Send(d);
xmlDoc = null;
xmlDoc = b.responseXML;
elmValue = null;
elmValue = xmlDoc.getElementsByTagName("Value");
for (i = 0; i < elmValue.length; i++)
{
if
(elmValue[i].parentNode.parentNode.parentNode.childNodes[2].text == 'PreferredName') {
strManagerName = elmValue[i].childNodes[0].nodeValue;
break;
}
}
var lblAuthorID =
document.getElementById('<%=lblAuthorID.ClientID
%>');
var lblAuthorWorkExtn =
document.getElementById('<%=lblAuthorWorkExtn.ClientID
%>');
var lblAuthorGroupCode =
document.getElementById('<%=lblAuthorGroupCode.ClientID
%>');
var lblAuthorPBG =
document.getElementById('<%=lblAuthorPBG.ClientID %>');
var lblAuthorManager1 =
document.getElementById('<%=lblAuthorManager1.ClientID
%>');
var lblAuthorLocation =
document.getElementById('<%=lblAuthorLocation.ClientID
%>');
var hdnAuthorName =
document.getElementById('<%=hdnAuthorName.ClientID %>');
var hdnAuthorID =
document.getElementById('<%=hdnAuthorID.ClientID
%>');
var hdnAuthorWorkExtn =
document.getElementById('<%=hdnAuthorWorkExtn.ClientID
%>');
var hdnAuthorGroupCode =
document.getElementById('<%=hdnAuthorGroupCode.ClientID
%>');
var hdnAuthorPBG =
document.getElementById('<%=hdnAuthorPBG.ClientID %>');
var hdnAuthorManager1 =
document.getElementById('<%=hdnAuthorManager1.ClientID
%>');
var hdnAuthorLocation =
document.getElementById('<%=hdnAuthorLocation.ClientID
%>');
lblAuthorID.innerText = strAuthorID;
lblAuthorWorkExtn.innerText = strExtn;
lblAuthorGroupCode.innerText = strGroupCode;
lblAuthorPBG.innerText = strPBG;
lblAuthorManager1.innerText = strManagerName;
lblAuthorLocation.innerText = strLocation;
hdnAuthorName.value = strAccountName;
hdnAuthorID.value = strAuthorID;
hdnAuthorWorkExtn.value = strExtn;
hdnAuthorGroupCode.value = strGroupCode;
hdnAuthorPBG.value = strPBG;
hdnAuthorManager1.value = strManager1;
hdnAuthorLocation.value = strLocation;
}
catch
(err) {
alert(err.description);
}
Happy coding..
Comments
Post a Comment