Help with GetListItems

Sep 30, 2011 at 1:20 PM
Edited Sep 30, 2011 at 1:22 PM

Hi there - am new to Sharepoint and new to SPServices.  I have some basic (from a few years past) knowledge about jQuery.  Am using a few of the basic SPServics features with great success.  Thank you.

I'm currently playing with the GetListItems function in the hopes of returning both the Name and URL of the most recently created document in a Document Library.  Had a couple questions:

  1. Where can I find a list of all the available variables that I can get from a list item (document library) ?  I tried "title" and "name" but neither works with the documents themselves (returns "undefined" - although it doesn't show the "title" for the folders ... so it is working somewhat).  I also need a URL to the document. 
  2. Where can I find more information about CAML - I need to limit the query to the most recently uploaded document (excluding folders).  Having trouble finding some good learning tools

Thanks again and am looking forward to learning more.

Damon

PS: here is the code I was trying:

<script language="javascript" type="text/javascript">

$(document).ready(function() {
  $().SPServices({
    operation: "GetListItems",
    async: false,
    listName: "Scanned Schedules",
    CAMLViewFields: "<ViewFields><FieldRef Name='Title' /><FieldRef Name='Name' /></ViewFields>",
    completefunc: function (xData, Status) {
      $(xData.responseXML).find("[nodeName='z:row']").each(function() {
        var liHtml = "<li>title: " + $(this).attr("ows_Title") + "<br />name: " +$(this).attr("ows_Name")  + "</li>";
        $("#tasksUL").append(liHtml);
      });
    }
  });
});
</script>
<ul id="tasksUL"/>

 

Coordinator
Oct 1, 2011 at 3:25 AM

Damon:

The code you are showing looks like it ought to work, assuming that your options are correct.

The best next step is probably to add an alert at the top of the completefunc to see if you are in fact getting valid results or an error.

M.

Oct 3, 2011 at 2:55 PM

Hi - so, I made some more progress, however, I am still not getting the document's "name" (it comes back as "undefined").  

Is maybe a "name" referenced in a different way for a document library than for a list?  

Here is how I am getting the link to the most recently created document ... just need to get the name.  Thanks

 

<script language="javascript" type="text/javascript">

$(document).ready(function() {
  $().SPServices({
    operation: "GetListItems",
    async: false,
    listName: "Scanned Schedules",
    CAMLViewFields: "<ViewFields><FieldRef Name='EncodedAbsUrl' /></ViewFields>",
    CAMLRowLimit: 1,
    CAMLQuery: "<Query><Where><Eq><FieldRef Name='ContentType' /><Value Type='Text'>Document</Value></Eq></Where><OrderBy><FieldRef Name='Created' Ascending='False' /></OrderBy></Query>",
    completefunc: function (xData, Status) {
      $(xData.responseXML).find("[nodeName='z:row']").each(function() {
        var liHtml = "<li>url: " +$(this).attr("ows_EncodedAbsUrl")  + "</li>";
        $("#tasksUL").append(liHtml);
      });
    }
  });
});
</script>
<ul id="tasksUL"/>

Oct 4, 2011 at 5:07 AM

try changing to:

$(xData.responseXML).find("z\\:row").each(function(){

 

Coordinator
Oct 4, 2011 at 3:21 PM
Edited Oct 4, 2011 at 3:21 PM

Actually, the notation

.find("[nodeName='z:row']")

is more reliable with disparate browsers. See my blog post on this.

Try adding an alert like I suggested right at the top of the completefunc:

alert(xData.responseText)

This will show you what has been returned in the XML so that you can decide with column you need. (Comment out the CAMLViewFields until you're sure what you want.)

M.

Oct 4, 2011 at 3:34 PM

Thanks for the xData.responseText hint - that's what I needed.  The document name is in: ows_LinkFileNameNoMenu ... Got it all working.  Thanks!

Coordinator
Oct 4, 2011 at 3:37 PM

Sorry my original suggestion was too vague. I think I sent it from my iPhone on an airplane that was taxiing.

Glad you got it working!

M.

Oct 4, 2011 at 4:47 PM

 

Actually, the notation

.find("[nodeName='z:row']")

is more reliable with disparate browsers

 

That may be true but at my current client (IE7) the      .find("[nodeName='z:row']") doesn't work and the    .find("z\\:row") does. Any idea why?

 

Oct 4, 2011 at 6:21 PM

Most of my SPServices work currently is for IE7 & 8.  We are using .find("[nodeName='z:row']") everywhere in our code base.  What version of jQuery/SPServices are you using @ChuckTPS?

Cheers,

Matt

Coordinator
Oct 14, 2011 at 1:53 AM

Chuck:

Dod you get this straightened out?

M.

Nov 18, 2011 at 2:36 AM
I have the same problem...I am using Safari 5, Chrome 14, IE9, and all have the same results.

$().SPServices({
            operation: "GetListItems",
            async: false,
            listName: "Announcements",
            webURL: "https://xx.xx.xx",
            completefunc: function (xData, Status) {
                if (Status == "success") {
                    var htmlList = "";
                    $(xData.responseXML).find("[nodeName='z:row']").each(function () {
                        htmlList = "<li><a href=\"#\"><h2>" + $(this).attr("ows_LinkTitle") + "</h2><p>" + $(this).attr("ows_Created") + "</p></a></li>";
                        $("#ulAnnouncements").append(htmlList);
                    });
                    $("#ulAnnouncements").listview("refresh");
                }
                else {
                    alert("Error!");
                }
            }
        });
Nov 18, 2011 at 2:39 AM

I tried

alert(xData.responseText)

and I think I got the column names correctly.

I am using the following libraby version:

jquery-1.7.js
jquery.mobile-1.0rc2.js
jquery.SPServices-0.6.2.js

Coordinator
Nov 18, 2011 at 2:56 AM
jQuery 1.7 breaks the [nodeName='z:row'] syntax. Check my blog for an alternative approach: http://Sympmarc.com. Alternatively, use jQuery 1.6.x.

M.
Nov 18, 2011 at 3:24 AM

Great, it works!

So i got the alpha, jquery.SPServices-0.6.3.ALPHA10.js

and used

.find("z\\:row, row")
Oct 22, 2013 at 9:59 AM
Edited Oct 22, 2013 at 11:53 AM
Will the SP Services method work for anonymous users?
Actually in need to read the list items using SP Services.
Is it possible for anonymous users to read list items using anonymous access?
Coordinator
Oct 22, 2013 at 1:40 PM
Yes.

M.
Oct 23, 2013 at 7:58 AM
This is my code

function getAnnounmousCount() {
    alert('anonymous');
    var i = 0;
    $().SPServices({
        operation: "GetListItems",
        async: false,
        listName: "Positions",
        CAMLViewFields: "<ViewFields><FieldRef Name='Title'/><FieldRef Name='ID'/><FieldRef Name='Position_x0020_Location'/><FieldRef Name='Opening_x0020_Date' IncludeTime='false'/><FieldRef Name='Short_x0020_Description'/></ViewFields>",
        CAMLQuery: "<Query><OrderBy><FieldRef Name='Title' Ascending='False' /></OrderBy></Query>",
        SOAPHeader: false,
        completefunc: function (xData, Status) {              
            if (Status == "success") {
                var htmlList = "";
                $(xData.responseXML).find("[nodeName='z:row']").each(function () {
                    alert('1');
                    htmlList = "<li><a href=\"#\"><h2>" + $(this).attr("ows_Title") + "</h2></a></li>"; $("#Positions").append(htmlList);
                });
                $("#ulPositions").listview("refresh");
                i++;
            }
            else {
                alert("Error!");
            }
        }

    });
    var count = document.getElementById("hms_notification_id04");
    count.innerHTML = i;
}
I am trying to get the count from positions list through anonymous access.But Iam unable to get it.
Please tell me what I am doing wrong here
Coordinator
Oct 23, 2013 at 2:52 PM
What happens when your code runs?

Lacking that info, here are a few observations:
  • The .find("[nodeName='z:row']" syntax will not work in jQuery 1.7+, so that may be your problem.
  • There is no parameter called SOAPHeader
M.
Oct 24, 2013 at 5:36 AM
Actually I need to display the count available which I am reading from custom list using anonymous user. I am using jquery 1.6 and spservices 0.6.2 version.
I am not able to get the xData.responseXML value .
.
Coordinator
Oct 24, 2013 at 1:35 PM
I'd suggest upgrading both jQuery and SPServices to more recent versions. I don't see anything obviously wrong with your code, so you'll need to do some debugging. Take a look at the Net traffic in Firebug or similar.

M.
Oct 24, 2013 at 4:34 PM
Hello Everyone,

I'm Really New to this jQuery SharePoint world. So my apologies for asking a simple question.

I have a Content Editor Webpart that contains some jquery that displays all Subsite in a collection with the last modified date.
My users requirements is that they want to see all of the document libraries listed instead of the sub sites.
Below it the code that I'm i have that displays the sub sites. Any help would be much much appreciated.
var sites;

ExecuteOrDelayUntilScriptLoaded(loadSites, 'SP.js');

function loadSites()
{
    var context = new SP.ClientContext.get_current();
    if(context != undefined && context != null) {
        var web = context.get_web();       
 this.sites = web.getSubwebsForCurrentUser(null);
        context.load(this.sites);
        context.executeQueryAsync(
            Function.createDelegate(this, this.onSuccess),
            Function.createDelegate(this, this.onFailed));
    }
}

function onSuccess()
{
    var items = this.sites.getEnumerator();
    var menuNavContent = '';

    menuNavContent += '<h1>First LI</h1>';
    menuNavContent += '<ul class="apps">';

    while(items.moveNext())
    {
        var item = items.get_current();
        var itemUrl = item.get_serverRelativeUrl();
        var title = item.get_title();

        menuNavContent += '<li>';
        menuNavContent += '<a href="' + itemUrl + '">' + title + '</a>';
        menuNavContent += '</li>';
    }
 
    menuNavContent += '</ul>';
    $('#menuNav').html(menuNavContent);
}
 
function onFailed(sender, args)
{
    $('#menuNav').html(args.get_message());
}

</script>
Coordinator
Oct 24, 2013 at 8:26 PM
Your code is using the Client Side Object Model (CSOM), not SPServices, so I'd suggest you head over to the MSDN forums.

M.