Tuesday, September 7, 2010

SharePoint Object Model

SharePoint Object Model is the way of accessing, creating, deleting the SharePoint lists, document libraries, features, meetings, subsites, etc through the visual studio code. It uses SharePoint hierarchy from SPSite to SPField for the same.


There are several namespaces (around 50) that house the SharePoint object model. Out of these 50 namespaces we are using only 16 namespaces, rest of the namespaces are reserved for Microsoft internal use.
SharePoint Object Model is used for operations on following fields:
  • Document Library
  • Lists
  • Features
  • Meetings
  • Sites
  • Solutions
  • User Profiles
  • Business Data Catalog

Refer the following diagram to get an idea about SharePoint Object Model overview.
Above diagram shows how the SharePoint List is accessed using SPSite, SPWeb objects.
Let’s see how the SharePoint Object Model is used for every SharePoint entity.

1) Document Library:

NameSpace: Microsoft.Sharepoint

Classes: SPDocumentLibrary, SPPictureLibrary

Document libraries are a special type of list designed to store documents. To access a document library through object model we need to use Microsoft.Sharepoint namespace. To use this namespace in ASP.net we need to add a reference of Microsoft.Sharepoint.dll. This dll we can find from the below mentioned path:

C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\ISAPI

Lets take each and every class in this namespace:
          In C# code: SPDocumentLibrary obj = (DocumentLibrary)oList.
         oList is an object for list. The above code will cast the list object into Document Library.
  • SPPictureLibrary Class: This class represents a picture library in Windows SharePoint Services. Both classes are used in the same manner.
         In C# code: SPPictureLibrary oPictureLibrary = (SPPictureLibrary)list;

         The only difference is: As the name indicates SPDocumentLibrary is used for Document Library and   SPPictureLibrary is used for Picture Library.
         Code Example:


2) Lists:

NameSpace: Microsoft.Sharepoint

Classes: SPList, SPListItem, SPLIstItemCollection

SPList object represent a List, SPListItem represent a List Item in the List and SPListItemCollection object represent a full item collection in the SPLIST.
  • SPList: Represents a list on a Microsoft SharePoint Foundation Web site.
  • SPListItem: Represents an item or row in a SharePoint list.
  • SPListItemCollection: Represents a collection of SPListItem objects
Code Example:


3) Features:

NameSpace: Microsoft.Sharepoint

 
Classes: SPFeatureDefinition, SPFeatureScope, SPElementDefinition, SPFeature, SPFeatureProperty

 
For using the features through SharePoint object model we need Microsoft.Sharepoint dll and you can get the dll from the above mentioned path.
  • SPFeatureDefinition Class: Contains the base definition of an SPFeature, including its name, identifier, scope, and version
  • SPFeatureScope Class: Specifies the scope to which the feature applies.
  • SPElementDefinition Class: Serves as the base class for implementing element types within Windows SharePoint Services
  • SPFeature Class: Represents the state of a feature at its corresponding scope
  • SPFeatureProperty Class: Represents a single Feature property.
Code Example:

 

4) Meetings:
NameSpace: Microsoft.Sharepoint.Meetings

Classes: SPMeetings, MtgUtility
For using meetings through object model we need to add Microsoft.Sharepoint dll as mentioned above. The classes under this category are explained as below:
SPMeeting Class: - Provides methods and properties that can be used to work with a Meeting Workspace. Suppose if you want to create a meeting workspace through object model then we need to use this class. There are different kind of meeting template that can be handled like Basic meeting, Decision Meeting, Multiple meeting etc.
MtgUtility: - Initialize a new instance of the MtgUtility class. To access the MtgUtility service and its methods, set a Web reference to http://Server_Name/[sites/][Site_Name/]_vti_bin/xxx.asmx.
Code Example:


5) Sites:


NameSpace: Microsoft.Sharepoint, Microsoft.Sharepoint.Administration (only for SPSiteCollection)

Classes: SPSite, SPSiteCollection, SPWeb

Sites objects are used to access the SharePoint site, the classes under this category of SharePoint Object model are explained as below:

SPSite: Represents a collection of sites in a Web application, including a top-level Web site and all its subsites. Each SPSite object, or site collection, is represented within an SPSiteCollection object that consists of the collection of all site collections in the Web application.

SPWeb: Represents a Windows SharePoint Services Web site

SPSiteCollection: Represents a collection of SPSite objects or site collections that are associated with a particular Web application, including a top-level Web site and all its sub-sites. Each SPSite object, or site collection, is represented within a SPSiteCollection objects that consists of the collection of all site collections in the Web application.

Code Example:

 
6) Solutions:


NameSpace: Microsoft.Sharepoint.Administration

Classes: SPSolution, SPFeatureReceiver, SPSolutionCollector

It provides administrative types and members for managing a Windows SharePoint Services deployment. There are so many classes, delegates, interface under this namespace.

SPSolution: Represents a solution on a farm.

SPFeatureReceiver:
Base abstract class that can be overridden to trap the activation, deactivation, installation, or uninstallation of a Feature.
Use the SPFeatureReceiver class to trap events that are raised after the Feature installation, uninstallation, activation, or deactivation action has been performed.
It is not possible to cancel an installation or uninstallation through Feature events.

SPSolutionCollector: Represents a collection of SPSolution objects.

7) User Profiles:

NameSpace: Microsoft.Office.Server.UserProfiles

Classes: UserProfile, UserProfileManager

UserProfile: Represents a user profile for a person in the user profile database

UserProfileManager: A collection of UserProfile objects used to access user profile data. To access a specific user profile, call the UserProfileManager class to create a UserProfile object and retrieve the corresponding data from the user profile database.

Code Example:


8) Business Data Catalog:


NameSpace: Microsoft.Office.Server.ApplicationRegistry.Administration

Classes: EntityCollection, ApplicationRegistry

EntityCollection: Represents a collection of Entity objects. Provides methods to create and enumerate entities.

ApplicationRegistry: Provides access to all of the line-of-business (LOB) systems and LOB system instances registered in the Business Data Catalog. This is the top-level object in the Business Data Catalog's object model. It is the entry point for you to create, read, update and delete all the metadata objects including LobSystem, Entity and Method. The ApplicationRegistry object has its own ACL and a user should at least have the Edit right on it to create a new LobSystem.


Memory issues with the Object Model:

• Management of these objects should be done with care. They use unmanaged resources, especially SPSite and SPWeb, so Dispose must be called at the finally block or else your application will leak memory.

• Another important memory issue involves the SharePoint indexing. Playing around with the code you probably noticed that everything is grouped into collections, such as the Sites collection and the Lists collection and can be accessed through indexing (i.e. Sites[i]). Well, under the hood SharePoint creates a new instance for each index and each of them needs to be disposed separately. This means that very easily, by a simple if (Sites[i] != null) you're leaking an object.MS came out with a tool called ‘SPDisposeCheck to help manage the SP objects.

You can visit my others blogs also related to SharePoint:
 
Checkpoints for creating Custom Event Handlers
Strong Named Assembly in SharePoint
SharePoint 2010 Overview

No comments:

Post a Comment