1

I am trying to learn how to use javascript to interact with SharePoint list. So I made a .html and a .js file (the html uses the js) following the Microsoft tutorial. I uploaded these files in a SharePoint library and I linked the html to a Web Part (content editor).

The HTML File :

<script src="https://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script>
<script src="/the/link/to/the/js/file/in/sharepoint.js" type="text/javascript"></script>

<script type="text/javascript">
  $(document).ready(function(){
    ExecuteOrDelayUntilScriptLoaded(function(){
      Suivi.Chantier.retrieveListItems()
    },"sp.js");
  });
</script>

<h1>TEST JAVASCRIPT</h1>

The JS File :

"use strict"

var Suivi = window.Suivi || {};

Suivi.Chantier = function() {
  var siteUrl = '/espaces/DR-IDFO-Interventions';
  var collListItem

  var retrieveListItems = function () {

      var clientContext = new SP.ClientContext(siteUrl);
      var oList = clientContext.get_web().get_lists().getByTitle('Rapports sur le contenu et la structure');

      var camlQuery = new SP.CamlQuery();
      collListItem = oList.getItems(camlQuery);

      clientContext.load(collListItem);
      clientContext.executeQueryAsync(Function.createDelegate(this, onQuerySucceeded), Function.createDelegate(this, onQueryFailed));
  }

  var onQuerySucceeded = function (sender, args) {

      var listItemInfo = '';

      var listItemEnumerator = collListItem.getEnumerator();

      while (listItemEnumerator.moveNext()) {
          var oListItem = listItemEnumerator.get_current();
          listItemInfo += 'Title: ' + oListItem.get_item('LinkTitle');
      }

      console.log(listItemInfo.toString());
  }

  var onQueryFailed = function (sender, args) {

      console.log('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
  }

  return {
    retrieveListItems: retrieveListItems
  }
}()

However, when the page load, I get the following errors :

sp.js?rev=b7Wjp3fete%2Bkyv8jTz1FWg%3D%3D:2 Uncaught Error: The property or field has not been initialized
    at Function.Error.create (ScriptResource.axd?d=yMsrpWWcx2JZcJhKmR_O6_uXEDDHPuvdtl30sD63nNijPo8F9vahWaA-6PWl-paDl_IU6o5d5i5AKLAcIuj_aF9ICMnuGkIqITYnUHmJABM0dOMJ95kv9oW7y2NhLyLg15W97MapK4WH69pvYysumt3Tw9npgrQ8PwfIh4ezCi3SHyMSafbNPKDnBZ1vgNs00&t=72e85ccd:5)
    at SP.ListItem.$Q_2 (sp.js?rev=b7Wjp3fete%2Bkyv8jTz1FWg%3D%3D:2)
    at SP.ListItem.get_item (sp.js?rev=b7Wjp3fete%2Bkyv8jTz1FWg%3D%3D:2)
    at Object.onQuerySucceeded (suiviChantier_getListItems.js:29)
    at Array.<anonymous> (ScriptResource.axd?d=yMsrpWWcx2JZcJhKmR_O6_uXEDDHPuvdtl30sD63nNijPo8F9vahWaA-6PWl-paDl_IU6o5d5i5AKLAcIuj_aF9ICMnuGkIqITYnUHmJABM0dOMJ95kv9oW7y2NhLyLg15W97MapK4WH69pvYysumt3Tw9npgrQ8PwfIh4ezCi3SHyMSafbNPKDnBZ1vgNs00&t=72e85ccd:5)
    at ScriptResource.axd?d=yMsrpWWcx2JZcJhKmR_O6_uXEDDHPuvdtl30sD63nNijPo8F9vahWaA-6PWl-paDl_IU6o5d5i5AKLAcIuj_aF9ICMnuGkIqITYnUHmJABM0dOMJ95kv9oW7y2NhLyLg15W97MapK4WH69pvYysumt3Tw9npgrQ8PwfIh4ezCi3SHyMSafbNPKDnBZ1vgNs00&t=72e85ccd:5
    at SP.ClientRequest.$32_0 (sp.runtime.js?rev=5f2WkYJoaxlIRdwUeg4WEg%3D%3D:2)
    at Array.<anonymous> (ScriptResource.axd?d=yMsrpWWcx2JZcJhKmR_O6_uXEDDHPuvdtl30sD63nNijPo8F9vahWaA-6PWl-paDl_IU6o5d5i5AKLAcIuj_aF9ICMnuGkIqITYnUHmJABM0dOMJ95kv9oW7y2NhLyLg15W97MapK4WH69pvYysumt3Tw9npgrQ8PwfIh4ezCi3SHyMSafbNPKDnBZ1vgNs00&t=72e85ccd:5)
    at ScriptResource.axd?d=yMsrpWWcx2JZcJhKmR_O6_uXEDDHPuvdtl30sD63nNijPo8F9vahWaA-6PWl-paDl_IU6o5d5i5AKLAcIuj_aF9ICMnuGkIqITYnUHmJABM0dOMJ95kv9oW7y2NhLyLg15W97MapK4WH69pvYysumt3Tw9npgrQ8PwfIh4ezCi3SHyMSafbNPKDnBZ1vgNs00&t=72e85ccd:5
    at Sys.Net.WebRequest.completed (ScriptResource.axd?d=yMsrpWWcx2JZcJhKmR_O6_uXEDDHPuvdtl30sD63nNijPo8F9vahWaA-6PWl-paDl_IU6o5d5i5AKLAcIuj_aF9ICMnuGkIqITYnUHmJABM0dOMJ95kv9oW7y2NhLyLg15W97MapK4WH69pvYysumt3Tw9npgrQ8PwfIh4ezCi3SHyMSafbNPKDnBZ1vgNs00&t=72e85ccd:5)

I am using SharePoint Server 2013

1

you have not defined your camlquery.ViewXml while getting LIst items. Use something like this

camlQuery.ViewXml = "<View><RowLimit>100</RowLimit></View>";

and then execute the code

  • I found the error, it passed a string that is not a header key in the list to the get_item() function. However how do I know what name a column have if it is not the same as the written one ? – Léo Coletta May 23 '18 at 9:45
  • you don't need to worry about the column name. This ViewXML will pick up all the fields whatever are present in default View (AllItems) in most cases – DvG May 23 '18 at 9:51
  • either way, you can use simply collListItem = oList.getItems(); to get all list items – DvG May 23 '18 at 9:53
0

the error caused by the below line, you're passing a wrong field name

listItemInfo += 'Title: ' + oListItem.get_item('LinkTitle');

please use the following instead

listItemInfo += 'Title: ' + oListItem.get_item('Title');

you should pass the internal field name to get_item function, a simple way to know the field internal name is to:

go to your List Settings \Library Settings , scroll down to columns , click on the column name.

you'll find the internal name in the url ,

FldEdit.aspx?List=%7B24039875%2DF8F3%2D40A2%2DAC66%2D0E2BCC837E45%7D&Field=Title

0

When using JSOM, you must specify the properties you will try to read once you get the items you queried. As you don't specify any, you will only be able to read a few standard fields (Id and Title I believe, to be confirmed).

However, you try to read on each item the field LinkTitle (which exists) but has not been explicitely requested, hence the error.

In your clientContext.load(collListItems) statement, you must add the fields you will try to read. The following Stack Overflow post gives that example:

clientContext.load(collListItem, 'Include(Title, empNickName, empPosition, empDepartment)');

Hope this helps!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.