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've been trying to convert this code to jQuery from Javascript but to no avail. This code is for adding a new item to a list which I took from the msdn website. It works in javascript but I need it to run every time the page is loaded. I tried to google for why it wasn't working with javascript and found this. The guy who asked the question said it worked well with jQuery. So, I've been trying to convert this. All help is appreciated! Thanks!!
Note: As I'm working for a client, I'm not allowed to use third party tools like SPServices.

Edit: Code I've done till now:

function createListItem() {

var clientContext = new SP.ClientContext(siteUrl);
var oList = clientContext.get_web().get_lists().getByTitle("Announcements");

var itemCreateInfo = new SP.ListItemCreationInformation();
this.oListItem = oList.addItem(itemCreateInfo);

oListItem.set_item("Title", "My New Item!");
oListItem.set_item("Body", "Hello World!");

oListItem.update();

clientContext.load(oListItem);

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

}

function onQuerySucceeded() {

alert("Item created: " + oListItem.get_id());

}

function onQueryFailed(sender, args) {

alert("Request failed. " + args.get_message() + "\n" + args.get_stackTrace());

} $(document).ready(function() { createListItem(); });

share|improve this question
    
_spBodyOnLoadFunctionNames.push("createListItem"); use this insteed of $(document).ready(function() { createListItem(); }); –  Rajesh Chowdary Dec 27 '12 at 10:40
    
Tried. It freezes up my screen after reloading. –  uberz91 Dec 27 '12 at 10:45
    
are you using firefox browser if so please install firebug (addons.mozilla.org/en-US/firefox/addon/firebug) and check console tab and refresh the browser and check for error –  Rajesh Chowdary Dec 27 '12 at 10:47
    
Thanks for the comment. I'm using IE9. –  uberz91 Dec 27 '12 at 10:48
    
no problem press F12 key and select console tab and check for the error –  Rajesh Chowdary Dec 27 '12 at 10:49

1 Answer 1

up vote 0 down vote accepted

_spBodyOnLoadFunctionNames.push("createListItem"); use this insteed of $(document).ready(function() { createListItem(); });

Hope this helps

share|improve this answer
    
Hi, I'm now getting the error: "Unable to get value of the property 'get_current': object is null or undefined" after I refresh the page. Please help. –  uberz91 Dec 27 '12 at 12:07
    
send me the code how you are calling that method and all –  Rajesh Chowdary Dec 27 '12 at 13:03
    
I replaced _spBodyOnLoadFunctionNames.push("createListItem"); with $(document).ready(function() { ExecuteOrDelayUntilScriptLoaded(createListItem,"SP.js");}); It seems to work properly now. Thanks. –  uberz91 Dec 27 '12 at 13:39

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.