JQuery Sharepoint List Issue with If Function

Posted on 2012-09-11
Last Modified: 2012-09-11

I have a java script which I am using to try and return the number of values in a sharepoint list that meet the criteria of being created in Period 2.

The script also does a count of the total number of entries in the list.

Currently, the script returns a result only if the following section is commented out. I've debugged the script and can see that there is a problem arround the datavalues that it is populated with. Screenshot attached.

//count number of lines in period
//      if ( periodValue === $node.attr("EM_REPORTING_PERIOD") ) {
//	numOpenedInPeriod++;}

Open in new window

Can anybody tell me what is wrong with the script and why it doesn't return a defined value for the sharepoint column to compare against?

I added  "alert($node.attr("EM_REPORTING_PERIOD"));" to see what was in the attribute & it just reports undefined.


Full Code:

<script type="text/javascript" src="http://mynationwideteam/sites/fin_comms_tech_serv/Javascript/jquery-1.8.1.min.js"></script>
<script type="text/javascript" src="http://mynationwideteam/sites/fin_comms_tech_serv/Javascript/jquery.SPServices-0.7.1a.min.js"></script>
<script language="javascript" type="text/javascript">

$(document).ready(function() {
   //define local variables

   var numOfLines = 0, numOpenedInPeriod = 0 ;
   var periodValue = 2; // this will be set by drop down on going

    operation: "GetListItems",
    async: false,
    listName: "EM Exception Log",
    CAMLViewFields: "<ViewFields><FieldRef Name='Title' /></ViewFields>",
CAMLQuery: "<Query><Where><Neq><FieldRef Name='ID' /><Value Type='Counter'>0</Value></Neq></Where></Query>",

    completefunc: function (xData, Status) {
      $(xData.responseXML).SPFilterNode("z:row").each(function() {
        var $node = $(this);

//count number of lines in period
     if ( periodValue === $node.attr("EM_REPORTING_PERIOD") ) {	numOpenedInPeriod++;}





var output = "<table><tr><td><h3>Total Number of Lines: " + numOfLines + "</h3></td></tr>" + "<tr><td><h3>Total Open In Period 2: " + numOpenedInPeriod + "</h3></td></tr></table>";

//Append output to the DOM.
$("#results").append( output );

<div id='results'></div>

Open in new window

Question by:bedsingar
    LVL 12

    Expert Comment

    you could try logging to console $node. Perhaps it's not what you think it is.


    If you view this in firebug, you'll see all the info on the object and should be able to work out if you have the correct element.

    Author Comment


    Firebug is a Firefox addin isn't it? - I'm unfortunately restricted to using Internet explorer and can't install addins due to the admin policy.

    Can I do this in IE Debug also? (As per the screenshot)

    LVL 19

    Accepted Solution

    Well, if $node.attr("EM_REPORTING_PERIOD") returns undefined is because that element isn't on your page.

    It could be two things:
    1) $node is not defined, maybe because the node you are trying to load and reference on your javascript is not on the page.

    2) There is no EM_REPORTING_PERIOD attribute on the element referenced by $node.

    You can check on your alert which of two options is just doing alert($node); if the element is on the page and is well referenced you should receive something like "[object Object]", if you receive here undefined then check what's happening with this node and why it's not being correctly loaded.

    If the node is correctly loaded you should check on the page why it doesn't has the attribute you are asking for "EM_REPORTING_PERIOD" maybe there is a spelling error when it's being created on the page code, or maybe it's not being loaded...
    LVL 12

    Expert Comment

    Unfortunately the Dev Tools in IE are somewhat lacking. In firebug if you console.log an object you're able to click the item in the log and view all it's details. In IE when you do the same you literally just get a line in the log saying:

    LOG: [object Object]

    It will at least tell you if you've got an object in the variable but you aren't getting much more info than that.

    Author Closing Comment

    Found it finally ... $node.attr("ows_EM_REPORTING_PERIOD")

    I missed off the ows_ .... In the mean time I'd completely re-written the code to take another approach.

    Thanks for all your contributions.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Suggested Solutions

    The vision: A MegaMenu for a SharePoint portal home page The mission: Make it easy to maintain. Allow rich content and sub headers as well as standard links. Factor in frequent changes without involving developers or a lengthy Dev/Test/Prod rel…
    Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
    Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
    In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now