【轉貼】Diving Into the Windows SharePoint Services 3.0 API
今天收到從 MSD2D 來的 SharePoint 電子報,講到 WSS 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 Share Point Services 3.0: Software Development Kit (SDK) . If you prefer, you can access the SDK online .