jeudi 21 avril 2011

Working with SharePoint Objects


This article shows how to work with SharePoint objects in order to retrieve, update and persist their properties, moreover how to create, delete or dispose SharePoint objects.

I-                   Retrieve properties

To retrieve a property is similar like with any .NET object. For example to retrieve properties for list and list collection:

SPWeb  myWeb = SPContext.Current.Web;
String title = myWeb.Title;  // simple property
SPListCollection listCollection = myWeb.Lists; // collection property

II-                Updating and persisting properties

To update a property is similar like with any .NET object. For example to update properties for a Web:

SPWeb myWeb = SPContext.Web;
myWeb.title = “My updated Title”;  
myWeb.Update();  // to persist changes

III-             Creating SharePoint Objects
To create a SharePoint object, we can instantiate a new object or add a object to a collection. Example:

SPSite  mySite = SPContext.Current.Site;
SPWeb myWeb = mySite.AllWebs.Add(‘’myProjectFolder ‘’) ;


IV-             Deleting SharePoint Objects
To delete a SharePoint object, we call the function Delete(). The Delete() method sends some objects to the recycle bin (as SPList) and delete other permanently (as SPSite). To ensure that the object will be sent to recycle bin, use the method Recycle().

Example:  
SPWeb  myWeb = SPContext.Current.Web;
myWeb.Delete(); //

V-                Disposing SharePoint Objects
To dispose a SharePoint object, call the Dispose() method. It ensures that the object is destroyed and release data correctly. Only SPSite and SPWeb objects support the dispose functionality. The other objects are managed by the garbage collector. If you set an object variable to a existing instance that you did not create, do not dispose this object, for example SPWite.Current.Web.  
You have 3 approaches to dispose objects:
-          With try-catch-finally block
-          With foreach loop
-          With the “using block”. When a using-block has completed execution, the object will be dispose automatically. Remind not to dispose instance that your code did not create.

Example:
Using (SPSite mySite = new SPSite(http://sharepoint))
{
}



SharePoint 2010 Object Hierarchy

This article presents the SharePoint 2010 (SP10) Objects Hierarchy, their relationships and how to use them programmatically.  
Figure 1: Hierarchy of the core objects in SharePoint 2010.
 
I-                   The SPFarm Object is the highest object in the hierarchy; it is a class and global static object. 
It has no constructor and must be set to a static object (for example SPFarm.Local). The SPFarm has 
properties, methods and event that impacts all the farm settings, for exampleServices, OnBackup event.

     To create a SPFarm object and set it to the local farm write:
           
###########################
SPFarm myFarm = SPFarm.Local;


II-                The SPService Object is a base class and represents a service operating on the entire farm, 
for example: Access service, Excel calculation service, Business Data connectivity service, User Profile 
service, SPWebService. A Web application always belongs to a SPWebService.    

     The sample code shows how to identify the SPWebServices in the farm services. It also check if the user has administrator rights: 
###########################
SPFarm myFarm = SPFarm.Local;
           
If (myFarm.CurrentUserIsAdministrator)    
{         
            foreach (SPService srvc in myFarm.Services)          
{         
if (srvc is SPWebService)     
{         
            SPWebService webservc = (SPWebService) srvc;   
….      
….      
}                     
}                     
           
}         


III-             The SPWebApplication Objects map to IIS web sites. 
It is different from   SharePoint Site or SharePoint Web. A SPWebApplication has its own web.config. 
 A SPWebApplication can be created or deleted using the Central Administration. To create: add a 
newWeb Application in the Web application collection of the SPWebService, A IIS web site is 
automatically created, look in IIS Manager.   Typically, developers work with existing Web application.

     The sample code shows how to iterate through the WebApplications collection of a SPWebService    Object, and check if a WebApplication   is a SharePoint Central Administration application.

###########################
SPFarm myFarm = SPFarm.Local;
           
If (myFarm.CurrentUserIsAdministrator)    
{         
            foreach (SPService srvc in myFarm.Services)          
{         
if (srvc is SPWebService)     
{         
            SPWebService webServc = (SPWebService) srvc;  

foreach (SPWebApplication webApp in webServc.WebApplications)
{
if (webApp.IsAdministrationWebApplication)
{
}                                             
}                    
}         
}                                
}         
           
           


IV-             The SPSite Object is a class and represents a site collection, a security boundary.   
A SPSite has a RootWeb property that contains users, groups, rights, permissions. It can have features 
and solutions that impact all the site collection. A SPSite can be created by passing a url to its constructor,
by adding a site in the site collection of a Web Application or by referencing the current context site. 
The deletion of a Site depends on the chosen instantiation approach.

      The sample code shows how to instantiate a SPSite Object. 

###########################
SPSite mySite = new SPSite(“http://sharepoint”);
mySite.Dispose();

or

foreach (SPSite mySite in webAppl.Sites)
{
try
{         
}         
finally
{
mySite.Dispose();
}
or

SPSite mySite = SPContext.Current.Site;
// this site must not be dispose, then “SPContext.Current” is a global static class.


V-                The SPWeb Object represents a SharePoint Site that can contain pages, lists, libraries, other 
SPWeb objects as subwebs. A SPWeb object can be instantiated as the RootWeb property of a SPSite 
object, as a member of AllWebs collection of a SPSite object, as the return value of the OpenWeb 
method, or by referencing the current context Web.

     The sample code shows how to instantiate a SPWeb Object. 

###########################
SPSite mySite = new SPSite(“http://sharepoint”);
SPWeb myWeb = mySite.RootWeb;
myWeb.Dispose();
mySite.Dispose();

or

SPSite mySite = new SPSite(“http://sharepoint”);
SPWeb myWeb = mySite.OpenWeb(“/mySiteProject/”);  // sous–site relatif au serveur
myWeb.Dispose();
mySite.Dispose();

or
foreach (SPWeb myWeb in mySite.RootWeb.Webs)
{
try
{         
}         
finally
{
myWeb.Dispose();  
}
}

or

SPWeb myWeb = SPContext.Current.Web;           
…       
// this web must not be dispose, then “SPContext.Current” is a global static class.


VI-             The SPList Object contains data: native data, ducuments, images and other media, lookups.  
A SPList object can be referenced as member of the Lists collection from a SPWeb object, by adding to
the Lists collection. SPList Objects have many properties, for example content type support, versioning 
settings, visibility.

     The sample code shows how to iterate through the Lists collection of an SPWeb object, and handle the visible Lists.

foreach (SPWeb myWeb in mySite.RootWeb.Webs)
{
try
{         
foreach (SPList list in childWeb.Lists)
{
            If (!list.Hidden) // Check if the list is visible  
{
                        …
}
}
}         
finally
{
myWeb.Dispose(); 
}
}


lundi 18 avril 2011

SharePoint 2010 Platform Overview

This article describes the SharePoint 2010 (SP10) development platform, its benefits, tools, architecture, execution environment, technologies, objects, functionalities, features and services.

SP10 includes 6 major Workloads: sites, communities, composites, content, search, insights.
The developer suite tools for SP10 includes: Microsoft Visual Studio 2010 (VS10), Microsoft SharePoint 2010 SDKs, Team Foundation Server, SharePoint Designer 2010 (SPD10). SharePoint Designer is a powerful tool to design and develop Data Content, Workflows, and “Look and Feel” for pages, Business Data Connectivity (BDC) and many other components.
With SP10, You can build 4 types of target applications: Web Part solution, Business Collaborative application, Portal for line-of-business application data, customization or extension of the 6 SP10 Workloads.
The SP10 Execution Environment is either on-premise, hosted or online (SharePoint Online). The SP10 environment administration reuses the well-know concepts of system administration such as users, groups, permissions, etc. It facilitates the control of the execution environment. The SP10 has an active community made of forums, MVP, guidance, best practices, Microsoft technical information, white papers, official courses, articles, books and trainings.

The SP10 technology stack is a 64-bit environment built on mature technologies (Windows, IIS 7.0, .NET, ASP). SP10 also integrate many familiar tools (Microsoft Office, Internet explorer) able to manipulate SP10 data.

Figure 1: SharePoint 2010 Technology Stack. In red, the old name of the technology.