Tuesday, August 10, 2004

Iterating SharePoint Portal

The client I'm working with would like to have a place where users can see a list of all items that have been submitted and are awaiting approval. These items could be documents in Document Libraries, or perhaps Area Listings. My thought was to create a web part that a user could place on their "My Site" that could iterate all areas of the Portal, and then iterate all lists within each area looking for items that are pending.

It turns out this is much more difficult than it should be. First, look at Listings, you have to reference each area as a Microsoft.SharePoint.Portal.SiteData.Area object. The Area object will give you access to the Listings on the area and you can see items that are Pending. I used code that referenced the Home area, then iterated its Areas collection, like so:


'Get the area GUID for the Home Area
Dim objCurrentAreaGuid As Guid = Microsoft.SharePoint.Portal.SiteData.AreaManager.GetSystemAreaGuid(PortalContext.Current, SystemArea.Home)
'Get an area object for Home
Dim objArea As Area = AreaManager.GetArea(PortalContext.Current, objCurrentAreaGuid)

'Iterate all listings on Home and count Pending Items
Dim areaLists As AreaListingCollection = objArea.Listings
For Each areaList As AreaListing In areaLists
'if the item is pending, output it
If areaList.Status = ListingStatus.Pending Then
'Output the List Item info
End If
Next
'Check all other System Portal Areas (Topics, Sites, etc)
For Each objSiblingArea As Area In objArea.Areas
For Each areaList As AreaListing In objSiblingArea.Listings
If areaList.Status = ListingStatus.Pending Then
'Output the List Item info
End If
Next
Next

The problem is that this only works on the "system" areas (News, Site Directory, Topics) and not on user created areas. User created areas are represented as webs, so a separate loop needs to be done to iterate those, like so:

Dim allSites As SPWebCollection = ParentWeb.GetSubwebsForCurrentUser()
For Each subWeb As SPWeb In allSites
Dim lists As SPListCollection = subWeb.Lists
lists.ListsForCurrentUser = True
For Each list As SPList In lists
If (list.BaseTemplate = SPListTemplateType.DocumentLibrary) Then
Dim docLib As SPDocumentLibrary = CType(list, SPDocumentLibrary)
If doclib.EnableModeration And doclib.Permissions.DoesUserHavePermissions(SPRights.ManageLists) Then
For Each doclibitem As SPListItem In docLibItems
If doclibitem.ModerationInformation.Status = SPModerationStatusType.Pending Then
Dim thisFile As SPFile = doclibitem.File
'output info about this item
End If
Next
End If
End If
Next

Now the problem is that the SPWeb object doesn't give access the listings. I can look at the Document Libraries and other types of lists, but not Portal Listings.

The areas to have Portal Listings in them, but they are not (as far as I can find) accessible as a Portal Area object. So how can I look at the listings? I don't know!

One other thing that I found was that using this code:
Dim allSites As SPWebCollection = ParentWeb.AllWebs()

Will not work if the user is not an Administrator. I searched forever and found this:
Dim allSites As SPWebCollection = ParentWeb.GetSubwebsForCurrentUser()

Which works great. I'm "this" close to getting a great web part built, but have hit a road block here....

No comments: