0

My script uses an array like this:

var locations = [
    ['Location 1 Name', 'New York, NY', 'Location 1 URL'],
    ['Location 2 Name', 'Newark, NJ', 'Location 2 URL'],
    ['Location 3 Name', 'Philadelphia, PA', 'Location 3 URL'],
];

I have a page with two list webparts (A and B). webpart A/List A has two columns

Title and Address

Using javascript, i need to be able to grab the data from Address column and pass that to my array.

I've tried ANYTHING javscript related for sharepoint, but nothing seems to run.

i have script.js in my SiteAssets folder and in a CONTENT EDITOR webpart i added the call for the script <script src=pathToMyScript> </script>

I tried running a alert("hello"); to confirm that its working and the alert works fine, but as soon as i add javascript that i've seen anywhere in this page or other pages, nothing seems to be working as nothing shows or loads.

Things to note: Treat me as if i knew nothing about javascript and sharepoint. However, i do know how to program in javascript. I never used sp.js libraries.

  • (IMHO) the best way to learn programming against SharePoint is to use the Chrome Browser and start with Chrome Snippets, that way you pass all the loading SharePoint libraries and focus on what you want to get done first. After you succeeded there you make the step to load it in a SharePoint page. It is a reversal of typical development in blogs.. But those Chrome Snippets are a a very powerful way of quickly getting results and not getting disparaged by SharePoint code. It is also easier to post code with your StackOverflow questions then – Danny '365CSI' Engelman Jan 9 '17 at 17:45
  • Yup, i tried that too, but does sharepoint load sp.js and all the requried libraries automatically or do i have to reference to them manually? for some reason, when i run the snippets nothing happens.. i saw somewhere a code where the script is supposed to grab the data from a list then alert(list) but i get [Object Object]. I copied and pasted sharepoint.stackexchange.com/questions/178060/… (see moryas answer) and it didnt do anything. no excel sheet was created – user1986245 Jan 9 '17 at 19:00
  • Check the Network tab on what is loaded. sp.js is so generic it is loaded on almost all pages. Since you are coding from a Chrome Snippet your code always executes after all resources have loaded. So if you get errors now, it is your code, not a missing sp.js. If you need stuff like userprofiles etc. you will get syntax or undefined errors in the Console and you know something is missing. – Danny '365CSI' Engelman Jan 9 '17 at 20:21
  • Oh, and try to avoid alert That is 1996 script kiddie coding... Learn to use console.log( ) (warning: don't leave console statements in the final code, as Internet Explorer only allows use when the F12 console is opened, so when your user says it doesn't work, your test with the Console openened will work... I burn myself on this one in every project still – Danny '365CSI' Engelman Jan 9 '17 at 20:24
  • Running this on a snipper gets me undefined – user1986245 Jan 9 '17 at 21:54
0

Answering to question at below link, putting answer here as below is marked as duplicate. Passing List Items to Array

If I understood correctly, you need 2D javascript array...I have not tested below but to give you idea, it should work.

function onQuerySucceeded(sender, args) {
    var locations = new Array(collListItem.get_count());
    var listItemInfo = '';

    var listItemEnumerator = collListItem.getEnumerator();

    while (listItemEnumerator.moveNext()) {
        var oListItem = listItemEnumerator.get_current();

        eventName = oListItem.get_item('Title');
        eventAddress = oListItem.get_item('WorkAddress');
        eventURL = website.get_url() + '/' + 'Lists/North East Reports/DispForm.aspx?ID=' + oListItem.get_id();
        var newLocation = [eventName, eventAddress, eventURL];
        locations.push(newLocation);

         listItemInfo += 
               //'\nID: ' + oListItem.get_id() +
               '\nEvent Name: ' +  eventName +//oListItem.get_item('Title') + 
            '\nAddress: ' + eventAddress +//oListItem.get_item('WorkAddress') +
            '\nURL: ' + eventURL;//website.get_url() + '/' + 'Lists/North East Reports/DispForm.aspx?ID=' + oListItem.get_id(); 
    }

    //alert(listItemInfo.toString());
}
  • Seems to be going the right path, but marker pins are still not generated.. see previous answer comment for complete code, i added your changes to my script, but yet no pins are being generated on the map. It's almost as if the array is not getting to the second part of the script. – user1986245 Jan 11 '17 at 3:25
0

You need to create locations array and then push location with properties. like this.

// Create Array 
var locations = [];
// Push items into array
locations.push(eventName, eventAddress, eventURL);

Here is complete code.

function onQuerySucceeded(sender, args) {

var listItemInfo = '';
var listItemEnumerator = collListItem.getEnumerator();
var locations = [];
while (listItemEnumerator.moveNext()) {

        var oListItem = listItemEnumerator.get_current();
        eventName = oListItem.get_item('Title');
        eventAddress = oListItem.get_item('WorkAddress');
        eventURL = website.get_url() + '/' + 'Lists/North East Reports/DispForm.aspx?ID=' + oListItem.get_id();

        locations.push(eventName, eventAddress, eventURL);
        listItemInfo += 
               //'\nID: ' + oListItem.get_id() +
               '\nEvent Name: ' +  eventName +//oListItem.get_item('Title') + 
            '\nAddress: ' + eventAddress +//oListItem.get_item('WorkAddress') +
            '\nURL: ' + eventURL;//website.get_url() + '/' + 'Lists/North East Reports/DispForm.aspx?ID=' + oListItem.get_id(); 
    }
//alert(listItemInfo.toString());
}

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.