SharePoint Client Object Model ( SP - COM )
Part - 2
[ Let's Code - Basic Coding Techniques in COM ].
I'll start with basic coding blocks and techniques which we use in ECMA scripts. but when you are in a practical environment you will need to know more coding styles and the best practicess which I'm gonna talk in Part 3 and onward.Some basics
First this Coding or Scripts lines are so similar to Server coding in SP 2010 so it's not hard to keep them in mind.
How to access a list and get Data from a list in Server code here is the sample:
using (SPSite site = new SPSite(topSiteUrl)) - Create SharePoint Site Object
{
using (SPWeb Web = site.OpenWeb()) - Create SharePoint Web Object based on site
{
string listUrl = Utilities.ConcatPath(topSiteUrl, "Lists/XXXinformation");
var XXXList = Web.GetList(listUrl); - Access list by URL (can be done using title)
SPQuery sp_Qry = new SPQuery(); - Create CAML Query object
sp_Qry.RowLimit = 20;
SPListItemCollection itemCol = XXXList.GetItems(sp_Qry); - Extract Data from the list
foreach (SPItem item in itemCol)
{ - Loop through the Item collection and get item data.
item["Title"].ToString()
new SPFieldUrlValue(item["XXXSiteURL"].ToString()).Url
}
}
}
First you need to load sp.js file which is the ECMAScript class library [Find the namespaces that are available from the SP.js] to do that you need to use following Function. [ Here youFunction is the function which include other coding.]
ExecuteOrDelayUntilScriptLoaded( youFunction , "sp.js");
in SharePoint 2013 you need to call this as,
SP.SOD.executeFunc('sp.js', 'SP.ClientContext', youFunction );
with-in the youFunction you can implement following scripts,
first of all you need to create a Context object which Similar to "Microsoft.SharePoint.SPContext"
var
context = new SP.ClientContext(Url); -Default Way
var context
= new SP.ClientContext(); - get root context
var
context = new SP.ClientContext.get_current(); - get Current context
var
web = context.get_web(); - Get Current Web (when you are in subsite it take tha subsite web object)
§[var web
= context.get_site().get_rootWeb();] - Get Root Web (even when you are in subsite it takes root web)
var
List = web.get_lists().getByTitle(‘Title of List Item'); - Note: you can access list only by it's title [i couldn't find any ]
Create a Caml query object as SPQuery as follows,
var
camlQuery
= new SP.CamlQuery();
camlQuery.set_viewXml(QueryLatest);
Note: this 'QueryLatest' is what you write you CMAL Query.
then defile a Class variable as var collListItem; then,
var QueryLatest =
'<View>' +
'<Query>' +
'<Where>' +
'<Eq><FieldRef Name=\'Disabled\' /><Value
Type=\'Boolean\'>0</Value></Eq>' +
'</Where>' +
'<OrderBy><FieldRef
Name=\'FooterOrder\' /></OrderBy>' +
'</Query>' +
'</View>';
then defile a Class variable as var collListItem; then,
this.collListItem = List.getItems(camlQuery);
context.load(collListItem);
context.executeQueryAsync(Function.createDelegate(this, this.onListDataLoadQuerySucceeded), Function.createDelegate(this,
this.onListDataLoadQueryFailed));
functions should be like this,
function onListDataLoadQuerySucceeded(sender, args) {
}
function onListDataLoadQueryFailed(sender, args) {
}
while (listItemEnumerator.moveNext())
{
}
All-together :
var ListName = 'Footer';
var Query =
'<View>' +
'<Query>' +
'<Where>' +
'<Eq><FieldRef Name=\'Disabled\' /><Value
Type=\'Boolean\'>0</Value></Eq>' +
'</Where>' +
'<OrderBy><FieldRef
Name=\'FooterOrder\' /></OrderBy>' +
'</Query>' +
'</View>';
$(function () {
ExecuteOrDelayUntilScriptLoaded(GetFooter,
"sp.js");
});
var ShowcorFooter = "";
var collListItem;
function GetFooter() {
var context = new SP.ClientContext();
var web = context.get_site().get_rootWeb();
var List = web.get_lists().getByTitle(ListName);
var camlQuery = new SP.CamlQuery();
camlQuery.set_viewXml(Query);
this.collListItem = List.getItems(fcamlQuery);
context.load(fcollListItem);
context.executeQueryAsync(
Function.createDelegate(this, this.onFooterListDataLoadQuerySucceeded),
Function.createDelegate(this, this.onFooterListDataLoadQueryFailed));
}
function onFooterListDataLoadQuerySucceeded(sender, args) {
var flistItemEnumerator = fcollListItem.getEnumerator();
if (this.fcollListItem.get_count()
> 0) {
while (flistItemEnumerator.moveNext()) {
var footerListItem =
flistItemEnumerator.get_current();
var link = footerListItem.get_item("Link").get_url());
var title = footerListItem.get_item("Title"));
}
}
}
function onFooterListDataLoadQueryFailed(sender, args) {
alert("Something
Wrong");
}
ok That's It ...
HAPPY CODING
No comments:
Post a Comment