1
SP.SOD.executeFunc('sp.js', 'SP.ClientContext', sharePointReady);
var listTitle = [];
var listUrl = [];
var listIsActive = [];
var listOrder = [];

function sharePointReady(){
var siteUrl = mysite;




var clientContext = new SP.ClientContext(siteUrl);
var oList = clientContext.get_web().get_lists().getByTitle('Privacy Statements');

var camlQuery = new SP.CamlQuery();
camlQuery.set_viewXml('');
this.collListItem = oList.getItems(camlQuery);

clientContext.load(collListItem);

clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));        
}  

function onQuerySucceeded(sender, args) {

var listItemEnumerator = collListItem.getEnumerator();


while (listItemEnumerator.moveNext()) {
    var oListItem = listItemEnumerator.get_current();
    listTitle.push(oListItem.get_item('Title'));
    listUrl.push(oListItem.get_item('PrivacyStatementUrls').get_url()); 
    listIsActive.push(oListItem.get_item('IsActive'));
    listOrder.push(oListItem.get_item('SortOrder'));

  }
sessionStorage.setItem('Title', JSON.stringify(listTitle));
sessionStorage.setItem('Url', JSON.stringify(listUrl));
sessionStorage.setItem('IsActive', JSON.stringify(listIsActive));
sessionStorage.setItem('Order', JSON.stringify(listOrder));
var retrievedTitle = sessionStorage.getItem("Title");
var arTitle = JSON.parse(retrievedTitle);
var retrievedUrl = sessionStorage.getItem("Url");
var arUrl = JSON.parse(retrievedUrl);
var retrievedIsActive = sessionStorage.getItem("IsActive");
var arIsAct = JSON.parse(retrievedIsActive);
var retrievedOrd = sessionStorage.getItem("Order");
var arOrd = JSON.parse(retrievedOrd);

var test = ''
for(x=0; x<arTitle.length+1;x++)
{

    for(i=0; i<arTitle.length+1;i++)
    {

        if(arIsAct[i] == true){
            if(arOrd[i] == x){
                test +='<a href="'+arUrl[i]+'">'+arTitle[i]+'</a><br>';
            }   
        }

    }
}

document.getElementById('test').innerHTML = test;
}

function onQueryFailed(sender, args) {

alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}

I am trying to use caching with sessionstorage when getting items from a list, so it doesn't have to keep querying the list over and over again. It can just check with the sessionstorage and if its in the list there is no need to get the item but if its not in sessionstorage get the item from the list. I am not sure how to do this, my code above shows my attempt

3

You should create a big array that you'll then store into the sessionStorage. And your code is not really clear.... based on what you asked I rewrote it:

// we suppose that "index" is the "Title" we are looking for
var index = "My Title";
// get the data from sessionStorage
!function() {
  var list = sessionStorage.getItem('PrivacyStatementsList');
  var passed = false;
  // if "list" exists
  if (!list) {
    list = JSON.parse(list);
    for (var i=0, len=list.length; i<len; i++) {
      if (list[i].Title === index) {
        passed = true;
        doSomeStuffWithThatItem(list[i]);
        break;
      }
    }
  }

  // "passed" is FALSE so we need to get the data from the list
  if (!passed) {
    SP.SOD.executeFunc('sp.js', 'SP.ClientContext', sharePointReady);
  }
}()

function sharePointReady(){
  var siteUrl = mysite;
  var clientContext = new SP.ClientContext(siteUrl);
  var oList = clientContext.get_web().get_lists().getByTitle('Privacy Statements');

  var camlQuery = new SP.CamlQuery();
  camlQuery.set_viewXml('');
  this.collListItem = oList.getItems(camlQuery);
  clientContext.load(collListItem);
  clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));        
}  

function onQuerySucceeded(sender, args) {
  var listItemEnumerator = collListItem.getEnumerator();
  var list = []; // create an array that will contain all your items
  var oListItem; // declare your variable here instead of redeclare it in the below WHILE
  var itemSelected; // it will be used later

  while (listItemEnumerator.moveNext()) {
    oListItem = listItemEnumerator.get_current();
    // create an hash for each item
    list.push({
      "Title":oListItem.get_item('Title'),
      "PrivacyStatementUrls":oListItem.get_item('PrivacyStatementUrls').get_url(),
      "IsActive":oListItem.get_item('IsActive'),
      "SortOrder":oListItem.get_item('SortOrder')
    })

    // check if the current item is the one we want to deal with
    if (oListItem.get_item('Title') === index) {
      itemSelected = list[list.length-1];
    }
  }
  // now save the array
  sessionStorage.setItem('PrivacyStatementsList', JSON.stringify(list));

  // and do some stuff with the selected item
  doSomeStuffWithThatItem(itemSelected);
}

function onQueryFailed(sender, args) {
  alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}

function doSomeStuffWithThatItem(item) {
  alert(item.PrivacyStatementUrls)
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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