Can't seem to get the GetAlerts webservice to work in a demo...

Aug 21, 2012 at 12:17 PM

Hi Marc,

 I'm loving the library - this is an awesome piece of kit to use in Sharepoint! I do have a question though, about how to get the GetAlerts web service to work in a demo:

<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">
var divId = "#WSOutput";
var out = "";

// main function
$().ready(function () {
var theList =  "";
  $().SPServices({
    operation: "GetAlerts",
    async: false,
    webUrl: "<server url>",    //name removed for post
    completefunc: function (xData, Status) {
       $(divId).append("<b>This is the output from the GetAlerts operation:</b><br>");
        var out = $().SPServices.SPDebugXMLHttpResult({
	  node: xData.responseXML,
	  outputId: divId
        });      
      $(divId).append(out);  

	$(xData.responseXML).find("Alerts > Alert").each(function() {
	  var exist = $(this).attr("Id");
	  if (exist == "" ){
	    alert("No alerts for this site");
	  } else {
            theList = $(this).attr("Title");
	    var theURL = $(this).attr("AlertForUrl");
            var theGUID = $(this).attr("Id");
	    var Row = "Title: " + theList +" - " + theURL + " - " + theGUID;
	    $(divId).append(Row);
	  } 
	}); //each           
      $(divId).append(out);
    }
  });

});

</script>

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

 I'm struggling to see what I'm doing wrong on this - every time I use the above demo, I get back the SOAP response to indicate there is an alert set on the site (which is the case), but when trying to get back inidividual responses, all I get back is "undefined". The above code is a variation from http://spservices.codeplex.com/discussions/287523, allowing for any comments made after the original post.

I've tried changing the location of the variables that are defined, to ensure this is not simply down to not declaring them at the right time; it hasn't made any difference. Could some please point me in the right direction?

Cheers,

Alex.

Coordinator
Sep 11, 2012 at 12:50 PM

Alex:

It may be that you are using "Id" rather than "ID". Capitalization matters!

Sorry for the delayed response, but I was on holiday.

M.

Sep 12, 2012 at 7:41 AM

Hi Marc,

No problem - we all have to have a break sometimes... ;-)

Thanks for this - I managed to trace where I went wrong; I wasn't using the right methods to filter out the right nodes in the XML being returned. Here's a sample for anyone who needs it:

        $(xData.responseXML).SPFilterNode("Alerts").each(function() {
	  $(this).find("Alert").each(function() {
            var alertID = $(this).find("Id").text();
            var alertTitle = $(this).find("Title").text();
            var alertActive = $(this).find("Active").text();
            var alertEventType = $(this).find("EventType").text();
            var alertForUrl = $(this).find("AlertForUrl").text();
            var alertEditUrl = $(this).find("EditAlertUrl").text();

            out += "Alert ID: " + alertID + "<br>" +
                   "Alert Title: " + alertTitle + "<br>" +
                   "Active: " + alertActive + "<br>" + 
                   "Event Type: " + alertEventType + "<br>" +
                   "Root Url: " + alertForUrl + "<br>" +
                   "Alert Edit Url: " + alertEditUrl + "<br>" +
                   "<hr>";
          });
        }); 

It's working beautifully now - I guess it's all part of the learning curve, as I am still somewhat new to traversing XML using jQuery! I am really loving the library though - it's an awesome bit of kit!

Cheers,

Alex.