Wednesday, April 25, 2012

Sharepoint Object Model vs Asp.Net Object Model

Microsoft SharePoint Foundation offers a highly structured server-side object model . Here are some comparisons


The objects in the object hierarchy include SPFarm, SPService, SPWebApplication, SPSite, SPWeb, SPList, and SPListItem

 
Location difference
-----------
asp.net: wwwroot/webapp/web.config
sharepoint:wwwroot/wss/virtualdirectries/5555/web.config

 
Major Differences
Sharepoint modiefies the pipeline
loading resource from DB
non asp.net related contenet
no compile mode
control restriction on page

 

 
Sharepoint :

  • Farm-SPFarm:
    Farm consists of one or more web Services and Web applications. Contains a global static object SPFarm.Local (Microsoft.SharePoint.Administration.dll ).  SPFarm objects are used only when you are creating administrative applications like upgrade backup restore etc. not for sites or list items.
SPService (base class for services) is also used in this context for Services for administration.
  • Web Application-SPWebApplication(traditional ISS web site equivalent with SP configured)
This is equivalent to web server in ASP.Net
eg: http://sp2010:2120 . This is server url in asp.net. But this is a web application in SharePoint.
For a .Net web developer, 'Web application' used to be the template you add in a VS solution.
When you create a Web application by using the SharePoint Central Administration application, an IIS Web site is automatically created. You can verify whether the IIS Web site has been created by looking in IIS Manager, where you can see its structure, Web.config file, and virtual directory mappings. Information workers/end users do not browse to SPWebApplication. They actually browse to SharePoint Sites, which are SPWeb objects(explained later).
As a developer, it is also possible to create Web applications by adding SPWebApplication objects to the WebApplications collection of a SPWebService object,. But only to replace the SharePoint Central Administration application. Usually you will work with an existing SPWebApplication to access its collection of SPSite objects, or to control application-wide settings(alert config).you might write code to add or modify Web.config settings for a Web application by using the SPWebConfigModification class.
  • Site Collection- SPSite 
           Objects with largest scope and has rootweb property (SPWeb) representing the URL the enduser might browse. SpSite is a security boundary and a container for Users and groups and also for SPWeb and SPList objects. As a developer, you will typically access SPSite objects in code to reference the Webs that they contain, or to administer security programmatically. However, also be aware that you can target development projects that are scoped at the site collection level(site-level features and solutions)
Instantiating SPSite Objects: You can work with SPSite objects in a very flexible way. For example, you can instantiate SPSite objects by doing any of the following:

    • Passing in a Url to its constructor.
    • Referencing a member of the Sites collection of a SPWebApplication
    • Referencing the current context site

  • SPWeb:
          
SPWeb objects are effectively a broad unit of storage, as they expose SPListCollection objects that contain pages libraries, lists, and document libraries. An important concept is that one SPWeb object can also contain other SPWeb objects to represent sites and sub-sites from the information worker's perspective. SPWeb represents a website from a end user experience. It is the container for items like lists and pages and so forth

Instantiating SPWeb Objects
    • As the RootWeb property of an SPSite object.
    • As a member of the AllWebs collection of an SPSite object.
    • As the return value of the OpenWeb method of an SPSite object.
    • By referencing the current context Web. 
  • Lists(SPList,also SPFile for documents)
       SPList objects are the primary container for SharePoint data, such as SPListItem objects. SPListItem objects lookups to other list data and may contain native SharePoint Server 2010 data, documents, site pages, images and other media. As a developer, you can reference SPList objects as members of the Lists collection from a SPWeb, and you can create SPList objects by adding to the Lists collection. Other properties include visibility, content type etc.

SPWebservice-represents a service contaning one or more Web applications
----
Disposal
SPSite - wrap in 'using '
            automatically disposes SPWeb(those that gets lazily initialised)
SPWeb-if you are creating it then you dispose.
SPContext
      Do not dispose as (Sharepoint handles)
Tip: check intelisense for a dispose method . if it has then dispose it
for more on disposal

----------start working
To create a SharePoint site programmatically, you add an SPWeb object to an SPWebCollection object. Existing sites expose a Webs property of type SPWebCollection, so you can use the Add method of this Webs property to create a subsite.
 
 
[to be continued]