Dynamic Footer using "GetListItems"

Apr 25, 2011 at 7:54 PM

I just stumbled across SPServices last week, and I'm trying to implement it into a solution I'm working through.  I'm attempting to create a dynamic footer that pulls it's content from a list item in a list called "Footer".  I created a custom column called "Footer-Content" that is a rich text field (to allow for formatting, links, etc.).  The basic set-up is one masterpage for multiple subsites, where each subsite will have different footer content.  The managers of each subsite need to be able to change the footer content but don't have access to SPDesigner, which is why I'm attempting to do this with a list.

Initially looking through the SPServices API, it looks like GetListItems is my best option (I could be wrong, this is my first time using it).  My trouble is that I don't know how to reference a custom column in a specific list using the API.  I'm hoping someone can point me in the right direction.

I can give more info in need be, I'm just looking for some inital thoughts and a little bit of direction.  

Thanks,

~Drew

Coordinator
Apr 25, 2011 at 8:43 PM
Edited Apr 25, 2011 at 8:44 PM

I've done this type of implementation a couple times; fat footer built by SharePoint list(s).

If you're pulling from multiple lists on multiple sites you'll need an SPServices GetListItems call to each list. The web services are permissions trimmed so you'll need to make sure that all users have read access to the list(s) containing the footer content.

As far as referencing the custom column, you'll reference by the static name for the column. There's a couple quick ways to get that, such as going to list settings and editing the column, then look to the URL and you'll see the static name for the column. The "-" in your column name is going to get escaped, so it won't quite match what you name your column. I can't remember the code for a hyphen off the top of my head, but if it was a space (ie: "Footer Content") the static name would be "Footer_x0020_Content".

SPServices will fire off a GetListItems operation to the list you specify (note that if you're doing this across multiple webs it's a better practice to use the list GUID vs the list name to avoid potential conflict). Once the web service results come back, you'll just use jQuery to parse through and access the data, using something like $(this).attr("Footer_x0020_Content");

There lot's of examples in the documentation for handling those requests. Have a look around and if you have any more questions come back and post a follow up.

Apr 26, 2011 at 7:43 PM

Awesome, thanks!  I'll see if I can dig through this and come up with something.

If I have any difficulties I'll post something back. 

Apr 26, 2011 at 8:08 PM
Edited Apr 26, 2011 at 8:09 PM

I think there is the possiblity I'm approaching this wrong, or maybe making the wrong calls.  but I did as you suggested and used $(this).attr("Footer_x002d_Content"); but it returns as an undefined value.  So just to double check things over I added another column called "testColumn" (single line text ) to see if I could get it to return any results, and I had the same thing happen.

This is the code I have, as I said I have no idea if I'm working with it right.

 

$( document ).ready( function() {
  $().SPServices( {
    operation: "GetListItems",
    async: false,
    listName: "Footer",
    CAMLViewFields: "<ViewFields><FieldRef Name='Footer-Content' /></ViewFields>",
    completefunc: function (xData, Status) { 
    	$(xData.responseXML).find( "[nodeName='z:row']" ).each( function() {
	    var attrs = $( this )[ 0 ].attributes;
	    var liHtml = '';
	    for( var i = 0; i < attrs.length; i++ ) {
		liHtml += "<li>" + attrs[i].nodeName + " => " + attrs[i].nodeValue + "</li>";
	    }		
	    liHtml += "<li>" + "Footer_x002d_Content" + " => " + $(this).attr( "Footer_x002d_Content" ) + "</li>";
	    liHtml += "<li>" + "testColumn" + " => " + $(this).attr( "testColumn" ) + "</li>";			
	    $( "#testDiv" ).append( liHtml );
        });      
    }
  });
});

 

I'm having it print out the parsed xml data from the response into a div into a CEWP, and this is what I get:

 

  • ows_Title => Main Footer
  • ows_MetaInfo => 2;#
  • ows__ModerationStatus => 0
  • ows__Level => 1
  • ows_ID => 2
  • ows_UniqueId => 2;#{F9589B0A-2A6E-4649-AD18-9AD9AE754AF4}
  • ows_owshiddenversion => 3
  • ows_FSObjType => 2;#0
  • ows_Created => 2011-04-22 12:12:16
  • ows_PermMask => 0x7fffffffffffffff
  • ows_Modified => 2011-04-26 13:54:09
  • ows_FileRef => 2;#Lists/Footer/2_.000
  • Footer_x002d_Content => undefined
  • testColumn => undefined
  •  

    So I'm not entirely sure how to approach this.  The documentation I read through references the column names in that manner, but I can't tell if I'm referencing the list differently.  It's obviously calling to the list correctly since I get the proper title, id, and so forth.

    Suggestions?

    Coordinator
    Apr 26, 2011 at 8:20 PM
    Edited Apr 26, 2011 at 8:22 PM

    Assuming "Footer_x002d_Content" is the correct identifier, you'd need to access it using "ows_Footer_x002d_Content".

    My advice would be to output everything using $().SPServices.SPDebugSMLHttpResult for debug purposes, so you can see exactly what's coming back from the web service call.

    Also, CAMLViewFields is an optional property; for this particular scenario where you're only dealing with a couple columns anyway I'd probably drop that all together. You're just introducing another area where you might potentially screw things up as you're learning (not you specifically, but people in general). Start with it as basic as possible and build on it (so you know what does/doesn't break it).

    You're definitely on the right track.

    Apr 26, 2011 at 9:01 PM

    Just a quick observation in that I thought that the space was escaped with x0020. Example ows_Footer_x0020_Content

    Just asking to be sure I have it straight!

    Apr 26, 2011 at 9:03 PM

    Yep! That was it. My problem was the reference tag of the column.  I was using Footer_x002d_Content when it should have been ows_Footer_x002d_Content.

    Works like a charm!

    Coordinator
    Apr 26, 2011 at 9:10 PM
    spevilgenius wrote:

    Just a quick observation in that I thought that the space was escaped with x0020. Example ows_Footer_x0020_Content

    Just asking to be sure I have it straight!

    He was using a hyphen, al la Footer-Content, not a space.

    Coordinator
    Apr 26, 2011 at 9:10 PM
    drewfrisk wrote:

    Yep! That was it. My problem was the reference tag of the column.  I was using Footer_x002d_Content when it should have been ows_Footer_x002d_Content.

    Works like a charm!

    Awesome!

    Apr 26, 2011 at 9:28 PM

    Duh! Yes a hyphen vs a space! I see now. Just wanted to be sure! Great content so far!

    Sep 11, 2012 at 6:54 PM
    Edited Sep 11, 2012 at 6:55 PM

    Sorry to bring an old topic, but I would like to know if anyone managed to get it work. I tried to use the code posted by drewfrisk, with the fix but It's not displaying anything not even an error.

    Coordinator
    Sep 11, 2012 at 7:36 PM

    feibien:

    Perhaps it's better to start a new thread with the problem you are having. Is it the GetListItems operation? Unknowln errors? What?

    M.

    Sep 11, 2012 at 10:07 PM
    feibien wrote:

    Sorry to bring an old topic, but I would like to know if anyone managed to get it work. I tried to use the code posted by drewfrisk, with the fix but It's not displaying anything not even an error.

    Nevermind I found a better way to do it based on this code. Thanks a lot