user3589162
user3589162

Reputation: 79

How to get attribute value of node in xml using xpath in javascript

Hi I am trying to get the user profile properties from sharepoint on client side with javascript.But I am not getting the value of nodes in xml. How to get them. the xml will look like as:

How to get attribute value of node in xml using xpath

Here I want to get the value which is between <name> tags <Name>AccountName</Name> and between Name tags

want to get the value = abc what will be the xpath expression

Please help

<?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">
  <soap:Body>
    <GetUserProfileByNameResponse xmlns="http://microsoft.com/webservices/SharePointPortalServer/UserProfileService">
      <GetUserProfileByNameResult>
        <Pro pertyData>
          <IsPrivacyChanged>false</IsPrivacyChanged>
          <IsValueChanged>false</IsValueChanged>
          <Name>UserProfile_GUID</N ame>
            <Privacy>NotSet</Privacy>
            <Values>
              <ValueData>
                <Value xmlns:q1="http://microsoft.com/wsdl/types/" xsi:type="q1:guid">8ed84415-7330-4857-a7d2- d797d71c439f

                </Value>
              </ValueData>
            </Values>
            </PropertyData>
            <PropertyData>
              <IsPrivacyChanged>false</IsPrivacyChanged>
              <Is ValueChanged>false</IsValueChanged>
                <Name>AccountName</Name>
                <Privacy>NotSet</Privacy>
                <Values>
                  <ValueData>
                    <Value xsi:type="xsd:string">abc

                    </Value>
                  </ValueData>
                </Values>
            </PropertyData>
      </GetUserProfileByNameResult>
    </GetUserProfileByNameResponse>
    </ soap:Body>
</soap:Envelope>

Please help me in this.

Upvotes: 1

Views: 648

Answers (2)

Dhaval Shah
Dhaval Shah

Reputation: 19

You have to traverse through the xml nodes returned from the SPServices. I have written a function for getting the desired user profile property.

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

Further to query the user property you just need to call like below,

getUPValue(xData.responseXML, "WorkEmail");  

These article provides a detail overview of it over here

Upvotes: 0

Vadim Gremyachev
Vadim Gremyachev

Reputation: 59318

var propertyData = $(responseXML).find("PropertyData").filter(function(e){
    return $(this).find("Name").text() == "AccountName";
});
var value = propertyData.length > 0 ? propertyData.find('Value').text() : '';

Since you are trying to retrieve user profile via SharePoint Web Services I would recommend to utilize SPServices library, it hides (almost)all the intricacies when working with SharePoint Web Services from JavaScript. The following example demonstrates how to retrieve user profile using GetUserProfileByName method and process the results:

function getUserProfile(accountName,completeFn) {
  var userInfo = {};
  $().SPServices({
    AccountName: accountName,  
    operation: 'GetUserProfileByName',
    completefunc: function (xData, Status) {
      $(xData.responseXML).SPFilterNode("PropertyData").each(function() {
        userInfo[$(this).find("Name").text()] = $(this).find("Value").text();
      }); 
      completeFn(userInfo);
    }
  });
}

var loginName = 'i:0#.f|membership|[email protected]';
getUserProfile(loginName,function(info){
    console.log(info);    
});

Upvotes: 1

Related Questions