Sharepoint 2010: Use Ecmascript To Manipulate (Add/Delete/Update/Get) List Items
Sharepoint 2010: Use Ecmascript To Manipulate (Add/Delete/Update/Get) List Items
Sharepoint 2010: Use Ecmascript To Manipulate (Add/Delete/Update/Get) List Items
2.
3. 4. 5. 6.
The addProduct method below takes few arguments that represents the product lists fields.
function addProduct(productName, productDesc, productLaunchDate, productAvailQty, productType) { try { var context = new SP.ClientContext.get_current(); var web = context.get_web(); var list = web.get_lists().getByTitle('product'); var listItemCreationInfo = new SP.ListItemCreationInformation(); var newItem = list.addItem(listItemCreationInfo); newItem.set_item('Title', productName); newItem.set_item('ProductName', productName); newItem.set_item('ProductDescription', productDesc); newItem.set_item('LaunchDate', productLaunchDate); newItem.set_item('AvailableQuantity', productAvailQty); newItem.set_item('ProductType', productType); newItem.update(); context.executeQueryAsync(Function.createDelegate(this, this.success), Function.createDelegate(this, this.failed)); } catch (e) {
alert('error:' + e.Message); } }
If you look a bit closer in the above code snippet you can find that ClientContext.executeQueryAsync takes two function delegates. The first one will be invoked when the ECMAScript get executed successfully. The second one will be invoked otherwise. The two methods are defined below:
function deleteProduct(productId) { var context = new SP.ClientContext.get_current(); var web = context.get_web(); var list = web.get_lists().getByTitle('product'); var itemToDelete = list.getItemById(productId); itemToDelete.deleteObject(); context.executeQueryAsync(Function.createDelegate(this, this.success), Function.createDelegate(this, this.failed)); }
To delete an object in Client Object Model you need to invoke the deleteObject method of that object.
Get Item By Id
To get an item using ECMAScript, you need to share a common variable between the method that execute the ECMAScript (getProductById method in the following code snippet) and callback method (productReceived, failed in the snippet below). Only for this reason I have defined a variable product in the first line of the code snippet below.
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); } } function productReceived() { alert('got product'); gotProduct(this.product); } function failed(sender, args) { alert('failed. Message:' + args.get_message()); }
In the code snippet above, the Context.Load method has taken the item to load (product) as the first parameter. And a comma separated list of columns to load for this item are passed then to the load method. If you want to load all properties of the item (which is not recommended) you can just call the context.load method with only first parameter.
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()); }
function updateProduct(productid, productName, productDesc, productLaunchDate, productAvailQty, productType) { var context = new SP.ClientContext.get_current(); var web = context.get_web(); var list = web.get_lists().getByTitle('product'); var product = list.getItemById(productid); product.set_item('ProductName', productName); product.set_item('ProductDescription', productDesc); product.set_item('ProductType', productType); product.set_item('LaunchDate', productLaunchDate); product.set_item('AvailableQuantity', productAvailQty); product.update(); context.executeQueryAsync(Function.createDelegate(this, this.success), Function.createDelegate(this, this.failed)); }
Points to Remember
1. FormDigest Control: If you write code that modifies data on server, include a FormDigest control to create a digest for security validation of the page. The formdigest control can be added in master page and then all content pages will get it automatically. <SharePoint:FormDigest runat="server" /> 2. Get and Set Property: For the scrip used, when you need to get a property, you need to add get_ as prefix. For example the web has the property title. So if you want to get the title property of web then you need to use the syntax web.get_title(). Similary, if you want to set the title value then you need to invoke web.set_title(title). 2. ClietContext.Load: ClientContexts load method is used to load object. The first parameter to the load method is the object to load. The second parameter vary depending on whether the first parameter is a single object or collection.
If the first parameter is a single object then the following syntax is used to load properties:
context.load(objectToLoad,property1,property2,.,propertyN) If the first parameter is a collection then the following syntax is used
context.load(objectCollectionToLoad,Include(property1, property2,.,propertyN))
Posted by SOHEL RANA at 11:47 AM Labels: Client OM, SharePoint 2010