Accessing ListItems of subsites

Jun 21, 2012 at 11:02 AM
Edited Jun 21, 2012 at 1:56 PM

Hi SPServices community!

I'm actually working on a webpart that has to get every event (page with an event CT that i created) and display them on a calendar (FullCalendar) in this same webpart.

The complications come when i have to get events from subsites. I'm new at SharePoint stuff and it is maybe really easy to do what i want to do but... I have to start somewhere! I've read tons of blogs from which i took code samples (thanks for that btw!) and modified them to work as i want them to.

I managed to get and display events on the site but not on the subsites. After some tests, it seems that my code allows me to find the subsites. It also finds the list that i want ("Pages") but i'm not sure if it's the one of the main site or the one of the subsite. I explain why: when it comes to get the events of subsites, the results that I get are the ones in the main site...

I think that the problem comes from the webURL option. As shown in the code below, I use $(this).attr("WebFullUrl") but when I test the value, it returns : "undefined". Is this normal? I'm going crazy looking for an answer on the web!

I used code sample from BenTedder and JoshMcCarty. Thanks for doing blogs!

Here is my code, I made it very easy to beggin with something that works and then implement the rest of ViewFields, etc. 

I put it inside the events option of FullCalendar.

 

function (start, end, callback) {
            	
     var events = [];
            	
     var startDate = $.fullCalendar.formatDate($('#calendar').fullCalendar('getView').start, "u").split("T")[0];
            	
     var calView = $('#calendar').fullCalendar('getView').title;
     var camlView = "";
            	
     switch (calView) {
	case "agendaWeek":
	  camlView = "<Week />";
	break;
	case "agendaDay":
	  camlView = "<Week />";
	break;
	default: // Default to month view
	  camlView = "<Month />";
     }
            	
     var camlFields = "<ViewFields>" +
	                "<FieldRef Name=\"Title\" />" +
	                "<FieldRef Name=\"StartDate\" />" +
	                "<FieldRef Name=\"_EndDate\" />" +
                      "</ViewFields>";
     var camlQuery = "<Query>" +
				         
			"<Where>" +

		            "<Eq><FieldRef Name='ContentType' /><Value Type='Text'>Evenement</Value></Eq>" +
		
		
		         "</Where>" +
		         "<OrderBy>" +
		             "<FieldRef Name='Title' />" +
		         "</OrderBy>" +

		     "</Query>";
     var camlOptions = "<QueryOptions><RecurrencePatternXMLVersion>v3</RecurrencePatternXMLVersion><ExpandRecurrence>TRUE</ExpandRecurrence><DateInUtc>TRUE</DateInUtc></QueryOptions>";
				            	
            	$().SPServices({
		    operation: "GetAllSubWebCollection",
		    webURL: "/",//$().SPServices.SPGetCurrentSite(),
		    async: false,
		    completefunc: function (xData, Status) {
			$(xData.responseXML).find("Web").each(function () {
			var site = $(this).attr("Title");
				        	
			$().SPServices({
			    operation: "GetListCollection",
			    webURL: $(this).attr("WebFullUrl"),
			    async: false,
			    completefunc: function (xData, Status) {
			    $(xData.responseXML).find("List").each(function () {
				//function to get all items inside every list called "Pages"
				                        
				if ($(this).attr("Title") == "Pages") {
				//alert($(this).attr("Title") + " " + site);
				   $().SPServices({
				       operation: "GetListItems",
				       webURL: $(this).attr("WebFullUrl"),
				       async: false,
				       listName: $(this).attr("Title"),
				       CAMLViewFields: camlFields,
				       CAMLQuery: camlQuery,
				       CAMLQueryOptions: camlOptions,
				       completefunc: function (xData, Status) {
				       $(xData.responseXML).SPFilterNode("z:row").each(function () {
					   var siteZ = $(this).attr("WebFullUrl");
					   alert(siteZ); //this is where i get "undefined"
					   var thisStart;
								
					   var thisTitle = $(this).attr('ows_Title');
														var thisStart = $(this).attr("ows_StartDate").toString().replace(/T/g, " ").replace(/Z/, "");
								
					   events.push({
							title: thisTitle,
							start: thisStart,
						      });
				        });
				        callback(events);
			        }
		             });
                           }
                       });
                   }
               });
           });
       }
    });

}

Coordinator
Jun 22, 2012 at 12:54 PM

GetAllSubWebCollection returns very little about the Webs: you get two fields, one is called Title and the other is called Url. So in your call to GetListCollection, it should be:

webURL: $(this).attr("Url"),

It's best to build things up in pieces, using alerts or the debugger in the browser to check each step as you nest things.

M.

Jun 25, 2012 at 1:00 PM

Ok thank you! I tried it and the site is not undefined anymore. It returns the subsites that I want! I still didn't manage to make my web part work but that will help me a lot!