SUBSCRIBE to Windows IT Pro Magazine & SAVE 30%     Register today for your FREE 'To The Point' SharePoint eNewsletter
     
     
Skip Navigation Links.
Collapse Office and SharePointOffice and SharePoint
Expand Newsletter ArchivesNewsletter Archives
Expand Office 2007Office 2007
Expand Office 2003Office 2003
Collapse SharePointSharePoint
Expand Installation and DeploymentInstallation and Deployment
SharePoint Extends a Nonprofit’s Reach
How to change your personal information in MOSS 2007
KPI's in Microsoft Office SharePoint Server 2007
Expand Integrating SharePoint and Microsoft Office 2003Integrating SharePoint and Microsoft Office 2003
Diving Into the Windows SharePoint Services 3.0 API
Hide custom list items
Linking to documents in another document library
Custom Web Part Basics
Expand Integrating SharePoint and Microsoft Office 2007Integrating SharePoint and Microsoft Office 2007
Testing Our Web Part Base Class
Expand Working OfflineWorking Offline
Installing Microsoft's Application Templates
Manage quick menu item using EditControlBlock in WSS 3.0
Expand Windows SharePoint Services Document LibrariesWindows SharePoint Services Document Libraries
Creating and Using a New Column Type
Corporate Blogging
SharePoint 2007 Content Types
Windows SharePoint Services Out of the Box
More About SharePoint 2007 Content Types
Using Content Types in Windows SharePoint Services 3.0
SQL storage planning & monitoring (MS white paper)
Display the user name for the logged on user
Outlook 2007 and SharePoint Synchronization
Use Kerberos to Secure MOSS 2007
10 Important Kerberos Facts
Stsadm
SSRS and MOSS 2007
Shared Tasks Lists with SharePoint and Outlook 2007
Introducing the Business Data Catalog
Information Integration: SSRS and MOSS 2007
What Can I Accomplish with Other SharePoint Technologies?
Integrate SharePoint into Your Exchange Environment
Outlook and SharePoint: Playing Well Together
SharePoint Integration with Outlook 2007, Part 3
Bridge the SharePoint File-Restore Gap
Migration Glitch in SharePoint Portal Server
Windows SharePoint Services 3.0 Out of the Box
SharePoint Security Evolution
Creating and Using a New Content Type in SharePoint 2007
Announcements
     

     

     
     

Diving Into the Windows SharePoint Services 3.0 API

Diving Into the Windows SharePoint Services 3.0 API

By: Bob Mixon - MSD2D SharePoint Community Manager and Managing Director of ShareSquared, Inc.

Most individuals who subscribe to this newsletter have technical roles in the workplace, so I'm going to shift gears and include more technical information. Because of the sheer size of the new Microsoft Office 2007 System, the platform on which you can build and deploy applications is truly incredible.  Before we dive too deep into the various Office application APIs or services (such as Forms Server and Excel Services), lets focus on the core Windows SharePoint Services 3.0 platform.

The core SharePoint Services platform is comprised of more than 3000 API's.  The most fundamental of these are found in the Microsoft.SharePoint namespace.  The classes in this namespace will be used in virtually every Web Part and application development effort, so this is a good place to start.

The four key classes you'll need to become familiar with first are SPContext, SPSite, SPWeb, and SPUser.  The SPContext class represents the context of an HTTP request in SharePoint Services.  The SPSite class represents a collection of sites, including the top-level site and all children sub-sites.  The SPWeb class is used to represent a single site in SharePoint.  And, the SPUser class is used to represent a single user.  Using these classes together lets you access virtually all information about sites and users, both in and out of the current site context.

In most situations, you'll need to know in what site context your code is currently running.  To obtain this information, use the following code:

SPWeb web = SPContext.Current.Web;

If you are interested in retrieving the current site collection, use this code:

SPSite siteCollection = SPContext.Current.Site;

And if you need to access a site collection for a different context, you can use the following approaches:

SPSite siteCollection = new SPSite( "http://yourdomain/sites/hr" );

or

SPSite siteCollection = new SPSite( siteGUID );

Many times you'll want to obtain the current user for the current site context.  Give this a try:

SPWeb web = SPContext.Current.Web;
SPUser currentUser = web.CurrentUser;

If you want to use some of what we learned above in a custom Web Part development effort, your code may look something like the following:

protected override RenderContents( HtmlTextWriter writer )
{
// Obtain context of current site and user.
SPWeb web = SPContext.Current.Web;
SPUser user = web.CurrentUser;

// Display the current users name.
writer.WriteLine( "Greetings: " + user.Name );
}

Obviously, this is a very basic example, but I hope it gives you a clear idea of how to use the current site context to obtain information about the site and user for which your code is executing.

I recommend you first download and install the Windows SharePoint Services 3.0: Software Development Kit (SDK). If you prefer, you can access the SDK online.