尝试从Sharepoint上的列表中获取数据并将其发布到外部网页上的下拉列表中

时间:2015-11-24 19:16:10

标签: javascript jquery html dom sharepoint

我的目标是拥有一个表单的外部网页。我正在尝试从共享点列表中获取数据,并且已经使用此代码来处理:

//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>           

问题是,我在解析和理解代码时遇到了困难,更不用说让它工作了。我已将代码复制并粘贴到正文中的网页中,但它不起作用。我试图查看我必须用真实的网址或标题替换哪些部分,但我不知道从哪里开始。我不擅长SharePoint,并且要求更加温和地分析我需要做些什么来使这段代码工作。

0 个答案:

没有答案