0

I know almost nothing about javascript, clearly. I have the script below that gets a list by name and it works perfectly. My problem is that when I copy it and insert a new list name for a second list, it does not work. I've tried many things but cannot figure out how to alter/combine the code to work for multiple lists. Any advice is greatly appreciated.

Using SharePoint 2013 Foundation Server

<script type="text/javascript" src="//ajax.aspnetcdn.com/ajax/4.0/1/MicrosoftAjax.js"></script>
<script type="text/javascript" src="_layouts/15/sp.runtime.js"></script>
<script type="text/javascript" src="_layouts/15/sp.js"></script>
<script type="text/javascript" src="https://code.jquery.com/jquery-2.2.4.min.js"></script>

<script type="text/javascript">
$(function(){
GetProducts();
});

function GetProducts() {
var clientContext = new SP.ClientContext.get_current();
var oList = clientContext.get_web().get_lists().getByTitle('HRKPI');
this.collListItem = oList.getItems("");
clientContext.load(collListItem);
clientContext.executeQueryAsync(
Function.createDelegate(this, this.onQuerySucceeded),
Function.createDelegate(this, this.onQueryFailed)
);
}

function onQuerySucceeded(sender, args) {
var listItemEnum = collListItem.getEnumerator();
var ProductInfo = '<div>';

while (listItemEnum.moveNext()) {
var oListItem = listItemEnum.get_current();
ProductInfo += '<div id="pParentSquareKPI"> <p id="pKPIName">' +
oListItem.get_item('Name') + '</p>' +
'<p id="pKPIValue">' + oListItem.get_item('Value') + '</p> </div>';
}

ProductInfo += '</div>';
$('#products').html(ProductInfo);
}

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

</script>
  • Are you getting any error after you change the list name... If Yes, please add the error here. – Ganesh Sanap Apr 19 at 3:23
  • How do I check for errors? The code works but then the original list disappears as if I can only have one at a time – SteveMills04 Apr 20 at 15:40
  • You can check for errors in browsers console window. Also, try appending the second lists data to another html div. – Ganesh Sanap Apr 20 at 15:50
0

May be this will help you

Change List name on below line

var oList = clientContext.get_web().get_lists().getByTitle('HRKPI');

As your code says that you are using Name and Value column in code so make sure you have that column on your list. Or you need to change that column internal name as per your new list columns

ProductInfo += '<div id="pParentSquareKPI"> <p id="pKPIName">' +
oListItem.get_item('Name') + '</p>' +
'<p id="pKPIValue">' + oListItem.get_item('Value') + '</p> </div>';
  • I've tried this. The code works but then my original list data disappears. – SteveMills04 Apr 20 at 15:41
  • Then you should bind data on other div or append on current div – Bharat Khunti Apr 20 at 15:42
0

Ok, I think I got it. The only thing I did differently that my prior attempts was I changed the variable 'clientContext' for the GetNews() function to ensure it was different from the GetProducts() function. Thanks all for your help!

<script type="text/javascript" src="//ajax.aspnetcdn.com/ajax/4.0/1/MicrosoftAjax.js"></script>
<script type="text/javascript" src="_layouts/15/sp.runtime.js"></script>
<script type="text/javascript" src="_layouts/15/sp.js"></script>
<script type="text/javascript" src="https://code.jquery.com/jquery-2.2.4.min.js"></script>

<script type="text/javascript">
$(function(){
GetProducts();
GetNews();
});

function GetProducts() {
var clientContext = new SP.ClientContext.get_current();
var oList = clientContext.get_web().get_lists().getByTitle('HRKPI');
this.collListItem = oList.getItems("");
clientContext.load(collListItem);
clientContext.executeQueryAsync(
Function.createDelegate(this, this.onQuerySucceeded),
Function.createDelegate(this, this.onQueryFailed)
);
}

function GetNews() {
var nclientContext = new SP.ClientContext.get_current();
var nList = nclientContext.get_web().get_lists().getByTitle('NewsInfo');
this.ncollListItem = nList.getItems("");
nclientContext.load(ncollListItem);
nclientContext.executeQueryAsync(
Function.createDelegate(this, this.onQuerySucceeded),
Function.createDelegate(this, this.onQueryFailed)
);
}

function onQuerySucceeded(sender, args) {
var listItemEnum = collListItem.getEnumerator();
var nlistItemEnum = ncollListItem.getEnumerator();
var ProductInfo = '<div>';
var NewsInfoHTML = '<div>';

while (listItemEnum.moveNext()) {
var oListItem = listItemEnum.get_current();
ProductInfo += '<div id="pParentSquareKPI"> <p id="pKPIName">' +
oListItem.get_item('Name') + '</p>' +
'<p id="pKPIValue">' + oListItem.get_item('Value') + '</p> </div>';
}

while (nlistItemEnum.moveNext()) {
var nListItem = nlistItemEnum.get_current();
NewsInfoHTML += '<div id="nParentSquare"> <p id="nNewsName">' +
nListItem.get_item('Name') + '</p>' +
'<p id="nNewsID">' + nListItem.get_item('ID') + '</p> </div>';
}

ProductInfo += '</div>';
$('#products').html(ProductInfo);

NewsInfoHTML += '</div>';
$('#news').html(NewsInfoHTML);
}

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

</script>

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.