简体   繁体   中英

add new item to sharepoint list in javascript

I want to add a new item to my sharepoint list using javascript. I used the code straight from MSDN but it doesn't work. I think the problem is whith the siteUrl I declare, because when I set alerts like this:

var siteUrl = 'http://units.mil.intra/sites/DGHR/h/default.aspx';

    function createListItem() {
        alert('in function ');
        var clientContext = new SP.ClientContext(siteUrl);
        alert('before oList');
        var oList = clientContext.get_web().get_lists().getByTitle('TestPostModification');
        alert('after oList');   
        var itemCreateInfo = new SP.ListItemCreationInformation();
        this.oListItem = oList.addItem(itemCreateInfo);

        oListItem.set_item('Title', 'Item from de Hrnode!');

        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());
    }

I get the first alert, but I don't get the alert('before oList'); So I think there is a problem whith:

var clientContext = new SP.ClientContext(siteUrl);

can someone help me please?

ps: the siteUrl is on our intranet site, so you can't reach it from where you are.

Please add the following :

ExecuteOrDelayUntilScriptLoaded(createListItem() ,"sp.js");

The script "sp.js" has to be loaded before the code runs

I think it's because the siteURL must not contain the page. So it should be http://units.mil.intra/sites/DGHR/h/ (it means the root directory of your website).

Tips: you can use a web console to debug your code with Firefox or Chrome, or you can install an addon like Firebug for Firefox. No need to use alert() , and console.log() provides more useful info.

Also, the native way to do it it's quite difficult to use and not "pretty". If using jQuery is not an issue for you, then I'd recommend to use a third library like SPServices or SharepointPlus . For example with SharepointPlus your code will become:

// if your code runs somewhere under this website, then you don't need to provide the URL
var siteUrl = 'http://units.mil.intra/sites/DGHR/h/';

$SP().list("TestPostModification", siteUrl).add({Title:"Item from de Hrnode!"}, {
  success:function(items) {
    if (items.length === 1) alert("Item created: "+items[0].ID)
  },
  error:function(items) {
    if (items.length > 0) alert("Request failed: "+items[0].errorMessage)
  }
})

From what am seeing you should use this as the siteUrl siteUrl = ' http://units.mil.intra/sites/DGHR/h/ ';

The siteUrl you used will direct it to a webpage which is not whats needed. The list is is housed within a sharepoint site and you already have the name of the list referenced in your code // var oList = clientContext.get_web().get_lists().getByTitle('TestPostModification');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM