ECMAScript对列表项的增删改查

一、新增

代码
< script type = " text/javascript " >

var siteUrl = ' /sites/MySiteCollection ' ;

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

< / script>

 

二、查询 

代码
var product;
function getProductById(productId) {
try {
var context = new SP.ClientContext.get_current();
var web = context.get_web();
var list = web.get_lists().getByTitle( ' product ' );
this .product = list.getItemById(productId);
context.load(product,
' ProductName ' , ' ProductDescription ' , ' ProductType ' , ' LaunchDate ' , ' AvailableQuantity ' );
context.executeQueryAsync(Function.createDelegate(
this , this .productReceived), Function.createDelegate( this , this .failed));
}
catch (e) {
alert(e);
}
}

 

代码
var productcollection;
function getProducts(title) {
try {
var context = new SP.ClientContext.get_current();
var web = context.get_web();
var list = web.get_lists().getByTitle( ' product ' );
var query = ' <View Scope=\ ' RecursiveAll\ ' > ' +
' <Query> ' +
' <Where> ' +
' <Contains> ' +
' <FieldRef Name=\ ' ProductName\ ' /> ' +
' <Value Type=\ ' Text\ ' > ' + title + ' </Value> ' +
' </Contains> ' +
' </Where> ' +
' </Query> ' +
' </View> ' ;
var camlQuery = new SP.CamlQuery();
camlQuery.set_viewXml(query);

this .productcollection = list.getItems(camlQuery);
context.load(
this .productcollection, ' Include(ProductName, ProductDescription, ProductType, LaunchDate, AvailableQuantity) ' );
context.executeQueryAsync(Function.createDelegate(
this , this .productsReceived), Function.createDelegate( this , this .failed));
}
catch (e) {
alert(e);
}
}
function productsReceived() {
alert(
' got products ' );
prcessProducts(
this .productcollection);
}
function failed(sender, args) {
alert(
' failed. Message: ' + args.get_message());
}

 

三、删除

代码
var siteUrl = ' /sites/MySiteCollection ' ;

function deleteListItem() {

this .itemId = 2 ;

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

this .oListItem = oList.getItemById(itemId);
oListItem.deleteObject();
// Update

clientContext.executeQueryAsync(Function.createDelegate(
this , this .onQuerySucceeded), Function.createDelegate( this , this .onQueryFailed));
}
function onQuerySucceeded() {
alert(
' Item deleted: ' + itemId);
}
function onQueryFailed(sender, args) {
alert(
' Request failed. ' + args.get_message() + ' \n ' + args.get_stackTrace());
}

 

四、CAML查询 

function retrieveListItemsInclude(siteUrl) {

    var clientContext = new SP.ClientContext(siteUrl);

    var oList = clientContext.get_web().get_lists().getByTitle('Announcements');



    var camlQuery = new SP.CamlQuery();

    camlQuery.set_viewXml('<View><RowLimit>100</RowLimit></View>');

    this.collListItem = oList.getItems(camlQuery);



    clientContext.load(

        collListItem, 

        'Include(Id, DisplayName, HasUniqueRoleAssignments)'

    );

    clientContext.executeQueryAsync(

        Function.createDelegate(this, this.onQuerySucceeded), 

        Function.createDelegate(this, this.onQueryFailed)

    );

}



function onQuerySucceeded(sender, args) {

    var listItemInfo = '';

    var listItemEnumerator = collListItem.getEnumerator();

        

    while (listItemEnumerator.moveNext()) {

        var oListItem = listItemEnumerator.get_current();

        listItemInfo += '\nID: ' + oListItem.get_id() + 

            '\nDisplay name: ' + oListItem.get_displayName() + 

            '\nUnique role assignments: ' + 

            oListItem.get_hasUniqueRoleAssignments();

    }



    alert(listItemInfo.toString());

}



function onQueryFailed(sender, args) {

    alert('Request failed. ' + args.get_message() + 

        '\n' + args.get_stackTrace());

}
View Code

 

 

 

你可能感兴趣的:(ECMAScript)