This sample project works for both SharePoint 2007 and 2010!
This project is for educational purposes and is to be used at your own risk…
Display a list of all sites a user has rights to…
The following is a “demo” project to show the workings of several SharePoint API features:
- Iterating through all sites in a farm
- Checking user rights
- Using an application page and the application master page
- and using a SharePoint grid control (SPGridView)
What’s interesting is that the majority of the code ended up supporting the SPGridView!
With only a change to the master page (application.master to applicationV4.master) it looks like this in SharePoint 2010 (beta).
Deploy the two files to the LAYOUTS folder on each web front end server. Best practice is to create a folder with a unique name and put your files there. Something like:
C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\LAYOUTS\techtrainingnotes\mysites\
Then you can test the pages from any site like this:
Here’s the ASPX file. Just two content tags, a dropdown list, two labels and the grid control. Note the @Register for Microsoft.SharePoint.WebControls needed for the SPGridView control.
For SharePoint 2010 you should change application.master to applicationV4.master.
Here’s the code for that finds the sites the user has access to. The code that does to real work here is pretty simple. The key methods are:
- GetSiteCollectionSites(SPSite) – this iterates through the web.AllWebs collection of the SPSite and checks to see if the user has at least the “ViewPages” permission
- GetApplicationSites(SPWebApplication) – this iterates thorough the Sites collection and calls GetSiteCollectionSites for each site found
- GetFarmSites() – this iterates through all of the WebApplictions in the farm and calls GetApplicationSites for each SPWebApplication found. (Note that this requires higher permissions that most users have so it has to be run using RunWithElevatedPrivileges
The rest of the code is used to setup the SPGridView.
If I get the time… I’ll post a longer version of this that adds sorting, paging, a menu bar and some other bells and whistles .