SharePoint Client Object Model - Javascript
First note:
To work with Client Object model - Javascript, you have to add some js references in your file.
<script type="text/ecmascript" src="/_layouts/SP.Core.js" />
<script type="text/ecmascript" src="/_layouts/SP.Debug.js" />
<script type="text/ecmascript" src="/_layouts/SP.Runtime.Debug.js" />
To work with Client Object model - Javascript, you have to add some js references in your file.
<script type="text/ecmascript" src="/_layouts/SP.Core.js" />
<script type="text/ecmascript" src="/_layouts/SP.Debug.js" />
<script type="text/ecmascript" src="/_layouts/SP.Runtime.Debug.js" />
Get SharePoint Website information
-------------------------------------
-------------------------------------
var siteUrl = '/MySiteCollection/mysite ';
function getMyWebSite() {
var clientContext = new SP.ClientContext(siteUrl);
var clientContext = new SP.ClientContext(siteUrl);
this.WebsiteObj = clientContext.get_web();
clientContext.load(this.WebsiteObj);
clientContext.executeQueryAsync(Function.createDelegate(this, this.onGetSucceeded), Function.createDelegate(this, this.onGetFailed));
}
}
function onGetSucceeded(sender, args) {
alert('Site Title: ' + this.WebsiteObj.get_title() + ' Site Decription: ' + this.WebsiteObj.get_description());
}
alert('Site Title: ' + this.WebsiteObj.get_title() + ' Site Decription: ' + this.WebsiteObj.get_description());
}
function onGetFailed(sender, args) {
alert('Site Request failed. ' + args.get_message() + 'n' + args.get_stackTrace());
}
alert('Site Request failed. ' + args.get_message() + 'n' + args.get_stackTrace());
}
Get SharePoint List data
------------------------
------------------------
var siteUrl = '/MySiteCollection/mysite';
function getMySiteListData() {
var clientContext = new SP.ClientContext(siteUrl);
var empList = clientContext.get_web().get_lists().getByTitle('EmpInfo');
var empList = clientContext.get_web().get_lists().getByTitle('EmpInfo');
var camlQuery = new SP.CamlQuery();
camlQuery.set_viewXml('<View><Query><Where><Geq><FieldRef Name='Department'/>' +
'<Value Type='Text'>HR</Value></Geq></Where></Query><RowLimit>10</RowLimit></View>');
this.empListItems = empList.getItems(camlQuery);
camlQuery.set_viewXml('<View><Query><Where><Geq><FieldRef Name='Department'/>' +
'<Value Type='Text'>HR</Value></Geq></Where></Query><RowLimit>10</RowLimit></View>');
this.empListItems = empList.getItems(camlQuery);
clientContext.load(empListItems);
clientContext.executeQueryAsync(Function.createDelegate(this, this.onListDataSucceeded), Function.createDelegate(this, this.onListDataFailed));
}
function onListDataSucceeded(sender, args) {
var empInfo = '';
var listItemEnumerator = empListItems.getEnumerator();
while (listItemEnumerator.moveNext()) {
var oListItem = listItemEnumerator.get_current();
empInfo += 'nEmp ID: ' + oListItem.get_id() +
'nEmployee Name: ' + oListItem.get_item('Title');
}
var oListItem = listItemEnumerator.get_current();
empInfo += 'nEmp ID: ' + oListItem.get_id() +
'nEmployee Name: ' + oListItem.get_item('Title');
}
alert(empInfo.toString());
}
}
function onListDataFailed(sender, args) {
alert('List Data fetch failed. ' + args.get_message() + 'n' + args.get_stackTrace());
}
Add new List Item
--------------------
Create input fields for each input
}
Add new List Item
--------------------
Create input fields for each input
Ex:
<input type="textbox" id="supplierName"/>
// Make sure the SharePoint script file 'sp.js' is loaded before your
// code runs.
SP.SOD.executeFunc('sp.js', 'SP.ClientContext', newSupplier);
function newSupplier(siteUrl) {
var sName = $('#supplierName').val();
var sRegion = $('#supplierRegion').val();
var sProduct = $('#supplierProduct').val();
var sAcctMgr = $('#supplierAcctMgr').val();
<input type="textbox" id="supplierName"/>
// Make sure the SharePoint script file 'sp.js' is loaded before your
// code runs.
SP.SOD.executeFunc('sp.js', 'SP.ClientContext', newSupplier);
function newSupplier(siteUrl) {
var sName = $('#supplierName').val();
var sRegion = $('#supplierRegion').val();
var sProduct = $('#supplierProduct').val();
var sAcctMgr = $('#supplierAcctMgr').val();
var clientContext = new SP.ClientContext(siteUrl);
var oList = clientContext.get_web().get_lists().getByTitle('Suppliers');
var itemCreateInfo = new SP.ListItemCreationInformation();
var itemCreateInfo = new SP.ListItemCreationInformation();
this.oListItem = oList.addItem(itemCreateInfo);
oListItem.set_item('Title', sName);
oListItem.set_item('Region', sRegion);
oListItem.set_item('Product', sProduct);
oListItem.set_item('AccountManager', sAcctMgr);
oListItem.update();
oListItem.set_item('Title', sName);
oListItem.set_item('Region', sRegion);
oListItem.set_item('Product', sProduct);
oListItem.set_item('AccountManager', sAcctMgr);
oListItem.update();
clientContext.load(oListItem);
clientContext.executeQueryAsync(
Function.createDelegate(this, this.onQuerySucceeded),
Function.createDelegate(this, this.onQueryFailed)
);
Function.createDelegate(this, this.onQuerySucceeded),
Function.createDelegate(this, this.onQueryFailed)
);
Update Item of SharePoint list
-----------------------------
function UpdateListItem()
{
var ListName = "MyList";
var context = new SP.ClientContext.get_current(); // the current context is taken by default here
//you can also create a particular site context as follows
//var context = new SP.ClientContext('/Sites/site1');
-----------------------------
function UpdateListItem()
{
var ListName = "MyList";
var context = new SP.ClientContext.get_current(); // the current context is taken by default here
//you can also create a particular site context as follows
//var context = new SP.ClientContext('/Sites/site1');
var lstObject = context.get_web().get_lists().getByTitle(ListName);
this.lstObjectItem = lstObject.getItemById(1);
lstObjectItem.set_item('Title', 'This is updated item');
lstObjectItem.update();
lstObject.set_description("Updated description using ECMAScript");
lstObject.update();
context.executeQueryAsync(Function.createDelegate(this, this.onSuccess),
Function.createDelegate(this, this.onFailure));
}
this.lstObjectItem = lstObject.getItemById(1);
lstObjectItem.set_item('Title', 'This is updated item');
lstObjectItem.update();
lstObject.set_description("Updated description using ECMAScript");
lstObject.update();
context.executeQueryAsync(Function.createDelegate(this, this.onSuccess),
Function.createDelegate(this, this.onFailure));
}
function onSuccess() {
alert('Item udated');
}
alert('Item udated');
}
function onFailure(sender, args) {
alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}
alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}
Delete Item from SharePoint list
-----------------------------------
function DeleteListItem()
{
var ListName = "MyList";
var context = new SP.ClientContext.get_current(); // the current context is taken by default here
//you can also create a particular site context as follows
//var context = new SP.ClientContext('/Sites/site1');
-----------------------------------
function DeleteListItem()
{
var ListName = "MyList";
var context = new SP.ClientContext.get_current(); // the current context is taken by default here
//you can also create a particular site context as follows
//var context = new SP.ClientContext('/Sites/site1');
var lstObject = context.get_web().get_lists().getByTitle(ListName);
this.lstObjectItem = lstObject.getItemById(1);
lstObjectItem.deleteObject();
this.lstObjectItem = lstObject.getItemById(1);
lstObjectItem.deleteObject();
context.executeQueryAsync(Function.createDelegate(this, this.onSuccess),
Function.createDelegate(this, this.onFailure));
}
Function.createDelegate(this, this.onFailure));
}
function onSuccess() {
alert('Item Deleted');
}
alert('Item Deleted');
}
function onFailure(sender, args) {
alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}
alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}