Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/sharepoint/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用JavaScript从SharePoint列表检索数据_Sharepoint - Fatal编程技术网

如何使用JavaScript从SharePoint列表检索数据

如何使用JavaScript从SharePoint列表检索数据,sharepoint,Sharepoint,我是SharePoint新手,正在尝试使用JavaScript从SharePoint列表中读取数据。我创建了一个新的SP页面并插入了一个内容编辑器web部件。在内容编辑器中,我插入了以下代码。单击“保存”时,我会看到代码本身,但不会看到SP列表中的内容。如何使用JS从SP列表中读取数据 <!DOCTYPE html> <html> <head></head> <body></body> <script> var

我是SharePoint新手,正在尝试使用JavaScript从SharePoint列表中读取数据。我创建了一个新的SP页面并插入了一个内容编辑器web部件。在内容编辑器中,我插入了以下代码。单击“保存”时,我会看到代码本身,但不会看到SP列表中的内容。如何使用JS从SP列表中读取数据

<!DOCTYPE html>
<html>
<head></head>
<body></body>
<script>
var siteUrl = '/sites/MySiteCollection';

function retrieveListItems() {

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

    var camlQuery = new SP.CamlQuery();
    camlQuery.set_viewXml('<View><Query><Where><Geq><FieldRef Name=\'ID\'/>' + 
        '<Value Type=\'Number\'>1</Value></Geq></Where></Query><RowLimit>10</RowLimit></View>');
    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 listItemInfo = '';

    var listItemEnumerator = collListItem.getEnumerator();

    while (listItemEnumerator.moveNext()) {
        var oListItem = listItemEnumerator.get_current();
        listItemInfo += '\nID: ' + oListItem.get_id() + 
            '\nTitle: ' + oListItem.get_item('Title') + 
            '\nBody: ' + oListItem.get_item('Body');
    }

    document.write(listItemInfo.toString());
    //alert(listItemInfo.toString());
}

function onQueryFailed(sender, args) {

    alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}
</script>
</html>

var siteUrl='/sites/MySiteCollection';
函数retrieveListItems(){
var clientContext=new SP.clientContext(siteUrl);
var oList=clientContext.get_web().get_lists().getByTitle(“公告”);
var camlQuery=new SP.camlQuery();
camlQuery.set_viewXml(“”+
'110');
this.collListItem=oList.getItems(camlQuery);
加载(collListItem);
clientContext.executeQueryAsync(Function.createDelegate(this,this.onQuerySucceed),Function.createDelegate(this,this.onQueryFailed));
}
函数onquerysucceed(发送方,参数){
var listItemInfo='';
var listItemEnumerator=collListItem.getEnumerator();
while(listItemEnumerator.moveNext()){
var oListItem=listItemEnumerator.get_current();
listItemInfo+='\nID:'+oListItem.get_id()+
'\n标题:'+oListItem.get_项目('Title')+
'\n正文:'+oListItem.get_项('Body');
}
document.write(listItemInfo.toString());
//警报(listItemInfo.toString());
}
函数onQueryFailed(发送方,参数){
警报('请求失败。'+args.get_message()+'\n'+args.get_stackTrace());
}
示例演示:

<!DOCTYPE html>
<html>
<head>
</head>
<body>
    <script>
        //var siteUrl = '/sites/MySiteCollection';

        function retrieveListItems() {
            //get current site context
            var clientContext = new SP.ClientContext.get_current();//SP.ClientContext(siteUrl);
            var oList = clientContext.get_web().get_lists().getByTitle('Test');

            var camlQuery = new SP.CamlQuery();
            camlQuery.set_viewXml('<View><Query><Where><Geq><FieldRef Name=\'ID\'/>' +
                '<Value Type=\'Number\'>1</Value></Geq></Where></Query><RowLimit>10</RowLimit></View>');
            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 listItemInfo = '';

            var listItemEnumerator = collListItem.getEnumerator();

            while (listItemEnumerator.moveNext()) {
                var oListItem = listItemEnumerator.get_current();
                listItemInfo += '\nID: ' + oListItem.get_id() +
                    '\nTitle: ' + oListItem.get_item('Title') +
                    '\nBody: ' + oListItem.get_item('Country');// custom field
            }

            //document.write(listItemInfo.toString());
            document.getElementById("DataResult").innerHTML = listItemInfo;
            //alert(listItemInfo.toString());
        }

        function onQueryFailed(sender, args) {

            alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
        }
    </script>
    <div id="DataResult">

    </div>
    <input id="Button1" onclick="retrieveListItems()" type="button" value="retrieveListItems" />
</body>

</html>

//var siteUrl='/sites/MySiteCollection';
函数retrieveListItems(){
//获取当前站点上下文
var clientContext=new SP.clientContext.get_current();//SP.clientContext(siteUrl);
var oList=clientContext.get_web().get_lists().getByTitle('Test');
var camlQuery=new SP.camlQuery();
camlQuery.set_viewXml(“”+
'110');
this.collListItem=oList.getItems(camlQuery);
加载(collListItem);
clientContext.executeQueryAsync(Function.createDelegate(this,this.onQuerySucceed),Function.createDelegate(this,this.onQueryFailed));
}
函数onquerysucceed(发送方,参数){
var listItemInfo='';
var listItemEnumerator=collListItem.getEnumerator();
while(listItemEnumerator.moveNext()){
var oListItem=listItemEnumerator.get_current();
listItemInfo+='\nID:'+oListItem.get_id()+
'\n标题:'+oListItem.get_项目('Title')+
'\n正文:'+oListItem.get_项('Country');//自定义字段
}
//document.write(listItemInfo.toString());
document.getElementById(“DataResult”).innerHTML=listItemInfo;
//警报(listItemInfo.toString());
}
函数onQueryFailed(发送方,参数){
警报('请求失败。'+args.get_message()+'\n'+args.get_stackTrace());
}

示例演示:

<!DOCTYPE html>
<html>
<head>
</head>
<body>
    <script>
        //var siteUrl = '/sites/MySiteCollection';

        function retrieveListItems() {
            //get current site context
            var clientContext = new SP.ClientContext.get_current();//SP.ClientContext(siteUrl);
            var oList = clientContext.get_web().get_lists().getByTitle('Test');

            var camlQuery = new SP.CamlQuery();
            camlQuery.set_viewXml('<View><Query><Where><Geq><FieldRef Name=\'ID\'/>' +
                '<Value Type=\'Number\'>1</Value></Geq></Where></Query><RowLimit>10</RowLimit></View>');
            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 listItemInfo = '';

            var listItemEnumerator = collListItem.getEnumerator();

            while (listItemEnumerator.moveNext()) {
                var oListItem = listItemEnumerator.get_current();
                listItemInfo += '\nID: ' + oListItem.get_id() +
                    '\nTitle: ' + oListItem.get_item('Title') +
                    '\nBody: ' + oListItem.get_item('Country');// custom field
            }

            //document.write(listItemInfo.toString());
            document.getElementById("DataResult").innerHTML = listItemInfo;
            //alert(listItemInfo.toString());
        }

        function onQueryFailed(sender, args) {

            alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
        }
    </script>
    <div id="DataResult">

    </div>
    <input id="Button1" onclick="retrieveListItems()" type="button" value="retrieveListItems" />
</body>

</html>

//var siteUrl='/sites/MySiteCollection';
函数retrieveListItems(){
//获取当前站点上下文
var clientContext=new SP.clientContext.get_current();//SP.clientContext(siteUrl);
var oList=clientContext.get_web().get_lists().getByTitle('Test');
var camlQuery=new SP.camlQuery();
camlQuery.set_viewXml(“”+
'110');
this.collListItem=oList.getItems(camlQuery);
加载(collListItem);
clientContext.executeQueryAsync(Function.createDelegate(this,this.onQuerySucceed),Function.createDelegate(this,this.onQueryFailed));
}
函数onquerysucceed(发送方,参数){
var listItemInfo='';
var listItemEnumerator=collListItem.getEnumerator();
while(listItemEnumerator.moveNext()){
var oListItem=listItemEnumerator.get_current();
listItemInfo+='\nID:'+oListItem.get_id()+
'\n标题:'+oListItem.get_项目('Title')+
'\n正文:'+oListItem.get_项('Country');//自定义字段
}
//document.write(listItemInfo.toString());
document.getElementById(“DataResult”).innerHTML=listItemInfo;
//警报(listItemInfo.toString());
}
函数onQueryFailed(发送方,参数){
警报('请求失败。'+args.get_message()+'\n'+args.get_stackTrace());
}


Lee\u MSFT感谢您的反馈。但是,我仍然有一个显示所有代码的页面。除了将代码添加到内容编辑器之外,您还做了什么吗?别介意我之前的评论。我知道我必须先点击“嵌入代码”,然后把代码放在那里。现在我看到按钮了。谢谢顺便问一下,传递给getByTitle的参数Test是什么?“测试”是你从中获得数据的列表标题。谢谢你的反馈。但是,我仍然有一个显示所有代码的页面。除了将代码添加到内容编辑器之外,您还做了什么吗?别介意我之前的评论。我知道我必须先点击“嵌入代码”,然后把代码放在那里。现在我看到按钮了。谢谢顺便问一下,传递给getByTitle的参数Test是什么?这就是我试图从中读取的页面吗?“Test”是从中获取数据的列表标题。