0

I try to create button that will be add item to the list. Unfortunately I do somethings wrong. My programming skill's are at beginner level.
I have list "List" with only one field Title (for test) - at default it will be User and Date.

Code that I have use to create item:

function postSurvey()
{
           var context = new SP.ClientContext.get_current();
           var myWeb = context.get_web();
           var myList = myWeb.get_lists().getByTitle('List');
           var itemCreateInfo = new SP.ListItemCreationInformation();
           var listItem = myList.addItem(itemCreateInfo);
           listItem.set_item("Title", Title.value);
           listItem.update();

           context.executeQueryAsync(Function.createDelegate(this, AddNewEmployeeSuccess),Function.createDelegate(this, AddNewEmployeeFail));

function AddNewEmployeeFail(sender, args)
{
  alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}
function AddNewEmployeeSuccess(sender, args)
{
  alert('Item created: ' + listItem.get_id());
}
}     

Script:

<script type='text/javascript' src="../SitePages/AddingNewUser.js"></script>

Button def.:

<tr>
  <td><input type='text' id='Title'/></td>  
</tr>
<tr>
  <td><button type="button" id="btnAddCar" onclick="postSurvey()">Add</button></td>
</tr>

After click on Button I don't have AddNewEmployeeFail message - just only "Fail" pop-up.

Fail alert

I try this samples without any results:

http://msdn.microsoft.com/en-us/library/office/hh185011(v=office.14).aspx
http://digsharepoint.blogspot.com/2012/03/javascript-front-end-to-start.html
http://ishaalan.wordpress.com/2010/05/23/adding-an-item-to-a-sharepoint-list-using-javascript/ - with this article I can't find SP.js and I don't know where to put this library.

I'm working on SharePoint Online 2010 (on 2013 will some difference's in code?) .

4
  • Does adding context.load(listItem); after .update() help? Apr 3, 2014 at 7:03
  • Robert, I have add context.load(listItem) like you suggest - without any positive result. Still have "Fail". Apr 3, 2014 at 7:12
  • what stacktrace error message you get? May 2, 2014 at 5:08
  • Varun, how I can get error messager detail? I have only popup "Fail" May 5, 2014 at 7:44

2 Answers 2

1

Setting Title value

The line:

listItem.set_item("Title", Title.name);

is used for setting Title column from Title.name object.

Are you using something like this to initialize Title object?

var Title = {};
Title.name = $('#title').val();

Ensure sp.js library is loaded

In order to ensure that sp.js is loaded, change function your function to

function postSurvey()
{
   SP.SOD.executeOrDelayUntilScriptLoaded(function(){
      var context = new SP.ClientContext.get_current();
      //your remaining code goes here...  
   }, 'sp.js');
}
2
  • Stil fail. I have change code, like you says. Apr 24, 2014 at 7:49
  • Or, how I can call out Workflow that will add item to the list? Apr 24, 2014 at 11:12
0

Try this:

listItem.update();

context.load(listItem);

           context.executeQueryAsync(Function.createDelegate(this, AddNewEmployeeSuccess),Function.createDelegate(this, AddNewEmployeeFail));

Your Answer

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

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