Add listitem with javascript 分类: Sharepoint 2015-07-16 20:23 4人阅读 评论(0) 收藏

时间:2024-01-04 15:47:08
  SP.SOD.executeFunc('sp.js', 'SP.ClientContext', createListItem);//makes sure sp.js is loaded and then calls createListItem method
function createListItem() {
var clientContext = new SP.ClientContext.get_current();//if the page and the list are in same site.If list is in different site then use relative url instead of get_current
var oList = clientContext.get_web().get_lists().getByTitle('ListName'); 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());
}