Technical Fun

Archive for April 2008

As usual I have a very unimportant need of getting the list of every site collections that a user has permissions on my company’s MOSS 2007 environment. The list is then to be passed to another application, for other not so important cause.

My company’s site collection structure looks like:
– etc ….

So a the list needs to list those site where a user got permission to access. (user A –> site01, site02, … user B –> site01, etc)

So, assuming that each site collection defines user permissions directly (not inside a group, which happens to be the case in my environment), I wrote this cheap thing…

public List<string> GetValidSites(string sitecollectionname, string userloginname) { SPSite sitecol = new SPSite(sitecollectionname); List<string> siteList = new List<string>(); foreach (SPSite s in sitecol.WebApplication.Sites) { if (s.Url.Contains("/sites/")) { if (IsAllowedFor(userloginname, s.Url)) { siteList.Add(s.Url); } } } return siteList; }

You get the dumbest idea just by looking at it, right? Next, we dig further;

private bool IsAllowedFor(string userloginname, string siteurl) { bool result = false; SPSite site = new SPSite(siteurl); SPWeb web = site.OpenWeb(); try { SPUser usr = web.AllUsers[userloginname]; result = true; } catch { result = false; } return result; }

Host it on web service or DLL, or whatever.
Then, you can call something like this on your client app:


Happy? I am .. 🙂