0

I'm using SharePoint 2010, SPServices and the GetListItems method to retrieve items from a list. The list has 31 categories such as A2, A3, A4... until A32. There are also sub categories such as, A2.1, A2.2, A2.3, etc.

The list data outputs into 31 boxes coded in HTML and JavaScript assigns a color to each box according to the highest severity level (high risk=red, moderate risk=orange...)

There will always be 31 colored boxes (A2 thru A32) and I don't need to add new boxes. The JavaScript works where it cycles through all subcategories and correctly outputs the color to the parent. If I EDIT a list item the color changes correctly and if I DELETE a list item, the page updates.

The problem is when I ADD a new list item (A2.1.1) SharePoint isn't seeing the latest list item that was just added until I add a another list item then it will update the previous HTML box color. My question is if the SPServices GetListItem method doesn't actually retrieve the current data set. I've inserted a CAMLRowLimit: 0, and that didn't work.

Has anyone heard of GetListItems not always returning the current data set? When I refresh the browser for a split second all of the boxes change to the previous browser version history and then the page loads.

<script type="text/javascript">

$( document ).ready(function() {

alert( "ready!" );

});

var method = "GetListItems";               
var webURL =  $().SPServices.SPGetCurrentSite() ;                      
var list = "Dashboard";                       
var fieldsToRead = "<ViewFields>"+"<FieldRef Name='Name' />" +"
</ViewFields>"; 
var query = "<Query><OrderBy><FieldRef Name='ID'  Ascending='True' /></OrderBy></Query>";


$().SPServices

        ({

                    operation: method,

                    async: false,  

                    webURL: webURL,

                    listName: list,

                    CAMLViewFields: "<ViewFields Properties='True' />",

                    CAMLQuery: query, 

                    // Override the default view rowlimit and get all appropriate rows
                CAMLRowLimit: 0,

                    completefunc: function (xData, Status)
       {

 var itemCount = $(xData.responseXML).SPFilterNode("rs:data").attr("ItemCount");
  alert(itemCount);       

// 1. Get the raw data from the Sharepoint Web Services API.

var subtasks = $(xData.responseXML).SPFilterNode("z:row"); //This is the list of all subtasks

// 2. Need to break it down into arrays -- one per category
// and 3. Loop thru each subtask to find the most high-severity status per category.

var categories = [];
var highest_severity = 0;

// if (typeof(last_category) !== 'undefined') 
var last_category = 'A2';
for(x=0; x<subtasks.length; x++)

{

var category = subtasks[x].getAttribute('ows_LinkTitle').split(".", 1)[0]; 
//e.g. A3

if(category != last_category){
    categories[last_category.replace('A', '')] = highest_severity;
    last_category = category; // Set up for the new category.
    highest_severity = 0;
}

var subtask_status = subtasks[x].getAttribute('ows_Status'); //e.g. "High Risk"
switch(subtask_status){

        case "High Risk": current_row_severity = 5;
            break;

        case "Moderate Risk": current_row_severity = 4;
            break;

    case "Accepted Risk": current_row_severity = 3;
            break;

        case "On Track": current_row_severity = 2;
            break;

        case "Complete": current_row_severity = 1;
            break;

        default: current_row_severity = 0;
            break;

    };

if(current_row_severity > highest_severity)

    highest_severity = current_row_severity;
} 


    // 4. Color the status box according to the most high-severity status for each.
    var colorArray = ['#EFEFEF', '#E6F2FF', '#C1E1A6', '#FFFFCC', '#FFD9B3', '#EBCFC4'];
for(x=2; x<34; x++) // looping through each of the status boxes, one at a time
{
    var status_box_element = document.getElementById('A' + x);
    var newColor = colorArray[categories[x]];
    status_box_element.style.backgroundColor = newColor;

}   
                       }

       });
</script> 
4
  • I’ve never had that happen. Are you running the above code in $(Document).ready()? What happens if you put an alert() before the SPServices() call and wait several seconds after you save the new item before clicking OK?
    – mannaggia
    Commented Nov 20, 2017 at 23:47
  • I included an itemCount alert that shows the call is getting the correct number of items in the list. I also added your suggestion and the alert states that it is ready. The itemCount alert appears prior to the “ready” alert. If it recognizes items that have been edited and also deleted and also is receiving all item in the list, then I’m really confused why it doesn’t see the very latest newly added list item. I could post the JavaScript if that would help? Commented Nov 21, 2017 at 18:31
  • Sure. I was just thinking that it might be a timing issue where the item had not been updated after submit by the time you tried to read it back.
    – mannaggia
    Commented Nov 21, 2017 at 19:17
  • Thank you for your suggestions. Yes, I was wondering if it relates to timing. I've waited for several hours and also a couple of days to see if it eventually updates the color on the newly added item. It does not so I'm thinking it isn't a timing issue? It will only update the newly added item's color if I add another item to a different subcategory. It's like it is one previous step/version behind. Commented Nov 21, 2017 at 19:26

1 Answer 1

0

According to your code, the problem might be the code below:

if(category != last_category){
  categories[last_category.replace('A', '')] = highest_severity;
  last_category = category; // Set up for the new category.
  highest_severity = 0;
}

I think it should be like below:

if(category != last_category){
  categories[last_category.replace('A', '')] = highest_severity;
  last_category = category; // Set up for the new category.
  highest_severity = 0;
  categories[last_category.replace('A', '')] = highest_severity;
}

Please try and show us the result.

1
  • Thanks for your suggestion. The good news is that it now recognizes the newly added item and does change the color. The bad news is that it is changing to the default severity level of 0 or "N/A" which is #EFEFEF. If I add another item to the same subcategory (A2.1, A2.2) there is no color change; however, if I add another item to a different subcategory (A2.1, A3.1) , the PREVIOUS item that was just ADDED changes to the correct color. EDIT and DELETE are still working correctly. I welcome any other suggestions that you may have. Commented Nov 22, 2017 at 16:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.