Tuesday, November 5, 2013

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
   }
}
}

ECMA Scripts are almost same. 

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

through Context you can get web object which similar to SpWeb

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)

Then you need to create a List object as SPList
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 aSPQuery as follows,
var camlQuery = new SP.CamlQuery();
camlQuery.set_viewXml(QueryLatest);
Note: this 'QueryLatest' is what you write you CMAL Query.
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);

This is how you wrap the ECMA object which filled with required details and bind with context then send it back to the WCF service (client.svc)

context.load(collListItem);

context.executeQueryAsync(Function.createDelegate(this, this.onListDataLoadQuerySucceeded), Function.createDelegate(this,
this.onListDataLoadQueryFailed));

you need to have two functions as you code with Ajax objects one is for Success and one is for fail. and it's asynchronous call, as it involve with web service.
 functions should be like this,
 
function onListDataLoadQuerySucceeded(sender, args) {
      
}

function onListDataLoadQueryFailed(sender, args) {

}

If Service deliver the results successfully onListDataLoadQuerySucceeded will fire. and you can get Item count by using, this.collListItem.get_count() and if you need to iterate through results you need to have a Enumerator object, var listItemEnumerator = collListItem.getEnumerator(); then you can iterate as like this,
          
while (listItemEnumerator.moveNext()) {
      
              }
get current item in the loop as, var listItem = listItemEnumerator.get_current();Then access List Item columns as, listItem.get_item("Title").

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");
}

In Next Post I'll discuss good techniques and best practices through a web Picture rotate custom web part.

ok That's It ... 
HAPPY CODING

No comments:

Post a Comment