Showing posts with label SharePoint Object Model. Show all posts
Showing posts with label SharePoint Object Model. Show all posts

Sunday, April 11, 2010

Microsoft SharePoint 2007 Development – Sites and Webs


This article doesn’t cover what SharePoint is or its usage. Targeted audience is the programmers who are already familiar with SharePoint who’s looking for a programming introduction to sites and webs in SharePoint and learn how to interact with sites and site collections with the classes SPSite and SPWeb in SharePoint Object Model.
Simplifying Sites and Webs
At a glance, yes, it’s complicating. Though the Webs and Sites sound similar, in SharePoint these are two different concepts. The site, as far as SharePoint is concerned, is actually a site collection. Each site collection consists of one or more root websites. If you use the object model to create a site collection and you indicate that the template for that site collection should be a Team site, what you have actually done is created a site collection and a root website (SPWeb instance) that was provisioned from the Team Site template. So simply that means you cannot have a site collection that doesn’t have a root website. Two things you should never forget.
•SPSite class is the model for a collection of SPWeb objects.
•To get at the list of site collections on a given web application, you simply reference the Sites property of an instance of the SPWebApplication class.
Working with SPSite Class
SPSite class can be used to create, delete and updating of site collections. For the complete listing of all properties and methods, glance through the online SharePoint Software Development Kit (SDK) found on MSDN (http://msdn2.microsoft.com/en-us/library/aa905858.aspx).
Enough training in the shallow end, let’s get to the deep end. Hit VS 2008 and create a new windows application. Add reference to Microsoft.SharePoint.dll. Add few textboxes, labels and button to the windows form as given below.

image

Switch to the coding window.

image

Then write the below coding in the click event of the button. (TextBoxes have been used as in given in the order).
Let’s have a look at the coding. First I have created a SPSite object called site passing the absolute URL as a parameter. The text on txtSPSite textbox represents the absolute URL.


image 

Next step is to create a SPWebapplication object. Site creation is done by calling the Add() on the SPSiteCollection class instance. 
There are multiple overloads that allow you to supply progressively more information to create the new site collection. Okay, let’s see the parameters I have used in the Add() method. First it’s the siteUrl, next the Title of the site and the Site Description. LCID is the Local Identifier. Default is 1033 for U.S. English. Next comes the Template ID, followed by the owner login, owners name and finally the owners email address.
image

Given above is the windows form filled with sample data according to my SharePoint server.


image

If you can see the above message in your screen, Congratulations! You have successfully created a SharePoint site programmatically using SharePoint Object Model. Open your web browser http://sharepointsvr:4488/sites/TestNewSite/default.aspx [http://sharepointsvr:4488 is my test server] and you can see a screen similar the one given below. For your convenience I’ve provided the default web templates in the table below.

image

Template ID Description
STS#0 Team site
STS#1 Blank site
STS#2 Document workspace
MPS#0 Basic meeting workspace
MPS#1 Blank meeting workspace
MPS#2 Decision meeting workspace
MPS#3 Social meeting workspace
MPS#4 Multipage meeting workspace
WIKI#0 Wiki
BLOG#0 Blog

Updating a site is not easy as creating one. You need to get an instance of the SPWeb class for the root web of the site collection (which was created by default when you created a new site collection).

Working with SPWeb Class
SPWeb class provides most of the functionality of single websites, whereas the SPSite class is designed to deal with site collections.
Let’s add a subweb, a blog as a subweb to the parent site collection we created earlier. To add a new web to the root web of a site collection, you obtain a reference to that site and then call the Add() method on the AllWebs property.
image 

Type the code above in your windows application, make sure to add reference to the Microsoft.SharePoint.dll. Pay attention to the overloads of the Add() method. Hit F5 to test the code. If you see the message box with the message “Web created successfully”, yes you have successfully created a blog web.
image

You can obtain a reference to a SPWeb instance either by obtaining it through an indexer property on a SPWebCollection instance.

image

There are some properties on a website that cannot be changed after the site has been created. Changes made to a SPWeb instance are only saved after calling the Update() method. One last code block to change the title of the blog site we just created.


image
image

Summary
The SharePoint object model is so powerful that you have the full control over each and every component SharePoint provides. But honestly the hard task is to remember the methods and the properties of the classes.
All comments welcome. :)

Tuesday, November 24, 2009

Microsoft SharePoint 2007 Development – SharePoint Object Model

It was in end of year 2007 my good old friend installed Microsoft SharePoint Server 2007 (MOSS 2007) in the company I worked previously. There were two main reasons why we installed it. We were new to the product so yes; we wanted to learn more about SharePoint. Collaboration and document management was key requirements in the project we worked.

But in this article I’m not going to explain about neither what SharePoint is nor its usage. The targeted audience of this article is programmers who are already familiar with SharePoint but wants to learn more about SharePoint Object Model and programmers who are new to SharePoint development.

Namespaces
The well known factor about SharePoint is it’s an application on its own and the less know is it’s a highly customizable application framework. SharePoint is functionally segmented by Windows SharePoint Services (WSS) and Microsoft Office SharePoint Server (MOSS). Technically Windows Server 2003 R2 includes WSS. To add additional enterprise portal functionality such as Excel Services, farm-enabled search, Single Sign-on and Business Data Catalog MOSS can be installed on top of WSS. There are two root namespaces for MOSS and WSS. The Microsoft.Office.Server namespace is the root namespace of all Office Server Objects as well as Microsoft.SharePoint is the root namespace for all WSS objects.

Functional Areas of Object Model
According to the functionality of SharePoint there are different namespaces given to manage and customize SharePoint using Object Model.

List Management
List is more like a Table in a Database. Of all the tasks a SharePoint programmer can do in SharePoint, working with Lists is the mostly common task.
Namespace: Microsoft.SharePoint
Classes: SPList, SPListItem, SPListItemCollection

Document Libraries
This is a special type of list created to store documents. Managing and storing different types of documents, pictures and files will be handled within SharePoint Document Libraries.
Namespace: Microsoft.SharePoint
Classes: SPDocumentLibrary, SPPictureLibrary

Sites
Site Collections, Sites, Web Collections and Webs which is obviously bit confusing tough is an area developers should have a clear understanding.
Namespace: Microsoft.SharePoint
Classes: SPSite, SPSiteAdministration, SPSiteCollection, SPWeb

Features and Solutions
With Features and Solutions it’s dramatically easier to customize sites and site collections. Features provide the ability for sites to reuse functionality that exists in other sites without manually copying and pasting XML code from one template to another. Solutions package Features in a cabinet (.cab) file and define important metadata about the included Features.
Namespace: Microsoft.SharePoint, Microsoft.SharePoint.Administration
Classes: SPFeatureDefinition, SPElementDefinition, SPFeatureProperty, SPFeatureScope, SPFeature, SPSolution, SPFeatureReceiver, SPSolutionCollection

Business Data Catalog
BDC is the most recommended way of Integrating Enterprise Data in to SharePoint. ERPs, Different Data sources, your own in house applications, BDC can cater the integration with SharePoint.
Namespace: Microsoft.Office.Server.ApplicationRegistry.Administration
Classes: EntityCollection, ApplicationRegistry

Meetings
SharePoint has used Meetings to provide collaboration within organizations. Meeting Workspaces provide websites dedicated to supporting and maintaining meetings, meeting information, attendee lists, minutes, meeting-related documents, and much more. (Meting workspace is a web template installed with default SharePoint installation)
Namespace: Microsoft.SharePoint.Meetings
Classes: SPMeeting, MtgUtility

User Profiles
User Profile is used to create, update and manipulate user profiles and profile properties which is way beyond storing basic information of an individual user but which also provide features for community building social networking, etc…
Namespace: Microsoft.Office.Server.UserProfiles
Classes: UserProfile, UserProfileManager

Windows SharePoint Services Server Architecture
In relation to the collections and objects of the Microsoft.SharePoint.Administration namespace the SPFarm object is the top most object in the Object Model hierarchy. Servers and Services properties represent the collections of each. Each SPServer object represents a physical server machine.


SPService object represents a logical service or application installed in the server farm. The 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. The SPDatabaseServiceInstance object represents a single instance of a database service running on the server computer. The Databases property gets the collection of content databases used in the service. Each SPWebApplication object represents a load-balanced Web application based in Internet Information Services (IIS). The SPContentDatabase class represents a database that contains user data for a SharePoint 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. The SPSiteCollection object represents the collection of site collections within the Web application. The Add method creates a site collection within the collection.

Windows SharePoint Server Site Architecture
The diagram below shows the site architecture in relation to the collections and objects of the Microsoft.SharePoint namespace.


Microsoft.SharePoint.SPSite.OpenWebmethod of the SPSite class returns a specific Web site. Each site collection includes any number of SPWeb objects, and each object has members that can be used to manage a site. Each SPList object has members that are used to manage the list or access items in the list. Each SPField object has members that contain settings for the field and each SPListItem object represents a single row in the list.

Development Environment
Well, when SharePoint development is concerned you can setup the development environment in two different ways, remote and local. Each has its own pros and cons. In this article I’m not discussing how to setup those two development environments rather I’ll show how to cheat by setting up a remote development environment despite debugging and easy testing, to try out your first SharePoint application developed by you.
First tool you need is Microsoft Visual Studio 2005/2008. If you are not a licensed user of Visual Studio please download the Express edition for free from Microsoft site. Setting the development environment remotely is simply a development environment that is not on a SharePoint server.
After setting up Visual Studio, open a new Console Application project. Now let’s get to the cheating part of copying the required assemblies from the SharePoint server to your development machine. Browse to the location:
[drive]:\program files\common files\microsoft shared\web server extensions\12\isapi
Copy the Microsoft.SharePoint.dll assembly (only assembly required for the test application) and add reference to the assembly in your project.


Next the most important thing is to setup the doorway which is to establish the site context. Site context is the link between your code and the SharePoint site collection hierarchy which is represented by SPContext class.

Note: Establishing site context is done differently between Web Parts and console/WinForms/WPF applications.


The SPSite constructor used in the preceding code obtains a reference to the site collection contained at the given url (http://sharepointsvr:4488). You can access the web application to which a given site collection belongs using the WebApplication property, which is of type Microsoft.SharePoint.Administration.SPWebApplication. http://sharepointsvr:4488 is where my SharePoint server is. 4488 is the port number. Change yours accordingly. Given below is the result I see in the console window.
Root collection found at Url http://sharepointsvr:4488 is part of Web Application SharePoint - 4488
Hit F5 and check out the output in the console window. WAIT!! You are on a remote dev machine if you followed me till now. Let’s deploy your application.

Application Deployment
For a Windows or Console SharePoint application there’s no difference than a standard application deployment. Either you can create a Click Once deployment. You can simply copy the application and its dependencies (no need to copy the SharePoint DLLs as it’s already there in the server) in to the SharePoint server and checkout the output.
But if you are developing SharePoint applications commercially make sure to follow a proper development & deployment process covering the QA stage.

Summery
SharePoint Object Model is a very powerful application programming interface (API) which gives developers the ability to control virtually every aspect of SharePoint through managed code.