Security trimmed results for custom query on property
-
28-09-2020 - |
Question
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.
Solution
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")
{}
}
}
}
Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange