Before you can write any managed code for SharePoint you must reference the assembly that contains the classes you need. Although there are many assemblies in a SharePoint installation, there are a few that you will use often. If you are using one of the SharePoint project templates in Visual Studio 2010, the appropriate references are usually preconfigured when you create the Visual Studio solution.
The most commonly used assemblies are in the SharePoint root within the ISAPI folder although there are many located in other places, especially in the global assembly cache. Some of the most commonly used assemblies are:
- Microsoft.SharePoint: The core classes for SharePoint Foundation.
- Microsoft.SharePoint.Client: The core classes for client applications using the managed client object model.
- Microsoft.SharePoint.Linq: LINQ to SharePoint.
- Microsoft.SharePoint.WorkflowActions: SharePoint specific workflow actions.
The classes that represent the elements common to all SharePoint sites are in the Microsoft.SharePoint assembly. Figure 15 shows a few of these classes. If you have taken the time to become familiar with SharePoint as a user, the purpose of most of these should be obvious to you at this point—SPSite is a site collection, SPWeb is a web, and so on.
Some of the commonly used classes in Microsoft.SharePoint.dll.
SPContext is the only class shown in figure above that doesn’t represent an item in a SharePoint site. You use this extremely handy class when writing code that runs when SharePoint renders a page, for example in a Web Part or user control, to gain access to the current request. You can use SPContext . Current to get access to the current:
It is important to note that SPContext is always governed by the security context of the user that is requesting the page. You can get the current user programmatically via the CurrentUser property of the SPWeb class. For example:
SPUser currentUser = SPContext.Current.Web.CurrentUser;
Because SPContext.Current uses the security context of the current user your code will throw a security exception if it attempts to perform any operations that the user does not have permission to perform.
As you saw in Figure above, it is easy to guess the names of most of the core classes—just add an SP to the front of the name. For example, it should come as no surprise that the class you use to work with an alert is SPAlert! Other common conventions include:
- An item’s name is usually the Title property such as SPWeb.Title and SPList.Title.
- Most collection indexers provide the following overloads for accessing collection items:
- Ordinal index
- Title (if applicable)
- Most changes to property changes only persist when you call the object instance’s Update() method.
This post is an excerpt from the online courseware for our Microsoft SharePoint 2010 for Developers course written by expert Doug Ware.
Doug Ware is a SharePoint expert and an instructor for many of our SharePoint 2007 and SharePoint 2010 courses. A Microsoft MVP several times over, Doug is the leader of the Atlanta .NET User Group, one of the largest user groups in the Southeast U.S., and is a frequent speaker at code camps and other events. In addition to teaching and writing about SharePoint, Doug stays active as a consultant and has helped numerous organizations implement and customize SharePoint.