SharePoint CAML Query and Scope attributes

Below article is above understanding scope attribute in CAML query. Suppose you have a document library where documents are stored with some folder structure.

Understanding Scope and its possible values

Default  – No specifying scope attributes, it will only returns all the files under root folder of document library. It won’t go return any files under a folder though applicable based on where condition

Recursive – By specifying scope as Recursive, it will returns files under document library including files under sub folders but not at second  level folder(folder inside folder).

FilesOnly – By specifying scope as FilesOnly, it will returns files only and exclude folders.

RecursiveAll – By specifying scope as RecursiveAll, it will returns all the files under document library including all the folder and sub folder inside folder.

Below are couple of scenarios to understand the same.

Scenario 1 – Retrieve file under specific folder, here folder path is “/folder/subfolder1” is relative path. Please note here example is based on Managed CSOM

List DocumentsList = clientContext.Web.Lists.GetByTitle("SharedDocuments");
CamlQuery camlQuery = new CamlQuery();
camlQuery = new CamlQuery();
camlQuery.ViewXml = @"<View Scope='RecursiveAll'>
                        <Query>
                            <Where>
                                <eq>
                                    <FieldRef Name='FileDirRef'/>
                                    <Value Type='Text'>
                                        /folder1/subfolder1
                                    </Value>
                                </eq>
                            </Where>
                        </Query>
                        <RowLimit Paged='TRUE'> 30 </RowLimit>
                    </View>";
ListItemCollection listItems = DocumentsList.GetItems (camlQuery);
clientContext.Load(listItems);
clientContext.ExecuteQuery();

Scenario 2 – Find all zip files within a document library, please note here example is based on JSOM.

var files = [];
var clientContext = new SP.ClientContext.get_current();
var oList = clientContext.get_web().get_lists().getByTitle('SharedDocuments');    
var camlQuery = new SP.CamlQuery();
camlQuery.set_viewXml("<View Scope='RecursiveAll'><Query><Where>" + 
                "" +
                "<Contains><FieldRef Name='FileLeafRef' /><Value Type='Text'>.zip</Value></Contains>" + 
                "</Where></Query></View>");
this.collListItem = oList.getItems(camlQuery);
clientContext.load(collListItem);
clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));        

function onQuerySucceeded(sender, args) {
    var listItemEnumerator = collListItem.getEnumerator();
    while (listItemEnumerator.moveNext()) {
        var oListItem = listItemEnumerator.get_current();  
        files.push(oListItem.get_item("FileLeafRef"));
    }    
    console.log(files);
}

function onQueryFailed(sender, args) {
    //Handle Error
}

Hope this  helps…Happy Coding..!!!

How to Test Your Local Web Sites on Your Mobile Devices Using XAMPP

Undoubtedly, every one of us would agree that Smart phone and new mobile devices have changed the world we live in. Everyone is so much dependent on smart phone that it has become very much necessary for the web site designers to make sure that the website being developed works well on
mobile devices also.
This smart phones and tablets have changed the development methodology or approach towards user experience and design. It won’t be incorrect to say that website designers and developers have yet not found a profound solution for challenge to keep same look and feel across different browsers
(IE, Firefox, chrome, safari etc…). And here comes a new one, support your website on various mobile devices and tablets at different resolutions.

To meet this challenge we have two design methods “Responsive web design” and “Adaptive”. Also it does not matter which one you are using until and unless the user is able to view website easily and effectively. But one of the problem designers/developers face is how to test their web application on actual devices during development. All web developers today should be testing their websites on mobile devices. I don’t mean simulators or emulators; I mean real-life devices that you hold in your hand. Simulator does not give us actual results because it is still running on desktop system.

Wouldn’t it be nice to test your local website on local computer from actual mobile devices? Let us see how we can do that.

Perquisites/Assumptions
1. It is being assumed that XAMP/WMAP or MAMP is installed in you system and you are doing local development using any of the Apache, MySQL and PHP webserver.
2. Your local computer and your mobile device is connected to same network via WIFI or any other connection. (Both desktop and mobile has to be on same network).
Step 1 – Go to XMAPP control panel and make sure you apache and MySQL is working. Also make
sure your website is running.

Step 2
Find your IP address of local computer.
Run CMD as administrator
And on CMD screen type ipconfig and the screen will appear with text

Step 3
Browse you site with URL on your local browser, this is to make sure site is working with IP address also.

Suppose you website is located at localhost/demosite/
It should be brows able with http://192.168.1.4/demosite.
Note – Make sure you setup BASE URL with IP ADDRESS
Step 4
Point your iPad or iPhone web browser to your IP address
You now have your IP address. It may look something like 192.168.1.4. Now, for the moment of truth, open a browser on your device at try browsing to the  directory using the IP address. In my case, I entered http://192.168.1.4/demosite. You’re now testing a website on your
computer from a remote device like an iPad.

That was easy, now you can do test and fix responsive mobile specific bugs easily before moving site to internet.

Hope this will help. Happy coding..!!!

Asp.Net Page Postback from Javascript

If you need to do page postback from javascript, this one is for you…
Today I came across situation where we were using Jquery calendar but we needed postback as soon as date is selected by user.

Step 1- Bind Javascript function to onselect event of Jquery calendar

$(“#mydatepicker”).datepicker({
onSelect: function(dateText, inst) {
_doPostBack(“buttonidoranyvalue”,””);
}
});

Above will make sure page is post back as soon as date is selected by user.
first parameter is eventtarget, second paramerter is eventargument.
Same can be used in any java scripts functions

Step 2 – Check Event Target in Request.Forms parameters and do whatever you want man  like below

if(Request.Form.Params.Get(“_EVENTTARGET”) == “buttonidoranyvalue”) {
//do whatever you want 
}

Hope this helps…Happy Coding!!!!

Asp.net Postback Mechanism(doPostBack function)

Let us understand how asp.net postback works

What is __doPostBack function in javascript and what it does? In asp.net all the web server controls except Button and ImageButton (discussed at the end) use javascript __doPostBack function to trigger postback. This behavior is known as asp.net postback mechanism.

To better understand this function lets go step by step. Lets take any web server control except Button and ImageButton on a sample aspx page.

///ASPX

<asp:LinkButton ID="LinkButton1" runat="server" OnClick="LinkButton1_Click" Text="LinkButton" />

///Code behind

protected void LinkButton1_Click(object sender, EventArgs e) { }

Run the page in browser and view it source.

///JavaScript

function __doPostBack(eventTarget, eventArgument) {

    if (!theForm.onsubmit || (theForm.onsubmit() != false)) {

        theForm.__EVENTTARGET.value = eventTarget;

        theForm.__EVENTARGUMENT.value = eventArgument;

        theForm.submit();

    }

}

///HTML

<input type="hidden" name="__EVENTTARGET" id="__EVENTTARGET" value="" />

<input type="hidden" name="__EVENTARGUMENT" id="__EVENTARGUMENT" value="" />



<a id="LinkButton1" href="javascript:__doPostBack('LinkButton1','')">LinkButton</a>

As you can see _doPostBack function takes two arguments viz. eventTarget and eventArgument. Moreover two hidden fields, “__EVENTTARGET” and “__EVENTARGUMENT,” are automatically declared which are used to store values of ‘eventTarget’ and ‘eventArgument’ respectively.

When LinkButton1 is clicked, __doPostBack function in javascript is called passing it respective arguments, in this case eventTarget is LinkButton1 and eventArgument is empty, these values are stored in respective hidden fields i.e. they are added to form parameter collection and finally the form is submitted. On server, ASP.NET retrieves and uses the values from form collection and understands which control caused the postback.

Ok if you have some confusion on these values are retrieved, lets do it ourselves to clear the things out. Take another control on web form, say another link button ‘LinkButton2’.

Insert break point on Page_Load event and hit F5. Click on LinkButton1, check the values that are retrieved in Page_Load event.

As you see __EVENTTARGET key returns the value of the control that caused postback, here LinkButton1. Now click on LinkButton2 and check the value you get, it will be “LinkButton2”. This is how the asp.net postback mechanism works through use of javascript __doPostBack function.

Button and ImageButton

These are the only two controls which cause postback in asp.net. Rest (as explained above) use javascript __doPostBack function and post the form (ASP.NET postback mechanism). In other words these two controls use client browser’s submit behavior and hence they do not use __doPostBack function.

However you can make Button to use asp.net postback mechanism by making use of Button.UseSubmitBehavior property (this property is not available for ImageButton control). UseSubmitBehavior property is of type bool and decides whether Button should use client browser’s submit behavior (Default. value = true) or asp.net postback mechanism (value = false).

Lets take two Button control on page:

<asp:Button ID="Button1" runat="server" Text="Uses browser submit behavior" UseSubmitBehavior="true" />

<asp:Button ID="Button2" runat="server" Text="Uses asp.net postback mechanism" UseSubmitBehavior="false" />

 

Note that you don’t have to specify UseSubmitBehavior=”true” explicitly as it is the default value/behavior. I have added it just for the sake of understanding. Now run this page and view its source in browser, you will these buttons rendered as follows:

<input type="submit" name="Button1" value="Uses browser submit behavior" id="Button1" />

<input type="button" name="Button2" value="Uses asp.net postback mechanism" onclick="javascript:__doPostBack('Button2','')" id="Button2" />

 

Button2 for which we set UseSubmitBehavior property to false, will now use _doPostBack function to submit the form.

Hope this helps…Happy Coding!!!!

How to add JS or css file reference to all pages with custom action ?

Scenario

  • Suppose you wanted to add JS file or css file reference to all the pages in your site collection via javascript but you are not allowed to edit master page.
  • Suppose you wanted to do branding like displaying message or modifying css to all the site collections then it would be difficult to go and edit css of each site collection. To overcome this we can write a utility which will iterate through each target site and add js and css reference.

Solution

  • Custom action functionality to add ScriptLink can be used to add JS and CSS file references to all the pages without using master page
  • We can do do this JSOM or managed CSOM based on our requirement

Using JSOM – You can add below function in any JS file or content editor webpart and call GetCustomActionsOfWeb from click event of button.

If you are calling on page load or something, make sure you wait till sp.js is loaded and then call your function.

ExecuteOrDelayUntilScriptLoaded(GetCustomActionsOfWeb, 'sp.js');



function GetCustomActionsOfWeb(){

clientContext = new SP.ClientContext.get_current();

var hostWeb = clientContext.get_web();

hostWebCustomActions = hostWeb.get_userCustomActions();

clientContext.load(hostWeb);

clientContext.load(hostWebCustomActions);

clientContext.executeQueryAsync(AddNewCustomAction, ErrorInGettingCustomActions);

}

function AddNewCustomAction(){

var customAction = hostWebCustomActions.add();

customAction.set_location('ScriptLink');

customAction.set_sequence(10001);

customAction.set_scriptSrc('~SiteCollection/Site Assets/jquery-1.11.1.min.js');

customAction.update();

clientContext.load(hostWebCustomActions);

clientContext.executeQueryAsync(onSuccess, OnFail);

}

function ErrorInGettingCustomActions(){

alert('Error in getting custom actions...');

}

function onSuccess(){

alert('Added Script Link Successfully.')

});

});

});

});

}

function OnFail(){

alert('Enable to add script link via custom action');

}

 

Using CSOM

private  void AddCustomAction(string SCURL)

       {

           using (ClientContext context = new ClientContext(SCURL))

           {

               // Check URL is root site or web

               Web oWeb = context.Web;



               UserCustomActionCollection collCustomActions = oWeb.UserCustomActions;

               //var serverURL =  oWeb.;

               context.Load(oWeb, webSite => webSite.UserCustomActions);

               context.ExecuteQuery();



               //******** Delete file script link reference if already exsits in page ********

               int actionCount = oWeb.UserCustomActions.Count;

               for (int i = actionCount - 1; i >= 0; i--)

               {

                   // if (oWeb.UserCustomActions[i].ScriptSrc != null && (oWeb.UserCustomActions[i].ScriptSrc.Contains(Constants.ScriptFileNames.JqueryRef) || oWeb.UserCustomActions[i].ScriptSrc.Contains(Constants.ScriptFileNames.HeaderFile) || oWeb.UserCustomActions[i].ScriptSrc.Contains(Constants.ScriptFileNames.StickyFooter)))

                   if (oWeb.UserCustomActions[i].ScriptSrc != null && (oWeb.UserCustomActions[i].ScriptSrc.Contains(Constants.ScriptFileNames.HeaderFile) || oWeb.UserCustomActions[i].ScriptSrc.Contains(Constants.ScriptFileNames.StickyFooter)))

                   {

                       Console.WriteLine(oWeb.UserCustomActions[i].Title + " Found");

                       oWeb.UserCustomActions[i].DeleteObject();

                       //break;

                   }

               }



context.ExecuteQuery();



                   UserCustomAction oUserCustomAction = collCustomActions.Add()



                   oUserCustomAction = collCustomActions.Add();

                   oUserCustomAction.Location = "ScriptLink";

                   oUserCustomAction.Sequence = Constants.sequenceCounter + 2;

                   oUserCustomAction.Title = "Custom Branding";

                   oUserCustomAction.Description = "Custom branding Description.";

                   oUserCustomAction.ScriptSrc = SCURL + "/Site Assets/" + "branding.js"; // thi sis custom js file

                   oUserCustomAction.Update();



                   

                   context.ExecuteQuery();

               }

           }

Hope this helps…Happy coding..!!!

How to get data from SharePoint List when threshold value is crossed ?

Scenario

  • Suppose you have List Employees which has more than 100000 rows(1 lakh)
  • Threshold limit set by administrator is 5000(default).
  • Employees are categorized in 3 department so we have one column Department which hold employee’s department. Each department has around 33000 employees.
  • Department column is indexed.

Requirement

  • Create a page which displays Employees in Jquery datatable(custom view) by calling GetListItems using spservices.js
  • Page will have a dropdown  for department which user can select and data will be filtered accordingly.
  • All the data needs to be displayed at once.

Issue

  • As each department has more than 33000 items, CAML query will fail with Threshold limit error.

Alternate solution approach.

  • Get the data in batch by querying multiple times. Though this is not effective but still it might be required when you need to display all data at once on filter.

Logic

  • Get data in batches by using SharePoint CAML query paging technique and specifying row limit.
  • We will get data in batch of 4999 rows at a time and merge data each time till all the rows are returned.
  • Same Caml query will be used with CAMLQueryOptions and sending ListItemCollectionPositionNext
//Glboal variables in your js file which will be used in below functions.

var isThreshold = false;

var dtEmployees;

var tempEmployeeData;



function GetDataFromList(strlistName)

{

var queryString = "";

queryString = "<Query>"+

"<Where>"+

"<Eq><FieldRef Name='Company' /><Value Type='Text'><![CDATA[" + "XYZ" + "]]></Value></Eq>" +

"</Where>"+

/*"<OrderBy>"+

"<FieldRef Name='Modified' Ascending='TRUE'/>"+

"</OrderBy>"+*/

"</Query>";

$().SPServices({

operation: "GetListItems",

async: false,

listName: strlistName,

CAMLRowLimit: 4999,

CAMLQueryOptions: "<QueryOptions><Paging ListItemCollectionPositionNext='"+position+"' /></QueryOptions>",

CAMLQuery: queryString,  

CAMLViewFields: "<ViewFields>" +

"<FieldRef Name='Title'/><FieldRef Name='EmployeeName' /><FieldRef Name='Department' />" +

"</ViewFields>",

completefunc: function (xData, Status) {

if(Status.toLowerCase() == "success")

{

// below xml node will return us next position, save it for next query

position = $(xData.responseXML).SPFilterNode("rs:data").attr("ListItemCollectionPositionNext");

//alert(position);

dtEmployees = $(xData.responseXML).SPFilterNode("z:row").SPXmlToJson({

mapping: {

ows_ID: {mappedName: "ID", objectType: "Counter"},

ows_Title: {mappedName: "Title", objectType: "Text"},

ows_EmployeeName: {mappedName: "EmployeeName", objectType: "Text"},

ows_Department: {mappedName: "Department", objectType: "Text"}

},

});

// Threshold exceed.. loop and fetch more than 5000 records.

if(position != undefined && position != "undefined" && position != "")

{

position = position.replace("&","&amp;");

if(tempEmployeeData == undefined || tempEmployeeData == "undefined" || tempEmployeeData == "")

{

tempEmployeeData = dtEmployees;

}

else

{

tempEmployeeData = tempEmployeeData.concat(dtEmployees);

}

isThreshold = true;

GetDataFromList("EmployeesInfo"); // calling same function again for further iteration.

}

else

{

if(tempEmployeeData != undefined && tempEmployeeData != "undefined" && tempEmployeeData != "")

{

tempEmployeeData = tempEmployeeData.concat(dtEmployees);

dtEmployees = tempEmployeeData;

tempEmployeeData = "";

}

isThreshold = false;

ProcessData(dtEmployees); // this can be your custom function which will be use final dtEmployees json object to processing.

}

}

else{

alert("Error ---\n" + $(xData.responseXML).find("errorstring").text());

}

}

});

}

 

Hope this helps…Happy Coding..!!!

What is Sharepoint ?

As defined by Wikipedia

Microsoft SharePoint is a Web application platform developed by Microsoft. First launched in 2001,[3] SharePoint has historically been associated with intranet, content management and document management, but recent versions have significantly broader capabilities.

As defined by Microsoft

Organizations use SharePoint to create websites. You can use it as a secure place to store, organize, share, and access information from almost any device. All you need is a web browser, such as Internet Explorer, Chrome, or Firefox.

Other definitions

At its most basic definition SharePoint is a web application from Microsoft that enables organizations to work more efficiently by letting users share documents, data and information. There are multiple SharePoint products for organizations to choose from, each offering a unique set of benefits.

In Simple terms
SharePoint is a:

  • Document management server
  • Web content management server
  • Portal solution
  • Search engine
  • List-based repository
  • Collaboration site
  • Replacement for file shares
  • etc etc…

…to summarize in one sentence what SharePoint is I would say:
Sharepoint is Microsoft’s Web OS.

Finally as explained by MSDN

The following is a selection of some of the most important things that SharePoint is:

  • A portal server with delegated administration. SharePoint enables information workers (IW) who have no knowledge of website design or website administration to create, almost instantly, attractive and functioning websites. This relieves information technology (IT) departments from the burden of creating and administering the sites, and it empowers the IWs to create their own sites for teams, blogs, wikis, and other purposes.
  • A groupware application kit. SharePoint provides a platform on which IWs can create collaboration solutions that include document libraries and workspaces, workflows, wikis, blogs, and team-oriented lists, such as Events, Announcements, and Tasks. Microsoft SharePoint Workspace (formerly Microsoft Office Groove 2007) provides an offline experience for these collaboration solutions.
  • A workflow host. Business processes can be systematized and modeled with workflows that are triggered by associated events; for example, the addition of a document to a document library.
  • A content management application. SharePoint Server Enterprise Content Management (ECM) features include document management, records management, and web content management.
  • A Business Intelligence (BI) application kit. The Microsoft Business Connectivity Services (BCS) features of SharePoint enable data from non-SharePoint sources, such as a SAP installation or Oracle database, to be accessed (read/write) just as if it were an ordinary SharePoint list.
  • The operating system of an intranet. SharePoint can provide for an intranet many of the functions that an operating system provides for a computer, including storing and copying files, hosting services, starting applications, and securing data. (This is not to imply that SharePoint can only be used on an intranet. SharePoint can also host extranet and Internet-facing solutions.)
  • A host for services. SharePoint deployments make data available through a client object model, the REST-based Windows Communication Foundation (WCF) Data Services (formerly ADO.NET Data Services), and many out-of-the-box ASMX web services. In addition, the SharePoint Service Application Framework provides a platform that enables developers to build scalable middle-tier services that can provide data or processing resources to other SharePoint features.
  • A data store. SharePoint stores data as multicolumn lists in a Microsoft SQL Server database. You can query the data by using LINQ and also using Collaborative Application Markup Language (CAML). The data can be mirrored, backed up, restored, and, depending on the edition of SQL Server being used, you may be able to take snapshots of the data store.
  • A data and processing layer for multiple user interfaces (UIs). Besides its native UI of webpages (including special versions for mobile devices), which can contain ECMAScript (JavaScript, JScript), SharePoint also supports access from Microsoft Silverlight applications and the Microsoft SharePoint Workspace client application. With the SharePoint client object model, you can access SharePoint using Windows Presentation Foundation (WPF), Windows Forms, or any other managed code application

Hope this helps…Happy Coding!!!!

Why SharePoint is popular ?

File Sharing SharePoint originally became popular because it was an easy way to share documents on the web. Many organizations that adopted SharePoint in the 2003 versions capitalized on the ability to upload documents to Document Libaries and share those documents with others.

Company Extranets One great example of this web based sharing, is a company extranet where users are not all in one location or authentication domain. Using form based authentication, accounts can be created for people across physical and company boundaries. By allowing one place for shared documents around a task rather than a corporate entity, SharePoint goes way beyond the common file share.
Content Management There are plenty of other Content Management Systems, but MOSS incorporated the functionality of the previously name Microsoft Content Management System which itself often cost more than MOSS alone. Check out this large list of public websites and blogs hosted on SharePoint And that is not all of them.
Search Search is greatly improved in SharePoint technologies. Search results are security trimmed, relevant and performant unlike the previous version.
Other reason
The OOB features are 1/10th of the whole story. Sharepoint exposes a comprehensive .Net object model that lets you customize the thing to your hearts content. People are coding amazing things with Sharepoint. With the object model, you can build and customize sites via code, in response to external events. You can write custom “web-parts” (controls hosted on special pages) that consume both internal (sharepoint) and external data.

Why companies end up going with Sharepoint:

  • they get it a very low cost (oftentimes free) because they are Microsoft partners
  • they are already so dependent on Microsoft products that Sharepoint seems to be the logical choice
  • a proper vendor evaluation never takes place and instead the company goes with the apparently easiest and lowest cost alternative
  • enterprise security from a reliable vendor
  • companies know that Microsoft isn’t going anywhere whereas some of the other collaboration vendors in the space might not be around the long
  • it was one of the earlier collaboration platforms available (initial release was actually in 2001)
  • they focus on what Microsoft says it can do and is good vs what it can really do and is good at (marketing vs reality)

Hope this helps….Happy Coding!!!!

Difference between List and Library

Although in most of the UI and API they can be manipulated the same way, there are some significant differences between Lists and Libraries. The latter is a specialized type of the former.

Lists:
Can have attachments
Have major versions only
Do not have Check-in/Check-out features

Libraries:
Cannot have attachments (files are directly in the library)
Have both minor (draft) and major (published) versioning
Have Check-in/Check-Out
Publishing Libraries can use Page Layouts
Have Unique Document Ids out of the box
In code, you can always obtain an SPList from a SPDocumentLibrary but not the other way around.

Hope this helps…Happy Coding!!!!

SharePoint Server object model

Microsoft SharePoint 2010 offers a highly structured server-side object model that makes it easy to access objects that represent the various aspects of a SharePoint Web site. From higher-level objects, you can drill down through the object hierarchy to obtain the object that contains the members you need to use in your code. These objects are listed below:

  • SPFarm Objects
  • SPServer Objects
  • SPService Objects
  • SPWebApplication Objects
  • SPSite Objects
  • SPWeb Objects
  • SPList Objects

SPFarm Object:

The SPFarm object is the top level  or root object in the hierarchy, which is designed to interact with the configuration data store. It contains global settings for all the servers, services, and solutions that are installed in a server farm.

Its Servers property contains a collection representing all the servers in the environment, and similarly the Services property has a collection representing all the services.

You can get the current top-level server farm object as follows:

SPFarm myFarm = SPContext.Current.Site.WebApplication.Farm;

To get a reference to either the server farm or the current physical server, you can use the following static properties:

Microsoft.SharePoint.Administration.SPFarm.Local or

Microsoft.SharePoint.Administration.SPServer.Local.

SPFarm myFarm = SPFarm.Local;

“SPFarm.Local provides the entry point to the current farm”

The term physical object does not necessarily mean that the objects exist separately on disk. SharePoint stores all objects in the content database. You may consider a physical object as something that might exist (such as a server machine) or that can be stored elsewhere (such as a file).

The SPFarm class has three child classes:

  1. SPServer, which represents a single server within the farm;
  2. SPService, which gives access to the farm’s services; and
  3. SPSolution, which is used to deploy something to the farm.

SPFarm is closely related to the configuration database. For instance, the “DisplayName” property of the SPFarm object returns the name of the configuration database. Like many other objects, SPFarm inherits from SPPersistedObject, which means the object’s data is persisted in the configuration database. That’s the reason so many objects have static properties returning useful information without any visible connection to other objects. They simply pull the data from the configuration database.

SPServer Object:

Each physical server computer is represented by an SPServer type.  The  ”ServiceInstances” property provides access to the set of individual service

instances that run on the individual computers, each as an SPService type.

Typical properties of an SPServer object are the IP address (Address property) and the Role property.

Role returns an enumeration of type SPServerRole:

  •  Application: The server runs a web application.
  •  Invalid: The server has not yet registered any role in the configuration database.
  • SingleServer: The server is the only server in the farm (also called single-server installation).
  •  WebFrontEnd: The server is a regular front-end server in the farm.

As for SPFarm, the SPServer object has a static property, Local, that represents the local machine:

SPServer myserver = SPServer.Local;

Using the constructor with a server’s address, you can instantiate any server’s object from:

SPServer myserver = new SPServer(“myfarm-dev64″);

SPServer’s ServicesInstances property returns all its services. This includes SharePoint’s Windows and web services.

SPService Object:

Each SPService object represents a logical service or application installed in the server farm. A  service object provides access to farm-wide settings of the load-balanced service that a respective service instance implements. Derived types of the SPService class include, for example, objects for Windows services, such as the timer service, search, Microsoft SQL Server, the database service, and objects for  web services.

All services have the SPService base class. The most confusing thing here is that both Windows services and web services inherit from same base class.

Moreover, the SharePoint-specific services, such as incoming e-mail, are in the same class hierarchy. This is another class that inherits from SPPersistedObject, ensuring persistence in the configuration database. The class provides members

that return the assigned and currently running jobs. Most services process jobs in a queue. A service can have multiple instances. The Instances property returns them, and the SPServiceInstance class defines the objects representing an instance. An instance can be started or stopped.

Several web services support features provided by the API as well. The major difference is that web services can be used remotely. The API access is only possible if the code is executed on the target machine. The internal services that SharePoint provides can be configured in several ways. These services are:

  • Web Application service
  • Administration service
  • Timer service
  • Diagnostics service
  • Central Administration service
  • Search service
  • Outgoing E-mail service
  • Incoming E-mail service
  • User Code service
  • Database service

Front-end servers usually run services such as the Web Application service. Application servers with a dedicated function run the Search service, for instance. Depending on the entire farm configuration, the services spread across the farm. The criteria to let one or another service run on a specific machine depend on the expected performance and physical capabilities of the hardware. If you know that the Search service takes 80 percent of the CPU power, then it would be unwise to locate it on the same machine as the front-end server.

From the class model perspective, the services structure has five specific variants:

  1. Windows services (SPWindowsService)
  2. Web services (SPWebService)
  3. Diagnostic services (SPDiagnosticService)
  4. Outgoing E-mail services (SPOutboundEmailService)
  5. Incoming E-mail services (SPIncomingEmailService)

All these classes inherit from SPService. To investigate the current configuration of all services within the farm, the following code snippet is

helpful. It consists of a small console application:

SPServiceCollection services = SPFarm.Local.Services;

foreach (SPService service in services)

{

SPRunningJobCollection runningJobs = service.RunningJobs;

}

 

Services provide specific functions. The execution can be scheduled by a job. A job can be launched by a timer—either regularly (e.g., every 12 hours) or at a specific time (e.g., Monday at 7:00 p.m.)—or triggered by an event. They execute either on one server or on several servers on the farm, depending on

the purpose of the service.

SPWebApplication Object:

Each SPWebApplication object represents a load-balanced web application based on IIS. The  SPWebApplication object provides access to credentials and other server farm–wide application settings.

The Sites property contains the collection of sites or site collections within the web application, and the ContentDatabases property is a collection of content databases used in the web application. The SPWebApplication class replaces the obsolete SPVirtualServer class, but it can still be helpful to think of an SPWebApplication object as a virtual server—that is, a set of one or more physical servers that appear as a single server to users.

The top-level object, SPWebApplication, represents a whole application. Usually this object is directly related to at least one content database.  SPWebApplication inherits from SPWebService, because an application is treated internally like a service. The ContentDatabases property returns a collection of content database represented by SPContentDatabase objects. Each web application is hosted in IIS; hence, the IisSettings property gives  convenient access to the settings without needing a connection to IIS. Similarly, the ApplicationPool property gives access to the application pool.

SPWebApplication is another object that inherits from SPPersistedObject, meaning that the object’s data is persisted in the configuration database.

You can get an SPWebApplication object by different methods. Within a SharePoint application, you can use the current context:

SPWebApplication web = SPContext.Current.Site.WebApplication;

From anywhere else, such as a console application, use the following:

SPWebApplication web = new SPSite(“http://myserver/site&#8221;).WebApplication;

You’ve already learned that any request is handled by an ASP.NET application and represented by an HttpApplication object. SharePoint has its own  implementation, SPHttpApplication, which is tightly coupled toSPWebApplication. Each web application runs its own SPHttpApplicationobject. The content stored with the features provided by a web application is held within one or more content databases. Each database object has, along with the web application object, references to one or more SPSite objects.

SPSite Object:

An SPSite object represents the collection of site collections within the web application. The Item property or indexer gets a specified site collection from the collection, and the Add method creates a site collection within the collection.

Each SPSite object, despite its singular name, represents a set of logically relatedSPWeb objects. Such a set is commonly called a site collection, but SPSite is not a typical collection class, like SPWebCollection. Instead, it has members that can be used to manage the site collection. The AllWebs

property provides access to the SPWebCollection object that represents the collection of all web sites within the site collection, including the top-level site. The Microsoft.SharePoint.SPSite.OpenWeb method of the SPSite class returns a specific web site.

If your application does not run inside the context of a SharePoint site, you have to retrieve the Site object based on its URL:

SPSite mySiteCollection = new SPSite(“AbsoluteURL”);

SPWeb Object:

Each site collection includes any number of SPWeb objects, and each object has members that can be used to manage a site, including its template and theme, as well as to access files and folders on the site. The Webs property returns anSPWebCollection object that represents all the subsites of a specified site,

and the Lists property returns an SPListCollection object that represents all the lists in the site.

To return the top-level web site of the current site collection, use the RootWeb property:

SPWeb topSite = SPContext.Current.Site.RootWeb;

The SPContext class does not limit you to getting the current object of any given type. You can use the Microsoft.SharePoint.SPSite.AllWebs property to obtain a reference to any other web site than the current one. The following code returns the context of a specified site by using an indexer with the  AllWebs property:

SPWeb webSite = SPContext.Current.Site.AllWebs[“myOtherSite”];

SPList Object:

Each SPList object has members that are used to manage the list or access items in the list. The GetItems method can be used to perform queries and retrieve items from the list. The Fields property of an SPList returns anSPFieldCollection object that represents all the fields, or columns, in the list, and the Items property returns an SPListItemCollection object that represents all the items, or rows, in the list.

Hope this helps…Happy Coding!!!!