Thursday, November 15, 2012

Sharepoint Server Object Model

Sharepoint Server Object Model      

 
If you are creating a Web Part, custom Web service, or Web application to work with site collections, individual sites, or lists, you can use members of the Microsoft.SharePoint.SPContext class to obtain the current site collection, Web site, or list. When you create a Web application in the /_layouts virtual directory, its functionality becomes available to all sites on the Web server. Outside of an HTTP context, such as in a console application or a Windows application, use a constructor of the SPSite class to obtain a specific site collection and to reach various objects within the collection. 

Server Architecture

The following diagram shows the SharePoint Foundation server architecture in relation to the collections and objects of the Microsoft.SharePoint.Administration namespace.

  1. The SPFarm object is the highest object within the SharePoint Foundation object model hierarchy. The Servers property gets a collection representing all the servers in the deployment, and the Services property gets a collection representing all the services.
  2. Each SPServer object represents a physical server computer. The ServiceInstances property provides access to the set of individual service instances that run on the individual computer.
  3. Each SPService object represents a logical service installed in the server farm. Derived types of the SPService class include, for example, objects for Windows services, such as the timer service, search, the database service, etc. and also objects for Web services, such as the basic content publishing Web service which supports the Web applications.
  4. An SPWebService object provides access to configuration settings for a specific logical service or application. The WebApplications property gets the collection of Web applications that run the service.
  5. If the service implements the Service Application Framework of SharePoint Foundation, then it can be split into multiple configured farm-scoped instantiations (CFSIs). Each of these provides the functionality of the service but each has its own individual permission and provisioning settings.
  6. Each instance of a service, or a CFSI, that is running on a specific server is represented by an SPServiceInstance object.
  7. An SPDatabaseServiceInstance object represents a single instance of a database service running on the database server computer. The SPDatabaseServiceInstance class derives from the SPServiceInstance class and thus inherits the Service property, which provides access to the service or application that the instance implements. The Databases property gets the collection of content databases used in the service.
  8. Each SPWebApplication object represents a Web application hosted in an Internet Information Services (IIS) Web site. The SPWebApplication object provides access to credentials and other farm-wide application settings. The Sites property gets the collection of site collections within the Web application, and the ContentDatabases property gets the collection of content databases used in the Web application.
  9. An SPContentDatabase object inherits from the SPDatabase class and represents a database that contains user data for a Web application. The Sites property gets the collection of site collections for which the content database stores data, and the WebApplication property gets the parent Web application.
  10. An SPSiteCollection object represents the collection of site collections within the Web application.
  

The following diagram shows the SharePoint Foundation site architecture in relation to the collections and objects of the Microsoft.SharePoint namespace.
Site Achitecture
  1. Each SPSiteobject, despite its singular name, represents a set of logically related SPWeb objects (see below). Such a set is commonly called a "site collection," but SPSite is not a standard Microsoft .NET collection class, in contrast to SPWebCollection. Rather, 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 SPSite.OpenWebmethod of the SPSite class returns a specific Web site.
  2. 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 an SPWebCollection 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.
  3. 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 that return specific items. The Fields property returns an SPFieldCollection 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.
  4. Each SPField object has members that contain settings for the field.
  5. Each SPListItem object represents a single row in the list.

    Example: Accessing the List Items in a List


    Create a console application project and use the following code inside the Console application and execute it.

    using (SPSite site = new SPSite("http://appes-pc/my/personal/dotnet")) // Site Collection

    {

    using (SPWeb web = site.OpenWeb()) // Site

    {

    SPList list = web.Lists[
    "My Contacts"]; // List

    int i = 1;

    foreach (SPListItem item in list.Items)

    {

    Console.WriteLine(
    "Item: " + (i++).ToString());

    Console.WriteLine(item.Name);

    Console.WriteLine(item.Title);

    Console.WriteLine(item.GetFormattedValue(
    "First Name"));

    Console.WriteLine(item.GetFormattedValue("Last Name"));

    Console.WriteLine(string.Empty);

    }

    }

    }

    Console.ReadKey(false);

    If you receive any build errors regarding namespace, change the Project Properties > Target to .Net Framework 3.5.

  6.  Accessing the items in a library
    Enter the following code in the console application.

    using (SPSite site = new SPSite("http://appes-pc/my/personal/dotnet")) // Site Collection

    {

    using (SPWeb web = site.OpenWeb()) // Site

    {
    SPDocumentLibrary library = web.Lists[
    "My Docs"] as SPDocumentLibrary; // Library

    int i = 1;

    foreach (SPListItem item in library.Items)

    {
    Console.WriteLine(
    "Item: " + (i++).ToString());
    Console.WriteLine(item.File.Name);
    Console.WriteLine(item.File.Length);
    Console.WriteLine(
    string.Empty);
    }
    }
    }


    }

No comments:

Post a Comment