Вы находитесь на странице: 1из 8

Introduction No matter how much you know about the SharePoint UI and Visual Studios support for SharePoint

development, you will eventually need to work with the SharePoint object model to get things done. Understanding the SharePoint Object Model in SharePoint 2010 Microsoft has spent a significant amount of time developing .NET namespaces for SharePoint. This comprehensive set of namespaces allows you programmatic access to a significant portion of SharePoint 2010 Server and Foundation Server. The SharePoint Services object model is extensive, to say the least. There are more than 30 namespaces within the object model and dozens of classes covering most of the features of SharePoint 2010. The depth and breadth of the architecture makes it impractical to study the object model completely, instead, it is better to use the object model to cover the main portions of the object model that you will find useful. Most Commonly Used SharePoint Object Model Objects Ordered by Number of References to the Object on the Web SharePoint Object Microsoft.SharePoint.SPList Microsoft.SharePoint.SPWeb Microsoft.SharePoint.SPUser Microsoft.SharePoint.SPContext Description A SharePoint list that can be used to modify the contents of a list. A SharePoint site. A SharePoint user. The context of an HTTP request that gives information, such as the current web application, site collection, list, etc. An item in a SharePoint list. A SharePoint site collection. A file in a SharePoint site, for example, a web part page, a file in a folder, or an item in a document library.
1

Microsoft.SharePoint.SPListItem Microsoft.SharePoint.SPSite Microsoft.SharePoint.SPFile

SharePoint 2010 course developed by Hafeez Mohammed [hafeez_itm@yahoo.com]

Most Commonly Used SharePoint Object Model Objects Ordered by Number of References to the Object on the Web SharePoint Object Microsoft.SharePoint.SPField Microsoft.SharePoint.SPQuery Description A column defined for a SharePoint list. A query (in CAML) that can be used to return required list items (SPListItem) Provides a method called RunWithElevatedPrivileges that lets you run code with more permissions than the one the current user has. An IIS web application that has methods and properties for modifying web application settings and administering at the web application level. A SharePoint Farm that has methods and properties for modifying farm settings and administering at the farm level. An exception thrown by SharePoint. The collection of SPItem objects from a SharePoint list (SPList) or query (SPQuery). This object represents SharePoints connection to native unmanaged code. In many cases, if you do not dispose correctly of SharePoint objects, the SPRequest object associated with them will
2

Microsoft.SharePoint.SPSecurity

Microsoft.SharePoint. Administration.SPWebApplication

Microsoft.SharePoint. Administration.SPFarm

Microsoft.SharePoint.SPException Microsoft.SharePoint.SPListItemCollection Microsoft.SharePoint.Library.SPRequest

SharePoint 2010 course developed by Hafeez Mohammed [hafeez_itm@yahoo.com]

Most Commonly Used SharePoint Object Model Objects Ordered by Number of References to the Object on the Web SharePoint Object Description proliferate and will be referred to in error messages. Microsoft.SharePoint.SPFolder Microsoft.SharePoint.Utilities.SPUtility A SharePoint folder in a list or document library. A utility class that provides useful methods such as SendEmail used to send an e-mail from a SharePoint site A query that can act against multiple lists in multiple sites in the same site collection.

Microsoft.SharePoint.SPSite-DataQuery

SharePoint 2010 course developed by Hafeez Mohammed [hafeez_itm@yahoo.com]

Getting Started with the SharePoint 2010 Object Model In order to get started with the SharePoint object model, you must set a reference to the appropriate assemblies in Visual Studio. For most of the basic operations, you will generally need to set a reference to the Microsoft.SharePoint.dll assembly. The simplest way to deploy ASPX pages is to create them in a single file and save them to the LAYOUTS directory. When you create ASPX files this way, however, the ASP.NET code must be written inline without the advantages of the normal code-behind model, when you place all of your code in a single file, you must include the assembly references and namespaces imports at the top of the file. Additionally, you should reference the application.master or default.master file so that your pages take on the same look as other pages in SharePoint and you can make use of the content placeholders defined within the master page. Programmatically accessing Site Collections and Sites Accessing objects in the SharePoint model is accomplished in a manner similar to any hierarchical object model you may have worked with in the past. The key to navigating such a model is to find the starting point for the model. In SharePoint, this is done through the ASP.NET Context object using the following code: SPSite site = SPControl.GetContextSite(Context); The SPControl class is a member of the Microsoft.SharePoint.WebControls namespace and is the base class from which all other WebControls in the namespace are created. You do not have to create an instance of the SPControl class to use it. Simply call the GetContextSite method and pass the Context property. The Context property comes from the System.Web.UI.Page object and is always available to web parts and ASPX pages that reside in the LAYOUTS directory. The GetContextSite method returns an SPSite object, which represents the site collection where the web part or ASPX page is currently running. SPSite objects contain information about the site collection and the sites within it. In order to access any particular site in the collection, you must return a collection of SPWeb objects. You may then access the individual web sites by enumerating them or accessing one directly through an index.
SharePoint 2010 course developed by Hafeez Mohammed [hafeez_itm@yahoo.com] 4

It is one of those oddities of naming conventions that a site collection is represented by an SPSite object, while a site is represented by an SPWeb object. This naming convention goes back to previous versions of SharePoint where site collections were sometimes called sites and sites were sometimes called webs. Over time, these naming conventions have lost favor, but the object model retains the names for consistency. If you want to make use of object model code in an ASPX page, you must add the code to the PlaceHolderMain placeholder associated with the application master file. When you do this, you simply place the code directly in the ASPX page surrounded by the <% %> delimiters. Code below shows an ASPX page that enumerates the sites in a collection using an ASPX page. You can take this page, save it to the LAYOUTS directory and access it through the browser with an address of the format http://[server]/ [site]/_layouts/[pagename].aspx
<%@ Page Language="C#" MasterPageFile="~/_layouts/application.master"%> <%@ Assembly Name="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71E9BCE111E9429C" %> <%@ Import Namespace="Microsoft.SharePoint" %> <%@ Import Namespace="Microsoft.SharePoint.WebControls" %> <asp:Content ID="Content4" runat="server" ContentPlaceHolderID="PlaceHolderMain"> <% Response.Write("<h2>Welcome to SharePoint Object Model</h2>" + "<p/>"); Response.Write("<h3>Displaying All Sites</h3>" + "<br/>"); SPSite siteCollection = SPControl.GetContextSite(Context); SPWebCollection sites = siteCollection.AllWebs; foreach (SPWeb site in sites) { Response.Write(site.Title + "<br/>"); } %> </asp:Content>

SharePoint 2010 course developed by Hafeez Mohammed [hafeez_itm@yahoo.com]

Accessing Lists and List Items Along with site collections, you will access lists and list items frequently. Typically when dealing with lists, you are interested in a particular site rather than a site collection. You can get a reference to an individual site by using the GetContextWeb method of the SPControl object. Once a site is open, you may access all of the lists it contains through the SPListCollection object. The collection contains an SPList object for every list on the web site. It is important to understand that SharePoint considers almost everything to be a list. This includes not only obvious components such as task lists, but more subtle components such as document libraries and discussion forums. Therefore, you will find it useful to be able to differentiate between various lists that are returned in code. Each SPList object has a BaseType property that returns an SPBaseType enumeration specifying what kind of list is represented. Here is a list of the members of the SPBaseType enumeration:

SPBaseType.DiscussionBoard SPBaseType.DocumentLibrary SPBaseType.GenericList SPBaseType.Issue SPBaseType.Survey SPBaseType.UnspecifiedBaseType

Once you have accessed a list of interest, you may subsequently access the items in the list. Each item in the list is represented by an SPListItem object contained in an SPListItemCollection object. Regardless of whether you are accessing sites, lists, or items, each object has a set of properties and methods that are meaningful. Typically, this means returning the Name, Title, or URL associated with an object. Additionally, each object has some special properties and methods designed to return useful collections. For example, you can return just the webs associated with the current user by utilizing the GetSubwebsForCurrentUser method of the SPWeb class.

SharePoint 2010 course developed by Hafeez Mohammed [hafeez_itm@yahoo.com]

Accessing User Information When iterating through sites and lists, you quite often want to know how they apply to the current user. You may be interested in knowing what role the current user has on a site or what items in a list are assigned to the current user. You can access this information using an SPUser object. The following code shows how to return the SPUser object that represents the current user: SPSite siteCollection = SPControl.GetContextSite(Context); SPWeb site = siteCollection.OpenWeb(); SPUser user = site.CurrentUser; Once the SPUser object is returned, you can retrieve the logon name of the user through the LoginName property. You can also retrieve the display name for the user through the Name property. Because list assignments are made using these values, you can often determine which items in a list belong to the current user by comparing the Assign To field of a list item to these values.
<%@ Page Language="C#" MasterPageFile="~/_layouts/application.master"%> <%@ Assembly Name="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71E9BCE111E9429C" %> <%@ Import Namespace="Microsoft.SharePoint" %> <%@ Import Namespace="Microsoft.SharePoint.WebControls" %> <asp:Content ID="Content4" runat="server" ContentPlaceHolderID="PlaceHolderMain"> <% Response.Write("<h2>Welcome to SharePoint Object Model</h2>" + "<p/>"); Response.Write("<h3>Displaying All Tasks Assigned To The Logged-In User</h3>" + "<br/>"); SPSite siteCollection = SPControl.GetContextSite(Context); SPWeb site = siteCollection.OpenWeb(); SPUser user = site.CurrentUser; SPListCollection lists = site.Lists; Response.Write("All Tasks for " + user.Name + "<br/>");

SharePoint 2010 course developed by Hafeez Mohammed [hafeez_itm@yahoo.com]

foreach (SPList list in lists) { if (list.BaseType == SPBaseType.GenericList || list.BaseType == SPBaseType.Issue) { for (int i = 0; i <= list.ItemCount - 1; i++) { try { SPListItem item = list.Items[i]; string assignedTo = item["Assigned To"].ToString().ToUpper(); if(assignedTo.IndexOf(user.LoginName.ToUpper()) > -1 || assignedTo.IndexOf(user.Name.ToUpper()) > -1) { } catch { } } } } site.Close(); siteCollection.Close(); %> </asp:Content> Response.Write(item.Title + "</br>"); }

SharePoint 2010 course developed by Hafeez Mohammed [hafeez_itm@yahoo.com]

Вам также может понравиться