Friday, August 12, 2011

Client Object Model – Part 7

We are going to continue exploring more ECMA script. If you have not gone through part 1 to part 6, I would recommend you reading them first and then continue exploring this part onwards.

1) Query List


<script type="text/javascript">

var currentcontext = null;
var currentweb = null;

ExecuteOrDelayUntilScriptLoaded(QueryListData, "sp.js");

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

currentweb = currentcontext.get_web();

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

var camlQuery = new SP.CamlQuery();

camlQuery.set_viewXml('<View><Query><Where><Eq><FieldRef Name="First_x0020_Name"/><Value Type="Text">Andre</Value></Eq></Where></Query><ViewFields><FieldRef Name="Title" /></ViewFields></View>');


this.listItems = this.list.getItems(camlQuery);


currentcontext.load(listItems);


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_item('Title');

alert(title);

}

}



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



2) Getting version and other info from list item. Also check out how to get lookup field value in case of created by and modified by.


<script type="text/javascript">

var currentcontext = null;
var currentweb = null;

ExecuteOrDelayUntilScriptLoaded(QueryListData, "sp.js");

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

currentweb = currentcontext.get_web();

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

var camlQuery = new SP.CamlQuery();

camlQuery.set_viewXml('<View><Query><Where><Eq><FieldRef Name="First_x0020_Name"/><Value Type="Text">Andre</Value></Eq></Where></Query></View>');


this.listItems = this.list.getItems(camlQuery);

currentcontext.load(listItems);


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_item('Title');

var vInformation = item.get_item('_UIVersionString');

var Created = item.get_item('Created');

var Modified = item.get_item('Modified');

var CreatedBy = item.get_item('Author').get_lookupValue();

var ModifiedBy = item.get_item('Editor').get_lookupValue();

var totalsctring = "Title:" + title + "\n" + "Version:" + vInformation + "\n" + "Created:" + Created +"\n" +
"Modified:" + Modified +"\n" +
"CreatedBy:" + CreatedBy +"\n" +
"ModifiedBy :" + ModifiedBy +"\n" ;


alert(totalsctring);

}
}

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




3) Iterate through Site Groups


<script type="text/javascript">

var currentcontext = null;
var currentweb = null;

ExecuteOrDelayUntilScriptLoaded(IteratethroughGroup, "sp.js");

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

currentweb = currentcontext.get_web();

this.groupCollection = currentweb.get_siteGroups();

currentcontext.load(this.groupCollection);

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

}


function ExecuteOnSuccess(sender, args) {

var listEnumerator = this.groupCollection.getEnumerator();
//iterate though all of the items
while (listEnumerator.moveNext()) {
var item = listEnumerator.get_current();


groupName = item.get_title();

alert(groupName);

}
}

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



4) Iterate through recycle bin

Remember, this is for the site collection recycle bin because we have taken site as a reference. If you want to take individual web recycle bin into consideration then use ge_web istead of get_site option. Rest everything need not to be changed.


<script type="text/javascript">


var currentcontext = null;
var currentsite = null;

ExecuteOrDelayUntilScriptLoaded(IteratethroughRecycleBin, "sp.js");

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

currentsite = currentcontext.get_site();

this.rbincoll = currentsite.get_recycleBin();

currentcontext.load(this.rbincoll);

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

}


function ExecuteOnSuccess(sender, args) {

if(this.rbincoll.get_count() > 0)
{
var listEnumerator = this.rbincoll.getEnumerator();
//iterate though all of the items
while (listEnumerator.moveNext()) {
var item = listEnumerator.get_current();

var title = item.get_title();
var id = item.get_id();

alert("ID: " + id+ "\nTitle:" + title);
}

}
else
{
alert('There is no item in recycle bin');
}
}

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



Keep reading Part 8 of this series.

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