简体   繁体   中英

Cant retrieve SPList items using javascript

Im trying to use a javascript to search a Sharepoint list for a person with the name "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. I cant find anything wrong with the CAML and the internal column names are used.

Code from .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:

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

Any ideas?

Try putting ExecuteOrDelayUntilScriptLoaded(MyJavaScript, "sp.js"); directly below your siteUrl variable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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