2

I'm using the code from this MSDN guide: http://msdn.microsoft.com/en-us/library/office/hh185011(v=office.14).aspx

Here's what it looks like:

var clientContext = new SP.ClientContext("http://sitecollection/sites/Dev");
var oList = clientContext.get_web().get_lists().getByTitle('User List');
var item = new SP.ListItemCreationInformation();
this.oListItem = oList.addItem(item);

oListItem.set_item('Last Name', 'toby');
oListItem.update();

clientContext.load(oListItem);

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

The onQuerySucceeded and onQueryFailed methods exist and are just basic alerts... nothing fancy.

When I get to this line:

oListItem.set_item('Last Name', 'toby');

I get an error:

0x800a1391 - JavaScript runtime error: 'oListItem' is undefined

A couple questions:

  1. What is oListItem anyway? Why is it referred to as this.oListItem the first time, and just as oListItem subsequently?

  2. How do I properly add an item to a list on my site? My list does exist and is under the URL http://sitecollection/sites/Dev/Lists/User%20List/AllItems.aspx

3 Answers 3

4

Seems like they declared oListItem as a global variable, but omitted the declaration from their code sample. They reference it as this.oListItem to show it's a property outside of the current scope (just like they did with this.onQuerySucceeded) though technically it's unnecessary.

Try like this:

var clientContext = new SP.ClientContext("http://sitecollection/sites/Dev");
var oList = clientContext.get_web().get_lists().getByTitle('User List');
var item = new SP.ListItemCreationInformation();
var oListItem = oList.addItem(item);

Or if you need to access that item in your callback handler:

var oListItem;

function createListItem() {
    var clientContext = new SP.ClientContext("http://sitecollection/sites/Dev");
    ...
}
2
  • OK, this got me past oListItem, but now the callback fails with 0x800a138f - JavaScript runtime error: Unable to get property 'apply' of undefined or null reference. Nothing is added to my list either.
    – tnw
    Feb 28, 2014 at 16:13
  • Since this is a different issue, you should probably ask it as a new question and post as much detail as you can there. Feb 28, 2014 at 16:52
2

I added the above comment because it is true, and it will never work with the space (since you'd already worked past your oListItem undefined issue). oListItem is merely a variable, you could call it anything you want. Microsoft uses that name in keeping with their oData theme, they use o for every variable on that whole tutorial pretty much. If it isn't working for a different reason, then I'm wondering where you're applying the code. Are you trying to do this with a SharePoint app? If so then that is your problem, simply generating a client context in this manner is insufficient. You'll need to get the current context, and the host context. See below:

var context = new SP.ClientContext.get_current();
var hostContext = new SP.AppContextSite(context, hostUrl);
var hostWeb = hostContext.get_web();

Use the hostContext and hostWeb variables in place of the normal context and web. I recommend reading the whole tutorial and further educating yourself on the SP namespace.

4
  • Please edit your original answer instead if posting multiple answers to the same question :) Mar 2, 2014 at 7:44
  • Can you go a step further with this? I'm unsure how to use hostWeb rather than clientContext. If I try to use hostWeb.load(oListItem), I get object doesn't support that method
    – tnw
    Mar 3, 2014 at 16:05
  • var yourList = hostWeb.get_lists().getByTitle('yourList'); context.load(yourList); Mar 9, 2014 at 3:01
  • 1
    That's just an example, but that should give you the general idea. The hostContext gives you the hosWeb which you need for host objects. Then use the current context to execute your queries. Mar 9, 2014 at 3:06
2

SharePoint uses the field internal name which does not contain spaces. Try LastName or Last_x0020_Name. Certain characters are encoded in the internal name. This is very common when the field is created via the user interface.

Examples:

[space] _x0020_
-       _x002D_
(       _x0028_
)       _x0029_
2
  • If you copy paste his "example" codes, please realize that the X in each case should be lower case 'x'. putting in Last_X0020_Name will not work.
    – user11457
    Apr 17, 2015 at 20:17
  • Correct, I didn't put those examples in. Those were put in on an edit. I have updated this though. Apr 22, 2015 at 14:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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