简体   繁体   English

无法使用JavaScript检索SPList项目

[英]Cant retrieve SPList items using javascript

Im trying to use a javascript to search a Sharepoint list for a person with the name "John". 我正在尝试使用JavaScript在Sharepoint列表中搜索名称为“ John”的人。 It seems as if the query fails because I get an alert with the error: Request failed. 好像查询失败,因为我收到以下错误警报: 请求失败。 One or more field types are not installed properly. 一种或多种字段类型未正确安装。 Go to the list settings page to delete these fields. 转到列表设置页面以删除这些字段。 undefined 未定义

This suggests that the script ends up in the onQueryFailed()-method. 这表明脚本最终以onQueryFailed()方法结束。 I cant find anything wrong with the CAML and the internal column names are used. 我找不到CAML有什么问题,并且使用了内部列名。

Code from .aspx: .aspx中的代码:

<script type="text/javascript">
            function MyJavascript() {
            var siteUrl = '/sites/MySite/';
            var clientContext = new SP.ClientContext(siteUrl);
            var oList = clientContext.get_web().get_lists().getByTitle('People');

            var camlQuery = new SP.CamlQuery();
            camlQuery.set_viewXml('<View><Query><Where><Eq><FieldRef Name="\'Name\'"/>' +
            '<Value Type=\'Text\'>John</Value></Eq></Where></Query></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() +
                '\nName: ' + oListItem.get_item('Name') +
                '\nAge: ' + oListItem.get_item('Age');
            }

            alert(listItemInfo.toString());
        }

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

        </script> 

Code from the .cs that registers the script to a button: 将脚本注册到按钮的.cs中的代码:

SubmitButton.Attributes.Add("onclick", "MyJavascript(); return false;");

Any ideas? 有任何想法吗?

Try putting ExecuteOrDelayUntilScriptLoaded(MyJavaScript, "sp.js"); 尝试将ExecuteOrDelayUntilScriptLoaded(MyJavaScript, "sp.js"); directly below your siteUrl variable. 直接在您的siteUrl变量下方。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM