Odd issue with running SPServices code

Aug 6, 2012 at 2:34 PM

Hi,

I'm running the code below in a CEWP, on a Sharepoint site:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"></script>  
 
<script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/jquery.SPServices/0.7.1a/jquery.SPServices-0.7.1a.min.js"></script>  

<script type="text/javascript">
$().ready(function () {
var siteTitle = "";
var siteGroups = "";
var siteOwners = "";
var sitecount = "";
var groupName = "";


  $().SPServices({
    operation: "GetAllSubWebCollection",
    completefunc: function (xData, Status) {
      $("#WSOutput").append("<b>This is the output from the GetAllSubWebCollection operation:</b><br>");
      $(xData.responseXML).find("Web").each(function() {

	siteTitle = $(this).attr("Title");
	siteURL = $(this).attr("Url");
        siteGroups = getGroups(siteURL);
        siteOwners = getGroupOwners(groupName);

   	$("#WSOutput").append("Site Title: " + siteTitle + 
                              "<br>URL: <a href='" + siteURL + "'>" + siteURL + "</a>" +
                              "<br>" + siteGroups +
                              "<p>" + siteOwners +
                              "<p> Count of groups: " + sitecount +
                              "<hr/>");
      });
    }
  });

  function getGroups(siteURL2) {
    var groupdata = "";

    $().SPServices({
      operation: "GetGroupCollectionFromWeb",
      webURL: siteURL2,
      async: false,
      completefunc: function (xData, Status) {

        var count = 1;

        $(xData.responseXML).find("Group").each(function() {
          groupdata = groupdata + "Group Name " + count + " : " + $(this).attr("Name") + "<br>";

          count++;

          var matched = $(this).attr("Name").match("Owner");
          if (matched == "Owner") {
            groupName = $(this).attr("Name");
          }  
        });
        sitecount = count - 1;
        count = 1;
      }
    });  
  return groupdata;
  }

  // Get Display name based on user ID
  function GetDisplayName(userNo) {
  var userName = "";
  var substr = "";
  $().SPServices({ 
    operation: "GetUserInfo", 
    async: false, 
    userLoginName: userNo, 
    completefunc: function (xData, Status) { 
        $(xData.responseXML).find("User").each(function() { 
            userName = $(this).attr("Name"); 
            substr = userName.split(',')
            userName = substr[1] + " " + substr[0];
        }); 
      } 
    }); 
    return userName;
  }

  function getGroupOwners(groupName) {
    var ownerdata = "";

    // Gets users within Owners group
    $().SPServices({
    operation: "GetUserCollectionFromGroup",
    groupName: groupName,
    async: false,
    completefunc: function (xData, Status) {
      $(xData.responseXML).find("User").each(function () {
        var displayName = GetDisplayName($(this).attr("LoginName"));
        ownerdata = ownerdata + "User ID: " + $(this).attr("LoginName") + " - Name: " + displayName + "<br>";
      });
    }
   });
   return ownerdata;
  }
});

</script>

<div id="WSOutput"></div>

As site owner and Sharepoint master admin, I can see the results of the code perfectly - yet 2 of my colleagues, who are also master site admins (and of which one of them is owner on at least 2 subsites), can't see any results at all? All they see is this line: <b>This is the output from the GetAllSubWebCollection operation:</b>.

Any ideas anyone?

Coordinator
Aug 6, 2012 at 6:01 PM

It's probably permissions. Check with Fiddler or Firebug to see what you're getting back in the Net traffic.

M.

Aug 8, 2012 at 7:24 AM

Hi Marc,

Apologies for the delay in responding - I was tied up all day yesterday.

I've tried on 2 different PCs - 1 with IE6 and one with IE8 (we're an IE only company!); still no joy yet.

Both show exactly the same static text only, as mentioned above. Neither show any of the results from the SPServices code. I've checked in IE Developer Toolbar on the IE8 PC - he can access both the library and version of JQuery absolutely fine - I can see calls to both of the CDN links being used, and the return time is about 70-80ms, which I don't think is unreasonable?

I know there is a momentary pause before any results show - I would expect this, and don't believe it is enough for either PC to cause an issue. Both users from these two PCs have Full Control at site level throughout all of the sites being tested - this shouldn't present an issue? (It's a dev environment, so not too fussed about level of permissions). I am wondering though if there is a browser setting I've changed, which is affecting how the code is being displayed for me, in comparison to everyone else? Would this make any difference?

Cheers,

Alex.

Aug 8, 2012 at 8:46 AM
Edited Aug 8, 2012 at 9:10 AM

An update - I've run a direct copy of my code in a different web app, used by a colleague; it works absolutely fine. This would suggest an issue outside of my code - not sure where though...

EDIT: Spoke too soon - it works fine for me, but still shows the same issue for a colleague in IE8, on the alternative web app. Seems the code doesn't like working at root level of a web application for non master admin people?

Aug 8, 2012 at 6:08 PM

If you haven't looked at the raw return on the first call, I would do that on the suspect machines/accounts. You mentioned all they see is 'This is the output from the GetAllSubWebCollection operation:' which indicates the completefunc is launching. If that's all they get, it may help to see if the web service call is returning anything. you can do this by....

$().ready(function () {
  $().SPServices({
      operation: "GetAllSubWebCollection",
      completefunc: function(xData, Status) {
        $("#MyXMLTextBox").val(xData.responseText);
      }
  });
});

</script>

<textarea rows="20" cols="150" id="MyXMLTextBox"></textarea>

Notice, I'm using a textarea to display the raw data. xData.responseText won't write directly to a div element.  I do this on all of my dev work if there's any concern that I'm not getting any return on my web service call.  Of course, you may have already done this.  if so, I didn't catch that above--carry on  ;-) 

 

 

Aug 9, 2012 at 7:03 AM

Hi gkoliver,

Thanks for this - I will do that on the suspect machines, both at a "page in a subsite", and a "page at root level of web app" - I will let you know what I get back...

Alex.

Aug 9, 2012 at 7:39 AM

Hi gkoliver,

Argh! I put up something similar on a sub page, and on a page at root level - it was piping out the contents being returned, into a DIV.

This confirmed that SPServices was working OK, for the IE8 PC that couldn't see my proper code before...I need to have another look at my original code, and see if I can figure out what is going on...

Alex.

 

Aug 9, 2012 at 8:13 AM

Hi gkoliver,

I added your code in on the default.aspx page, at root level of my web app - it came back with this at the start:

<HTML dir="ltr">
<HEAD><meta name="GENERATOR" content="Microsoft SharePoint" /><meta name="progid" content="SharePoint.WebPartPage.Document" /><meta HTTP-EQUIV="Content-Type" content="text/html; charset=utf-8" /><meta HTTP-EQUIV="Expires" content="0" /><meta name="ROBOTS" content="NOHTMLINDEX" /><title>
 
 Error: Access Denied

</title><link rel="stylesheet" type="text/css" href="/_layouts/1033/styles/core.css?rev=5msmprmeONfN6lJ3wtbAlA%3D%3D"/>
<script type="text/javascript" language="javascript" src="/_layouts/1033/init.js?rev=SKi7C%2FTrsh1U%2FCnIwkB9Ag%3D%3D"></script>
<script type="text/javascript" language="javascript" src="/_layouts/1033/core.js?rev=mHKsOQ0iU3Q5jdm9OZNDdg%3D%3D"></script>

 <meta name="Robots" content="NOINDEX " />
</HEAD>
<BODY scroll="yes" onload="javascript:if (typeof(_spBodyOnLoadWrapper

This clearly indicates a permissions issue, but I'm not sure where - both users who I tested this on, have access; one via an AD group, and one via a named user account.

Any ideas?

Alex.

Aug 9, 2012 at 11:50 AM

For me, I have always had issues with this call and permissions. This is a site collection call in most cases. A simple test would be to add one of the problem users to the site collection administrators group and see what happens. My guess is it will work fine for them then as I have tested this thoroughly in the past. The next thing that I do, is wrap my loop iterations in a try/catch block. So you "try" to read the item and if it works write the intended mark up and if you "catch" an error you can write out a message with the site and the error so that the code will execute and you can see where any issues are. If I can find an example of where I do this, I will post it!

Aug 9, 2012 at 12:02 PM
Edited Aug 9, 2012 at 12:24 PM

Thanks for this - I've tried adding 2 users in at site collection admin level (which they should have been in any case!), to see if this would make any difference; unfortunately no change.

I will try putting the code into a try / catch block, to see what happens... if you have an example of how you've done it, it would help please!

EDIT: I've put a try...catch block in on the GetAllSubWebCollection block, just after this line:

$(xData.responseXML).find("Web").each(function() {

... to see if it would help catch what is stopping the code from working - unfortunately it doesn't appear to have made any difference, in that it is not showing any error, and is still showing just the static text?

Aug 13, 2012 at 8:16 AM

I don't know what or why, but I stripped back my original code to the core request (which is to get a list of subsites from specific site collection) - the code now seems to work for my colleagues! Bizarre...

It's also uncovered another issue with how I implement security trimming, but I can deal with that separately...

Thanks for your help!

Apr 15, 2014 at 3:42 PM
What do you mean stripped back? I am having the same problem. I can see it just fine but others cannot. I am putting a modded version of your code in my footer to display the siteowner name so I don't get calls constantly. Works for me but others are not seeing the info. Thanks in advance - Mike B
Coordinator
Apr 16, 2014 at 4:39 PM
nasamike:

Sounds like a permission issue. Whatever your call is, if the user can't do the same thing in the UI, they can't via the Web Services.

M.