The sites in my site collection are using a custom property to determine what "type" of site they are. E.g.

web.Properties["SiteCategory"] = "SomeCategory1";

I'm planning on creating a web part to show all the sites in my site collection that have custom property.

Here's a snippet of what I'm planning on doing:

SPSite site = SPContext.Current.Site;
foreach (SPWeb web in site.AllWebs)
  if (web.Properties["SiteCategory"] == "SomeCategory1") {
     /* Insert code to display the current site URL on the web part */

Would the result be security trimmed for the current user? Or would it just show all the sites in the site collection? What would I need to make sure it is security trimmed?

Also, performance-wise, is this a viable option? If you have some other way to do this, I'm all ears.

  • You want to show the sites which has this web properties as well as user has permissions on it? – Aanchal Jun 18 '14 at 4:41

Use the following code:

using(SPSite site = new SPSite("http://example/site/"))
    using (SPWeb web = site.OpenWeb())
        SPWebCollection webCollection = web.GetSubwebsForCurrentUser();

            foreach (SPWeb web in webCollection)
                if (web.Properties["SiteCategory"] == "SomeCategory1")
| improve this answer | |
  • GetSubwebsForCurrentUser() is exactly what I'm looking for! Thanks! – kei Jun 18 '14 at 13:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.