Monday, August 8, 2011

Client Object Model – Part 5

We are going to continue with ECMA script examples and going to see how to perform
various operations sing ECMA script.

If you have not gone through Part 1 to Part 4, I would recommend reading them first before continue this.

Let’s expand our example and explore how we can create lists, list items, delete list etc. These operations are easy to perform. Once you get to know how to call list and list items, then it’s really easy to play around with them.

1) Create List


<script type="text/javascript">

var currentcontext = null;
var currentweb = null;

ExecuteOrDelayUntilScriptLoaded(CreateList, "sp.js");


function CreateList()
{
currentcontext = new SP.ClientContext.get_current();
currentweb = currentcontext .get_web();


var ListInfo = new SP.ListCreationInformation();
var ItemInfo = new SP.ListItemCreationInformation();

ListInfo.set_title('List Created From ECMA Script');
ListInfo.set_templateType(SP.ListTemplateType.genericList);

this.createdList = currentweb.get_lists().add(ListInfo);


currentcontext.load(createdList, 'Title','Id');

currentcontext.executeQueryAsync(Function.createDelegate(this, this.ExecuteOnSuccess),
Function.createDelegate(this, this.ExecuteOnFailure));

}

function ExecuteOnSuccess(sender, args) {
alert('list title:' + createdList .get_title() + '\n ID' + createdList .get_id());
}

function ExecuteOnFailure(sender, args) {
alert('request failed ' + args.get_message() + '\n' + args.get_stackTrace());
}

</script>



2) Delete List


<script type="text/javascript">

var currentcontext = null;
var currentweb = null;

ExecuteOrDelayUntilScriptLoaded(DeleteList, "sp.js");


function DeleteList()
{
currentcontext = new SP.ClientContext.get_current();
currentweb = currentcontext.get_web();

this.list = currentweb.get_lists().getByTitle('List Created From ECMA Script');
this.list.deleteObject();


currentcontext.executeQueryAsync(Function.createDelegate(this, this.ExecuteOnSuccess),
Function.createDelegate(this, this.ExecuteOnFailure));

}

function ExecuteOnSuccess(sender, args) {
alert('list deleted successfully');
}

function ExecuteOnFailure(sender, args) {
alert('list could not be deleted');
}

</script>




3) Update List


<script type="text/javascript">

var currentcontext = null;
var currentweb = null;

ExecuteOrDelayUntilScriptLoaded(UpdateList, "sp.js");


function UpdateList()
{
currentcontext = new SP.ClientContext.get_current();
currentweb = currentcontext.get_web();

this.list = currentweb.get_lists().getByTitle('List Created From ECMA Script');
this.list.set_description('Description set from ECMA script');

list.update();
currentcontext.load(list, 'Description');

currentcontext.executeQueryAsync(Function.createDelegate(this, this.ExecuteOnSuccess),
Function.createDelegate(this, this.ExecuteOnFailure));

}

function ExecuteOnSuccess(sender, args) {
alert('list description :' + this.list.get_description());
}

function ExecuteOnFailure(sender, args) {
alert('Description cannot be set');
}

</script>




4) Create List Item


<script type="text/javascript">

var currentcontext = null;
var currentweb = null;

ExecuteOrDelayUntilScriptLoaded(CreateListItem, "sp.js");


function CreateListItem ()
{
currentcontext = new SP.ClientContext.get_current();
currentweb = currentcontext.get_web();

this.list = currentweb.get_lists().getByTitle('List Created From ECMA Script');
var ItemInfo = new SP.ListItemCreationInformation();
this.ListItem = list.addItem(ItemInfo);


ListItem.set_item('Title', 'Item created from ECMA script');

ListItem.update();

currentcontext.load(ListItem);

currentcontext.executeQueryAsync(Function.createDelegate(this, this.ExecuteOnSuccess),
Function.createDelegate(this, this.ExecuteOnFailure));

}

function ExecuteOnSuccess(sender, args) {
alert('list item id:' + this.ListItem.get_id());
}

function ExecuteOnFailure(sender, args) {
alert('List Item cannot be created');
}

</script>




5) Update List Item


<script type="text/javascript">


var currentcontext = null;
var currentweb = null;

ExecuteOrDelayUntilScriptLoaded(UpdateListItem, "sp.js");

function UpdateListItem()
{
currentcontext = new SP.ClientContext.get_current();
currentweb = currentcontext.get_web();

this.list = currentweb.get_lists().getByTitle('List Created From ECMA Script');

this.ListItem = list.getItemById(1);
ListItem.set_item('Title', 'Item ID 1 update using ECMA script');
ListItem.update();


currentcontext.executeQueryAsync(Function.createDelegate(this, this.ExecuteOnSuccess),
Function.createDelegate(this, this.ExecuteOnFailure));

}

function ExecuteOnSuccess(sender, args) {
alert('list item updated successfully');
}

function ExecuteOnFailure(sender, args) {
alert('List Item cannot be updated');
}

</script>



6) Delete List Item


<script type="text/javascript">


var currentcontext = null;
var currentweb = null;

ExecuteOrDelayUntilScriptLoaded(DeleteListItem, "sp.js");

function DeleteListItem()
{
currentcontext = new SP.ClientContext.get_current();
currentweb = currentcontext.get_web();

this.list = currentweb.get_lists().getByTitle('List Created From ECMA Script');
this.ListItem = list.getItemById(1);
ListItem.deleteObject();


currentcontext.executeQueryAsync(Function.createDelegate(this, this.ExecuteOnSuccess),
Function.createDelegate(this, this.ExecuteOnFailure));

}

function ExecuteOnSuccess(sender, args) {
alert('list item deleted successfully');
}

function ExecuteOnFailure(sender, args) {
alert('List Item cannot be deleted');
}

</script>



There are more examples to come in series. Read Part-6

No comments:




Share your SharePoint Experiences with us...
As good as the SharePointKings is, we want to make it even better. One of our most valuable sources of input for our Blog Posts comes from ever enthusiastic Visitors/Readers. We welcome every Visitor/Reader to contribute their experiences with SharePoint. It may be in the form of a code stub, snippet, any tips and trick or any crazy thing you have tried with SharePoint.
Send your Articles to sharepointkings@gmail.com with your Profile Summary. We will Post them. The idea is to act as a bridge between you Readers!!!

If anyone would like to have their advertisement posted on this blog, please send us the requirement details to sharepointkings@gmail.com