Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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?) .

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

2 Answers 2

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');
}
share|improve this answer
    
Stil fail. I have change code, like you says. –  Krzysztof S. Apr 24 at 7:49
    
Or, how I can call out Workflow that will add item to the list? –  Krzysztof S. Apr 24 at 11:12

Try this:

listItem.update();

context.load(listItem);

           context.executeQueryAsync(Function.createDelegate(this, AddNewEmployeeSuccess),Function.createDelegate(this, AddNewEmployeeFail));
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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