Accessing Portal Site and User Information


Accessing Portal Site and User Information

One of the major uses of the SharePoint Services object model is to access component parts of a SharePoint installation. Using the object model, you can access any site collection, site, or list on an extended virtual server. You can also identify the current user and access information about the user, associated groups, and assigned roles. Accessing the site and user components of the installation will allow you to create web parts that fill in some of the gaps in SharePoint Services that users will surely encounter.

Consider the scenario where an end user has navigated to the top-level site in a collection. With the top-level site open , the user has no simple way to discover what other sites are contained in the collection. If you could present a list of available sites in the current collection, users would be better able to find what they are interested in. The SharePoint Services object model allows you to provide this view to the user.

Accessing Site Collections

Accessing objects in the SharePoint Services 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 ”or root ”of the model. In SharePoint Services, you can access the navigation root in the hierarchy with one of the following lines of code.

 //C# SPSite thisSite = SPControl.GetContextSite(Context); 'VB.NET Dim objSite As SPSite = 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. In order to use this namespace, you must set a reference to the Microsoft SharePoint Services library. You do not have to create an instance of this class to use it. Simply call the GetContextSite method and pass the Context variable. The Context variable is inherited from System.Web.UI.Page and is always available to web parts and web applications you create in Visual Studio. The GetContextSite method returns an SPSite object, which represents the site collection where the web part is currently running.

SPSite objects represent a site collection as an aggregate object. 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. The following code shows how to enumerate the sites in a collection using C#.

 SPSite thisSite = SPControl.GetContextSite(Context); SPWebCollection webs = thisSite.AllWebs; foreach(SPWeb web in webs) {     //add code here } 

Accessing Lists and List Items

Along with site collections, you will access lists and list items frequently. A significant problem for end users of SPS is that they are typically assigned tasks associated with many different sites. This rapidly results in a situation where end users cannot manage all of the tasks they are assigned and frequently are not even aware that a task exists. Of course, alerts are helpful, but alerts must be created by the end user. You have seen many cases where someone creates a team site and enters tasks on a list, but no one visits the site to create an alert. Therefore, building web parts that help manage task lists is critical to the success of an SPS implementation.

Once a web site is opened, 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. The following code shows how to enumerate the lists for the current web site from which a web part is running.

 SPWeb thisWeb = SPControl.GetContextWeb(Context); SPListCollection spsLists= thisWeb.Lists; foreach(SPList spsList in spsLists) {     //add code here } 

It is important to understand that SharePoint Services considers almost everything to be a list. This includes not only obvious components such as task lists, but more subtle components like 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. Enumerating these list items follows the same pattern as you have already seen.

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. All of these classes, and others, are fully documented in the SharePoint Services SDK available at msdn.microsoft.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 site = SPControl.GetContextSite(Context); SPWeb web = site.OpenWeb(); SPUser user = web.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. Listing 9-8 shows how to look through a collection of lists and identify tasks assigned to the current user.

Listing 9-8: Determining List Item Ownership
start example
 Dim objSite As SPSite = SPControl.GetContextSite(Context) Dim objWeb As SPWeb = objSite.OpenWeb() Dim objUser As SPUser = objWeb.CurrentUser Dim objLists As SPListCollection = objWeb.Lists Dim objList As SPList    'Walk every list on a site    For Each objList In objLists       If objList.BaseType = SPBaseType.GenericList _       OrElse objList.BaseType = SPBaseType.Issue Then          For i As Integer = 0 To objList.ItemCount - 1             Try                Dim objItem As SPListItem = objList.Items(i)                'Check to see if this task is assigned to the user                Dim strAssignedTo As String = _                 UCase(objItem.Item("Assigned To").ToString)                If strAssignedTo.IndexOf(UCase(objUser.LoginName)) > -1 _                OrElse strAssignedTo.IndexOf(UCase(objUser.Name)) > -1 Then                   'Add code here                End If                Catch             End Try          Next       End If    Next objWeb.Close() objSite.Close() 
end example
 



Microsoft SharePoint[c] Building Office 2003 Solutions
Microsoft SharePoint[c] Building Office 2003 Solutions
ISBN: 1590593383
EAN: N/A
Year: 2006
Pages: 92

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net