Diving Into the Windows SharePoint Services 3.0 API

Author Bob Mixon is the MSD2D SharePoint Community Manager and Managing Director of ShareSquared, Inc.

 Core Windows SharePoint Service 3.0 Platform

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), let's focus on the core Windows SharePoint Services 3.0 platform.

The core SharePoint Services platform is composed of more than 3,000 APIs. 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.

Retrieve Current Site Collection

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;

Custom Web Part Development Effort

If you want to use some of what we learned above in a custom Web Part development effort, your code might 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.

Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish