Object Hierarchy
Following is the hierarchy in which the classes supporting Server OM reside.
Object Hierarchy - Main Members
SPFarm
SPFarm class belong to the "Microsoft.SharePoint. Administration" namespace refers to a complete Farm. We can create a new farm from scratch or connect to an existing farm.
Connecting to Farm
1) Connect to Existing Farm: (the most common scenario), you provide a SQL Server connection string and the Farm’s secret passphrase to the public static Open() method.
public static SPFarm Open(SqlConnectionStringBuilder connectionString, SecureString passphrase)
Where, The connection string corresponds to the Farm configuration database that is defined while configuring the Farm using the SharePoint 2010 Products Configuration Wizard. It can also be found in the system registry at:
HKLM\Software\Microsoft\Shared Tools\Web Server Extensions\14.0\Secure\ConfigDB\dsn.
2) Connect to Local Farm: we can directly connect to a local Farm, using the static property
2) 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.
3) The SPFarm class has three child classes:
SPServer, which represents a single server within the farm;
SPService, which gives access to the farm’s services; and
SPSolution, which is used to deploy something to the farm.
SPServer
SPServer represents a server in a SharePoint farm.
We can get a current reference to the SPServer:
foreach (SPServiceInstance objService in SPServer.Local.ServiceInstances)
Console.Writeline("objService.TypeName");
Or, Instantiate any server’s object in a specific farm
SPServer objServer = new SPServer(“MyFarmServer″);
SPService
There are numerous services such as web services or Search Services running inside a SharePoint Server.Each of these services can be referenced through SPServices Object.
SPServiceCollection ObjServiceCollection = SPFarm.Local.Services;
foreach (SPService service in ObjServiceCollection){}
SPWebApplication
In object model, SPWebApplication class refers to each Web application configured in SharePoint. It is the topmost object in the site hierarchy.
SPWebApplication objSPWebApplication = SPContext.Current.Site.WebApplication;
Or
SPWebApplication objSPWebApplication = new SPSite("http://home:3000/").WebApplication;
SPSite
The SPSite represents a site collection. To access an SPSite instance, you can create it using one of the following available constructors:
public SPSite(Guid id);
public SPSite(string requestUrl);
public SPSite(Guid id, SPUrlZone zone); // public enum SPUrlZone {Default,Intranet,Internet,Custom,Extranet}
public SPSite(Guid id, SPUserToken userToken); // SPUserToken is the token for a valid SharePoint user.
public SPSite(string requestUrl, SPUserToken userToken);
public SPSite(Guid id, SPUrlZone zone, SPUserToken userToken);
We can create a new Site instance as follows:
SPSite objSPSite = new SPSite("http://home:3000/");
Or
SPSite objSPSite = SPContext.Current.Site;
Note: When you create an SPSite instance using SPUserToken, you can impersonate the user who owns that token rather than the current user. You can import an SPUserToken instance from a previously exported array of bytes, or you can create one from a generic System.Security.Principal.IIdentity.
SPWeb
The SPweb class represent a website or sub-site in a site collection. This class does not have a public constructor. The only way to obtain a reference to a website is through its parent SPSite. The SPSite class provides the OpenWeb() method which has the following overloads:
public SPWeb OpenWeb();
public SPWeb OpenWeb(Guid gWebId);
public SPWeb OpenWeb(string strUrl);
public SPWeb OpenWeb(string strUrl, bool requireExactUrl);
You can use an SPWeb reference to navigate the contents of the website:-
SPSite site = new SPSite("http://home:3000/");
SPWeb web = site.OpenWeb();
Console.WriteLine(web.Title);
You can also use an SPWeb reference to read or change its configuration.
SPSite site = new SPSite("http://home:3000/");
SPWeb web = site.OpenWeb("Samples");
web.Title = web.Title + " - Changed by code!";
web.Update();
Note: While working with the Server Object Model, you are reading and changing an in-memory representation of the current object. Thus, any changes you make will not be applied to the database unless you explicitly request the object to persist its state using the Update method.
SPList
SPList corresponds to a single list instance, whether that is a list of items or a document library.
SPSite site = new SPSite("http://home:3000/");
SPWeb web = site.OpenWeb();
SPList list = web.Lists["MyList"];
Applying Filters on List
SPQuery class is used for applying filters on a list while retrieving items. Commonly used properties of SPQuery are:-
Sample Code
SPWeb objWeb = SpContext.Current.Web;
SPList objList = objWeb.Lists[“Products"];
SPQuery objQuery = new SPQuery();
objQuery.Query = "<Where><Eq><FieldRef Name='P_Category' /><Value Type='Lookup'>" + strCategory + "</Value></Eq></Where>";
oQuery.ViewFields = "<FieldRef Name='Title' /><FieldRef Name='Price' /><FieldRef Name='Description' />";
oQuery.ViewFieldsOnly = true;
SPListItemCollection objColl = objList.GetItems(oQuery);
gvwProducts.DataSource = objColl.GetDataTable();
gvwProducts.DataBind();
Handling Document Libraries
Whenever you use an SPList instance, which corresponds to a document library, you can cast that instance to an SPDocumentLibrary type. When you need to enumerate the files contained in a document library, you can browse the SPListItem elements of the list, accessing their File property, which is of type SPFile.
SPSite site = new SPSite("http://home:3000/");
SPWeb web = site.OpenWeb();
SPDocumentLibrary docLibrary = web.Lists["MyDocs"] as SPDocumentLibrary;
foreach (SPListItem item in docLibrary.Items) {
Console.WriteLine("{0} - {1}",item.File.Name,item.File.Length);}
SPListItem
SPListItem defines a reference to a specific item of a list.
SPList list = web.Lists["MyList"];
foreach (SPListItem item in list.Items) {
Console.WriteLine(item.Title);}
Sample Code : Programmatic drill down form Farm to List
The following codes drills down the various object in Server Object Model and displays the hierarchy from Farm to ListItems.
Following is the hierarchy in which the classes supporting Server OM reside.
Object Hierarchy - Main Members
- SPFarm
- SPServer
- SPService
- SPWebApplication
- SPSite
- SPWeb
- SPList
- SPListItem
SPFarm
SPFarm class belong to the "Microsoft.SharePoint. Administration" namespace refers to a complete Farm. We can create a new farm from scratch or connect to an existing farm.
Connecting to Farm
1) Connect to Existing Farm: (the most common scenario), you provide a SQL Server connection string and the Farm’s secret passphrase to the public static Open() method.
public static SPFarm Open(SqlConnectionStringBuilder connectionString, SecureString passphrase)
Where, The connection string corresponds to the Farm configuration database that is defined while configuring the Farm using the SharePoint 2010 Products Configuration Wizard. It can also be found in the system registry at:
HKLM\Software\Microsoft\Shared Tools\Web Server Extensions\14.0\Secure\ConfigDB\dsn.
2) Connect to Local Farm: we can directly connect to a local Farm, using the static property
SPFarm objFarm = SPFarm.Local
OR
SPFarm objFarm = SPContext.Current.Site.WebApplication.Farm
Inside SPFarm
1) SPFarm contains global settings for all the servers, services, and solutions that are installed in a server farm.Inside SPFarm
2) 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.
3) The SPFarm class has three child classes:
SPServer, which represents a single server within the farm;
SPService, which gives access to the farm’s services; and
SPSolution, which is used to deploy something to the farm.
SPServer
SPServer represents a server in a SharePoint farm.
We can get a current reference to the SPServer:
foreach (SPServiceInstance objService in SPServer.Local.ServiceInstances)
Console.Writeline("objService.TypeName");
Or, Instantiate any server’s object in a specific farm
SPServer objServer = new SPServer(“MyFarmServer″);
SPService
There are numerous services such as web services or Search Services running inside a SharePoint Server.Each of these services can be referenced through SPServices Object.
SPServiceCollection ObjServiceCollection = SPFarm.Local.Services;
foreach (SPService service in ObjServiceCollection){}
SPWebApplication
In object model, SPWebApplication class refers to each Web application configured in SharePoint. It is the topmost object in the site hierarchy.
SPWebApplication objSPWebApplication = SPContext.Current.Site.WebApplication;
Or
SPWebApplication objSPWebApplication = new SPSite("http://home:3000/").WebApplication;
SPSite
The SPSite represents a site collection. To access an SPSite instance, you can create it using one of the following available constructors:
public SPSite(Guid id);
public SPSite(string requestUrl);
public SPSite(Guid id, SPUrlZone zone); // public enum SPUrlZone {Default,Intranet,Internet,Custom,Extranet}
public SPSite(Guid id, SPUserToken userToken); // SPUserToken is the token for a valid SharePoint user.
public SPSite(string requestUrl, SPUserToken userToken);
public SPSite(Guid id, SPUrlZone zone, SPUserToken userToken);
We can create a new Site instance as follows:
SPSite objSPSite = new SPSite("http://home:3000/");
Or
SPSite objSPSite = SPContext.Current.Site;
Note: When you create an SPSite instance using SPUserToken, you can impersonate the user who owns that token rather than the current user. You can import an SPUserToken instance from a previously exported array of bytes, or you can create one from a generic System.Security.Principal.IIdentity.
SPWeb
The SPweb class represent a website or sub-site in a site collection. This class does not have a public constructor. The only way to obtain a reference to a website is through its parent SPSite. The SPSite class provides the OpenWeb() method which has the following overloads:
public SPWeb OpenWeb();
public SPWeb OpenWeb(Guid gWebId);
public SPWeb OpenWeb(string strUrl);
public SPWeb OpenWeb(string strUrl, bool requireExactUrl);
You can use an SPWeb reference to navigate the contents of the website:-
SPSite site = new SPSite("http://home:3000/");
SPWeb web = site.OpenWeb();
Console.WriteLine(web.Title);
You can also use an SPWeb reference to read or change its configuration.
SPSite site = new SPSite("http://home:3000/");
SPWeb web = site.OpenWeb("Samples");
web.Title = web.Title + " - Changed by code!";
web.Update();
Note: While working with the Server Object Model, you are reading and changing an in-memory representation of the current object. Thus, any changes you make will not be applied to the database unless you explicitly request the object to persist its state using the Update method.
SPList
SPList corresponds to a single list instance, whether that is a list of items or a document library.
SPSite site = new SPSite("http://home:3000/");
SPWeb web = site.OpenWeb();
SPList list = web.Lists["MyList"];
Applying Filters on List
SPQuery class is used for applying filters on a list while retrieving items. Commonly used properties of SPQuery are:-
- Query – Filtering and sorting of data based on conditions.
- ViewFields – Retrict the number of fields to be retrieved.
- ViewFieldsOnly – If set true, remove the default fields (ID, Created, Modified) in ViewFields.
Sample Code
SPWeb objWeb = SpContext.Current.Web;
SPList objList = objWeb.Lists[“Products"];
SPQuery objQuery = new SPQuery();
objQuery.Query = "<Where><Eq><FieldRef Name='P_Category' /><Value Type='Lookup'>" + strCategory + "</Value></Eq></Where>";
oQuery.ViewFields = "<FieldRef Name='Title' /><FieldRef Name='Price' /><FieldRef Name='Description' />";
oQuery.ViewFieldsOnly = true;
SPListItemCollection objColl = objList.GetItems(oQuery);
gvwProducts.DataSource = objColl.GetDataTable();
gvwProducts.DataBind();
Handling Document Libraries
Whenever you use an SPList instance, which corresponds to a document library, you can cast that instance to an SPDocumentLibrary type. When you need to enumerate the files contained in a document library, you can browse the SPListItem elements of the list, accessing their File property, which is of type SPFile.
SPSite site = new SPSite("http://home:3000/");
SPWeb web = site.OpenWeb();
SPDocumentLibrary docLibrary = web.Lists["MyDocs"] as SPDocumentLibrary;
foreach (SPListItem item in docLibrary.Items) {
Console.WriteLine("{0} - {1}",item.File.Name,item.File.Length);}
SPListItem
SPListItem defines a reference to a specific item of a list.
SPList list = web.Lists["MyList"];
foreach (SPListItem item in list.Items) {
Console.WriteLine(item.Title);}
Sample Code : Programmatic drill down form Farm to List
The following codes drills down the various object in Server Object Model and displays the hierarchy from Farm to ListItems.
Console.ForegroundColor = ConsoleColor.Green;
Console.BufferHeight = 1000;
//Get local farm
SPFarm farm = SPFarm.Local;
Console.Write(farm.Id.ToString("N").ToLower());
//loop Services
foreach (SPService service in farm.Services)
{
//find WebService
if (service is SPWebService)
{
SPWebService webService = (SPWebService)service;
//Loop WebApps
foreach (SPWebApplication webApp in webService.WebApplications)
{
Console.Write(webApp.Id.ToString("N").ToLower());
//Loop Site Collections
foreach (SPSite site in webApp.Sites)
{
Console.Write(site.ID.ToString("N").ToLower());
//Loop Sites
foreach (SPWeb web in site.AllWebs)
{
Console.Write(web.ID.ToString("N").ToLower());
//Loop Lists
foreach (SPList list in web.Lists)
{
Console.Write(list.ID.ToString("N").ToLower());
//Loop Items
foreach (SPListItem item in list.Items)
{
Console.Write(item.UniqueId.ToString("N").ToLower());
}
}
web.Dispose();
}
site.Dispose();
}
}
}
}
No comments:
Post a Comment