Unable to find node "user" in xml response in GetUserCollectionFromGroup

Apr 24, 2011 at 6:40 AM
Edited Apr 27, 2011 at 1:24 AM

Here's my code -



<script type="text/javascript">
$(document).ready(function() {
var thisSite = $().SPServices.SPGetCurrentSite();
$("#jQueryDiv").html("<-- jQuery on -->" + "<br>" + "Your site : " + thisSite);
beginLoad();
});

function beginLoad(){
 $().SPServices({
    webURL: https://something.com,
    operation: "GetGroupCollectionFromSite",
    async: false,
    completefunc: processResults
          });

function processResults(xData, Status) {
    $('#jQueryDiv').html("");
    var count = 0;
    var liHtml = "<table border=1px cellpadding=1 cellspacing=1><tr><td><b>#</b></td><td><b>Group ID</b></td><td><b>Group Name</b></td><td><b>Group Description</b></td><td><b>Users</b></td></tr>";
    $(xData.responseXML).find("[nodeName='Group']").each(function() {
   // Do something here if there are items returned
      count = count + 1;          
      liHtml += "<tr><td>" + count + 
               "</td><td>" + $(this).attr("ID") + 
               "</td><td>" + $(this).attr("Name") + 
               "</td><td>" + $(this).attr("Description") +
               "</td><td>" + "<INPUT TYPE=\"BUTTON\" VALUE=\"Get Users\" onclick =\"gtGetUsersCollectionFromGroup('"+ $(this).attr('Name')+"')\">" + "</td></tr>";
            });       
      $('#jQueryDiv').append(liHtml + "</table>"); 
   }   
}

//////////////////////////////////////////////////////

function gtGetUsersCollectionFromGroup(grpName){
 var count = 0;
alert(grpName);
 var liHtml1 = "<table border=0 cellpadding=1 cellspacing=1><tr><td><b>#</b></td><td><b>UserID</b></td><td><b>User Name</b></td><td><b>User Email</b></td></tr>";
$().SPServices({
    webURL: "https://something.com",
    operation: "GetUserCollectionFromGroup",
    groupName: grpName,
    async: false,
    completefunc: processGroupPermsResults,
    dataType: "xml"
          });
}

function processGroupPermsResults(xDataG, Status){
$('#loadGrp').html("");
alert(xDataG.responseText);
var out = $().SPServices.SPDebugXMLHttpResult({
			node: xDataG.responseXML
		});
$('#loadGrp').append(out);
$(xDataG.responseXML).find("[nodeName='User']").each(function() {
      count = count + 1;
      alert("hi");
      liHtml1 += "<tr><td>" + count + 
               "</td><td>" + $(this).attr("ID") + 
               "</td><td>" + $(this).attr("Name") + 
               "</td><td>" +  $(this).attr("Email") +
               "</td></tr>";
            });       
      liHtml1 = liHtml1 + "</table>";
      alert(liHtml1); 
   }
</script>

<b>Get All Groups under this site collection</b><br>
<div id="jQueryDiv"><IMG ID="waitSymbol" src="/_layouts/images/GEARS_AN.GIF" />
</div>
<div id="loadGrp"></div>


The higlighted yellow line doesn't work. I can get the responseText and using debug correct results, but unable to find node='user' in the reponsexml. What am I doing wrong ?
Any help/suggestions is appreciated.
Thanks in advance.
Coordinator
Apr 24, 2011 at 1:56 PM

Possibly just an error in the copy/paste, but you are missing a closing } on your beginLoad function.

I'm assuming you are seeing the ouput for SPDebugXMLHttpResult? Can you show us what that output looks like (obviously scrub out the necessary user data)?

Apr 24, 2011 at 4:11 PM

Thank you so much for taking a look into this -

here's what i get from debug o/p -

#document 
xml version="1.0" encoding="utf-8" 
0 version 1.0 
1 encoding utf-8 
 
soap:Envelope 
0 xmlns:soap http://schemas.xmlsoap.org/soap/envelope/ 
1 xmlns:xsi http://www.w3.org/2001/XMLSchema-instance 
2 xmlns:xsd http://www.w3.org/2001/XMLSchema 
 
soap:Body 
 
GetUserCollectionFromGroupResponse 
0 xmlns http://schemas.microsoft.com/sharepoint/soap/directory/ 
 
GetUserCollectionFromGroupResult 
 
GetUserCollectionFromGroup 
 
Users 
 
User   
0 ID 34
1 Sid M-1-dummy-data 
2 Name test user 
3 LoginName domain\userid 
4 Email username@something.com 
5 Notes  
6 IsSiteAdmin False 
7 IsDomainGroup False 
 
User   
0 ID 26
1 Sid M-1-dummy-data 
2 Name test user 
3 LoginName domain\userid 
4 Email username@something.com 
5 Notes  
6 IsSiteAdmin False 
7 IsDomainGroup False
 
User   
0 ID 567
1 Sid M-1-dummy-data 
2 Name test user 
3 LoginName domain\userid 
4 Email username@something.com 
5 Notes  
6 IsSiteAdmin False 
7 IsDomainGroup False 
 
 
 I get the expected result and can verify using alert - reponseText and SPDebugXMLHttpResult. However, above indicated yellow line doesn't work and control doesn't go inside each loop. I suspect if jQuery is able to find the node "User".
If you see, I am using the same code for getting all group from the site in the above beginLoad function. My objective is to display all the groups of the site including the all the users for respective groups in a simple html table.
Please suggest if where i am going wrong.
Thanks,
Shashank
 
 
 
 
 

Coordinator
Apr 25, 2011 at 5:36 PM

Are you seeing your "hi" alerts, or does the script bomb out at that point? Do you see any script errors or anything in your debug tools?

Your script looks correct based on the debug output you posted. You haven't put anything in to actually append the record to the table.

Just another though, my personal approach would be to append each record to the table vs. building a big variable. Unless you need it in a variable for some reason. This way you don't have to put all of that logic in to add the new record to the previous one, and append the closing table tag at the end.

Say you have your table as below:

<table id="outputTable">
  <tr>
    <th></th><th>ID</th><th>Name</th><th>E-Mail</th>
  </tr>
</table>

You can simply append() each record to #outputTable; modified version of your function below:

function processGroupPermsResults(xDataG, Status){
	$('#loadGrp').html("");
	alert(xDataG.responseText);
	var out = $().SPServices.SPDebugXMLHttpResult({
		node: xDataG.responseXML
	});
	
	$('#loadGrp').append(out);
	
	var count = 1;
	
	$(xDataG.responseXML).find("[nodeName='User']").each(function() {
		count = count + 1;
		alert("hi");
		$("#outputTable").append("<tr><td>" + count + "</td><td>" + $(this).attr("ID") + "</td><td>" + $(this).attr("Name") + "</td><td>" +  $(this).attr("Email") + "</td></tr>");
	}); 
}

Apr 26, 2011 at 1:38 AM

Thanks for the suggestion and making correction in the function.

No I am not able to see "hi". I had kept it purposely to check if the control goes insisde the each function loop. There are no javascript errors prompting at the status bars or in debug tools.

Not sure if I need find some other workaround for looping through XML response. Let me know if you find some fix on this.

Again thanks for answering to the discussion promptly.

Thanks,

Shashank

Coordinator
Apr 26, 2011 at 2:12 AM

Is your "processResults" function working for the groups? There was an issue with an older version of jQuery that affected the use of "nodeName", but if that other function is working then that can't be it.

Everything else looks right to me, so I'm not sure what it would be.

Apr 26, 2011 at 2:58 AM

yes..processResults is working properly and gets me all groups. I will try to find the issue and let you know about the same.

Thanks for your help.

Shashank

Coordinator
Apr 26, 2011 at 8:53 PM
Edited Apr 26, 2011 at 10:37 PM

Shashank:

I was just fiddling around with this in my environment. This seems to work:

<script language="javascript" type="text/javascript" src="jQuery%20Libraries/jquery-1.5.min.js"></script>
<script language="javascript" type="text/javascript" src="jQuery%20Libraries/jquery.SPServices-0.6.1.js"></script>
<script language="javascript" type="text/javascript">
 $(document).ready(function() {
  var thisSite = $().SPServices.SPGetCurrentSite();
  $("#jQueryDiv").html("<-- jQuery on -->" + "<br>" + "Your site : " + thisSite);
  beginLoad();
 });
 function beginLoad(){
   $('#jQueryDiv').html("");
   var count = 0;
   var liHtml = "<table border=1px cellpadding=1 cellspacing=1><tr><td><b>#</b></td><td><b>Group ID</b></td><td><b>Group Name</b></td><td><b>Group Description</b></td><td><b>Users</b></td></tr>";
   $().SPServices({
  //    webURL: "http://something.com/",
      operation: "GetGroupCollectionFromSite",
      async: false,
      completefunc: function(xData, Status) {
       $(xData.responseXML).find("[nodeName='Group']").each(function() {
         count = count + 1;          
         liHtml += "<tr><td>" + count + 
                  "</td><td>" + $(this).attr("ID") + 
                  "</td><td>" + $(this).attr("Name") + 
                  "</td><td>" + $(this).attr("Description") +
                  "</td><td>" + "<INPUT TYPE=\"BUTTON\" VALUE=\"Get Users\" onclick =\"gtGetUsersCollectionFromGroup('"+ $(this).attr('Name')+"')\">" + "</td></tr>";
    });
          $('#jQueryDiv').append(liHtml + "</table>"); 
      }
    });
 }
 function gtGetUsersCollectionFromGroup(grpName){
   var count = 0;
   alert(grpName);
   $('#loadGrp').html("");
   var liHtml1 = "<table border=0 cellpadding=1 cellspacing=1><tr><td><b>#</b></td><td><b>UserID</b></td><td><b>User Name</b></td><td><b>User Email</b></td></tr>";
   $().SPServices({
  //    webURL: "http://something.com/",
      operation: "GetUserCollectionFromGroup",
      groupName: grpName,
      async: false,
      completefunc: function(xData, Status) {
       alert(xData.responseText);
    $(xData.responseXML).find("[nodeName='User']").each(function() {
         count = count + 1;
         liHtml1 += "<tr><td>" + count + 
                  "</td><td>" + $(this).attr("ID") + 
                  "</td><td>" + $(this).attr("Name") + 
                  "</td><td>" +  $(this).attr("Email") +
                  "</td></tr>";
               });       
       }
   });
      liHtml1 = liHtml1 + "</table>";
   $('#loadGrp').append(liHtml1);
 }
</script>
<b>Get All Groups under this site collection</b><br>
<div id="jQueryDiv"><IMG ID="waitSymbol" src="/_layouts/images/GEARS_AN.GIF" />
</div>
<div id="loadGrp"></div>

I think you may have been making things more complicated with the extra function calls. Since you're calling each function only once, there's not really any benefit in breaking things apart the way you were.

M.

Apr 26, 2011 at 10:03 PM

Admin,

ok..I just figured out I missed to scrub the data - i.e webURL used in calling the $().SPServices functions. Kindly, please remove off or scrub it as I don't want it to be public .

Please remove or all such instances replace with dummy url.

Thank you so much,

Shashank

Coordinator
Apr 26, 2011 at 10:36 PM

Shashank:

I'll edit out the URLs in my post above, but you'll need to edit your own, as I cannot access them.

M.

Apr 27, 2011 at 1:23 AM

Marc,

Thank you so much for prompt reponse in correcting the url. Also, thanks for clarifying above code, it works now :-)