0

I have an external website that is pretty much a form that interfaces with another enterprise software. One of the requirements of the fields in the form is to provide a location. We have the list of locations in our SharePoint, so I want to reference the SharePoint locations in a cascading drop down list on our form.

I am pretty new to referencing SharePoint-related services to external websites, and could really use a jumpstart. I've tried YouTube searching video demonstrations and reading some articles, but could not find my solution. I'm using predominantly HTML/CSS/JS/JQUERY for our external website.

1 Answer 1

0
//Assuming Column1 is the parent column in sharepoint list and Column2 is child column
<script type="text/javascript" src="filelink/jquery-1.6.1.min.js"></script>
<script language="javascript" type="text/javascript">
var dropdown1 = document.getElementById('DropDown1');   //get dropdown1 by id
var dropdown2 = document.getElementById('DropDown2');   //get dropdown2 by id
var dropdown1Array=[];
var dropdown2Array=[];    //populate 1st dropdown
var call1 = $.ajax({
        url: "absolute URL" +  "/_api/Web/Lists/GetByTitle('ListName')/Items?$select=column1",
        type: "GET",
        dataType: "json",
        headers: {
            Accept: "application/json;odata=verbose"
        }
    });
    $.when(call1).done(function(data, textStatus, jqXHR) {
        data.d.results.forEach(function(entry) {
            //inserting unique values in dropdown1
            if (dropdownArray1.indexOf(entry.column1) == -1) { 
                dropdownArray1.push(entry.column1);
                //inserting items into dropdown1
                    var child = document.createElement("option");
                    child.textContent = entry.column1;
                    child.value = entry.column1;
                    dropdown1.appendChild(child);
                }
        });
    });
//populate 2nd dropwon in cascaded format
dropdown1.addEventListener("change", function () {
 while (dropdown2.firstChild) {
    dropdown2.removeChild(dropdown2.firstChild);
}
var call2 = $.ajax({
        url: "absolute URL" +  "/_api/Web/Lists/GetByTitle('ListName')/Items?$select=column2&$filter=column1 eq dropdown1.value",
        type: "GET",
        dataType: "json",
        headers: {
            Accept: "application/json;odata=verbose"
        }
    });
    $.when(call2).done(function(data, textStatus, jqXHR) {
        data.d.results.forEach(function(entry) {
            //inserting unique values in dropdown2
            if (dropdownArray2.indexOf(entry.column2) == -1) { 
                dropdownArray2.push(entry.column2);
                //inserting items into dropdown2
                    var child = document.createElement("option");
                    child.textContent = entry.column2;
                    child.value = entry.column2;
                    dropdown2.appendChild(child);
                }
        });
    });
});                 
</script>           
3
  • First off, thank you so much for this code! I really appreciate it. Second, could you help walk me through the code and getting this to display the dropdown with my SharePoint list? Do I need to create a separate dropdown list and reference this into the dropdown list's class? Thanks again! Commented Nov 20, 2015 at 16:59
  • From my understanding you have SharePoint List which contains some locations that you are going to populate in external website. So if this is the case then you can use above code to fetch data from list. Replace the Title with column name containing location. Commented Nov 21, 2015 at 11:33
  • Correct, however I'm a little confused in order to get it functioning. I copy and paste it into the body, but still do not see anything. Only the comments show up on my webpage, but that's because it's using the javascript comment syntax instead of html (but that's totally ok). Other than replacing "GetByTitle", are there any other portions I will need to replace? Thank you so much again! Commented Nov 23, 2015 at 6:33

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.