Reputation: 219
I'm trying to hide items in the Content tree outside of my Site Root item so that they become inaccessible by URL.
Two of my approaches did not work:
The first one was to play with settings in Web.config
<sites>
<site name="website" virtualFolder="/" physicalFolder="/" rootPath="/sitecore/content/home" startItem="/" database="web" .../>
</sites>
This could solve the problem, but LinkManager started to raise exceptions. As startItem is empty, it tries to make a Substring from an empty string, it crashes.
The second try was to implement an ItemNotFound processor so that if my Item is not SiteRoot or its descendant, it sets Context item to null and makes the 404 routine.
public class ItemNotFoundHandler : HttpRequestProcessor
{
public override void Process(HttpRequestArgs args)
{
if (Sitecore.Context.Item != null && Sitecore.Context.Site.Name == "website")
{
Item siteroot = Sitecore.Context.Database.GetItem(Settings.SiteRootID);
if (Sitecore.Context.Item.ID != siteroot.ID && !Sitecore.Context.Item.Axes.IsDescendantOf(siteroot))
//Do404();
Sitecore.Context.Item = null;
}
if (Sitecore.Context.Item != null || Sitecore.Context.Site == null || Sitecore.Context.Database == null)
return;
Do404();
}
private void Do404()
{
Item item404 = Sitecore.Context.Database.GetItem(Settings.Error404ID);
Sitecore.Context.Item = item404;
}
}
This didn't change anything: the Context item stays unchanged. I cannot understand why.
Can you help me out?
Upvotes: 0
Views: 1062
Reputation: 219
Finally I figured it out. I decompiled the Sitecore LinkProvider and saw where the problem was.
The first approach was correct, with only one remark: startItem should be empty.
The "/" symbol is taken into account when calculating rootPath, its length becomes one symbol too big, and it throws OutOfRangException. If I remove this symbol or remove startItem at all, there is no more problem.
<sites>
<site name="website" virtualFolder="/" physicalFolder="/" rootPath="/sitecore/content/home" startItem="" database="web" .../>
</sites>
Upvotes: 1
Reputation: 4410
If you don't want to be able to browse to items outside the Home
node, what you could do is have those items not have any presentation details.
You can then change the setting LayoutNotFoundUrl
to your 404 page:
<setting name="LayoutNotFoundUrl" value="/My404" />
If you do want to have presentation details on those nodes for whatever reason, or if the above doesn't work for any different reason, why not just call your 404 page in the Do404 method?
private void Do404()
{
Item item404 = Sitecore.Context.Database.GetItem(Settings.Error404ID);
Sitecore.Web.WebUtil.Redirect(LinkManager.GetItemUrl(item404));
}
Upvotes: 1