Wednesday, August 10, 2011

Client Object Model – Part 6

If you have not gone through Part 1 to Part 5, I would recommend you reading them first and then continue reading from here.

We are going to see more examples of ECMA script here.


1) Create Site

<script type="text/javascript">


var currentcontext = null;
var currentweb = null;

ExecuteOrDelayUntilScriptLoaded(CreateSite, "sp.js");

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

var webCreateInfo = new SP.WebCreationInformation();
webCreateInfo.set_description("This site created from ECMA script");
webCreateInfo.set_language(1033);
webCreateInfo.set_title("ECMA Script created site");
webCreateInfo.set_url("ECMAScriptSite");
webCreateInfo.set_useSamePermissionsAsParentSite(true);
webCreateInfo.set_webTemplate("STS#0");

this.NewWebsite = this.currentweb.get_webs().add(webCreateInfo);

currentcontext.load(this.NewWebsite, 'ServerRelativeUrl', 'Created');


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

}

function ExecuteOnSuccess(sender, args) {
alert("Web site url : " + this.NewWebsite.get_serverRelativeUrl());
}

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

</script>







2) Iterate through sub sites
<script type="text/javascript">

var currentcontext = null;
var currentweb = null;

ExecuteOrDelayUntilScriptLoaded(EnumerateThroughSite, "sp.js");

function EnumerateThroughSite()
{
currentcontext = new SP.ClientContext.get_current();

currentweb = currentcontext.get_web();

this.subsites = currentweb.get_webs();

//this.sitecoll = currentcontext.get_site(); //to get top level site - just for information

currentcontext.load(this.subsites);

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

}

function ExecuteOnSuccess(sender, args) {
var subsites = '';

var groupEnumerator = this.subsites.getEnumerator();
while (groupEnumerator.moveNext()) {
var Site = groupEnumerator.get_current();
subsites += '\nID: ' + Site.get_id() +
'\nTitle: ' + Site.get_title();

}
alert(subsites);
}

function ExecuteOnFailure(sender, args) {
alert("Cannot enumerate");
}
</script>




3) Load List properties
<script type="text/javascript">


var currentcontext = null;
var currentweb = null;

ExecuteOrDelayUntilScriptLoaded(LoadListProp, "sp.js");

function LoadListProp()
{
currentcontext = new SP.ClientContext.get_current();

currentweb = currentcontext.get_web();

this.list = currentweb.get_lists().getByTitle("Employees");

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

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

}


function ExecuteOnSuccess(sender, args) {

alert('List title : ' + this.list.get_title() + '; List ID : '+ this.list.get_id());

//alert('success');

}



function ExecuteOnFailure(sender, args) {
alert("Cannot enumerate");
}
</script>





4) Load List Data

<script type="text/javascript">


var currentcontext = null;
var currentweb = null;

ExecuteOrDelayUntilScriptLoaded(LoadListData, "sp.js");

function LoadListData()
{
currentcontext = new SP.ClientContext.get_current();

currentweb = currentcontext.get_web();

this.list = currentweb.get_lists().getByTitle("Employees");

var camlQuery = new SP.CamlQuery();
var query = '<View><RowLimit>3</RowLimit></View>';
camlQuery.set_viewXml(query);

this.listItems = list.getItems(camlQuery);
currentcontext.load(listItems, 'Include(DisplayName,Id)');


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

}


function ExecuteOnSuccess(sender, args) {

var listEnumerator = this.listItems.getEnumerator();
//iterate though all of the items
while (listEnumerator.moveNext()) {
var item = listEnumerator.get_current();
var title = item.get_displayName();
var id = item.get_id();
alert("Item title : " + title + "; Item ID : "+ id);
}

}



function ExecuteOnFailure(sender, args) {
alert("Cannot load list data");
}
</script>



Keep reading Part 7 for further exploring ECMA script.

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