SITECORE Multi-Sites with Shared Pages
Overview
SITECORE support
having multiple website in a single instance, every site has it is own pages,
content, etc... And you can add shared
content items between all sites as you can see in the below screenshot:
That’s awesome!
But the challenge comes when you need to have shared pages between more than
one sites. Don’t worry it is doable, but we need to do some coding here.
What to do?
I am supposing
that you know how to configure multiple website in the normal scenario, if you
don’t, please check out this document by Sitecore.
After configuring
our sites we need to go through the below steps to have the shared pages
working fine:
1)
We need to
override “GetItemUrl” method to update the URLs for the items under shared
content to be formed the same as the links under the site itself.
We need to be able to browse the pages under the shared
folder without include “/Global/” in the URL, for example if we are on “site1”
(suppose that the URL is http://site1) and we
want to browse this page “/sitecore/content/Global/Products/Product1” then the
URL should be this http://site1/Products/Product1 instead of
http://site1/Global/Products/Product1
and
if we are on “Site2” the URL
should be this http://site2/Products/Product1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class CustomLinkProvider : Sitecore.Links.LinkProvider | |
{ | |
/// <summary> | |
/// Gets the item Url. | |
/// </summary> | |
/// <param name="item">The item.</param> | |
/// <param name="options">The options.</param> | |
/// <returns></returns> | |
public override string GetItemUrl(Sitecore.Data.Items.Item item, Sitecore.Links.UrlOptions options) | |
{ | |
var linkBuilder = new LinkBuilder(options); | |
string str = linkBuilder.GetItemUrl(item); | |
return str; | |
} | |
/// <summary> | |
/// Gets the item URL. | |
/// </summary> | |
/// <param name="item">The item.</param> | |
/// <returns></returns> | |
public string GetItemUrl(Sitecore.Data.Items.Item item) | |
{ | |
return GetItemUrl(item, LinkManager.GetDefaultUrlOptions()); | |
} | |
public new class LinkBuilder : Sitecore.Links.LinkProvider.LinkBuilder | |
{ | |
private UrlOptions Options { get; set; } | |
public LinkBuilder(UrlOptions options) | |
: base(options) | |
{ | |
Options = options; | |
} | |
/// <summary> | |
/// Gets the Url. | |
/// | |
/// </summary> | |
/// <param name="item">The item.</param> | |
/// <returns/> | |
public new string GetItemUrl(Item item) | |
{ | |
return this.BuildItemUrl(item); | |
} | |
/// <summary> | |
/// Builds the item Url. | |
/// | |
/// </summary> | |
/// <param name="item">The item.</param> | |
/// <returns/> | |
protected override string BuildItemUrl(Item item) | |
{ | |
try | |
{ | |
if (item == null) | |
{ | |
return base.BuildItemUrl(item); | |
} | |
Item sharedGlobalItem = Sitecore.Context.Database.GetItem("{4813AC95-D5E9-40AC-938C-DAAEC9FA4A7C}");//here is the ID for Global folder (/sitecore/content/Global/Products) | |
bool matched = false; | |
if (sharedGlobalItem != null && sharedGlobalItem.Axes.IsAncestorOf(item)) | |
{ | |
matched = true; | |
} | |
if (matched) | |
{ | |
if (sharedGlobalItem == null) | |
{ | |
return base.BuildItemUrl(item); | |
} | |
string sharedGlobalItemName = sharedGlobalItem.Name.ToLowerInvariant(); | |
var itemPath = item.Paths.ContentPath.ToLowerInvariant(); | |
var categoryIndex = itemPath.IndexOf(sharedGlobalItemName, StringComparison.InvariantCultureIgnoreCase); | |
if (categoryIndex == -1) | |
{ | |
categoryIndex = Sitecore.MainUtil.DecodeName(itemPath).IndexOf(sharedGlobalItemName, StringComparison.InvariantCultureIgnoreCase); | |
} | |
if (categoryIndex != -1) | |
{ | |
var suffixPath = itemPath.Substring(categoryIndex + sharedGlobalItemName.Length).Replace('/' + sharedGlobalItemName, "/"); | |
itemPath = string.Format("/{0}{1}", sharedGlobalItemName, suffixPath); | |
} | |
SiteInfo siteInfo = this.ResolveTargetSite(item); | |
var url = base.BuildItemUrl(this.GetServerUrlElement(siteInfo), itemPath); | |
return this.Options.LowercaseUrls ? url.ToLowerInvariant() : url; | |
} | |
} | |
catch (Exception ex) | |
{ | |
Log.Error(ex.Message, ex, this); | |
} | |
var baseurl = base.BuildItemUrl(item); | |
return this.Options.LowercaseUrls ? baseurl.ToLowerInvariant() : baseurl; | |
} | |
} | |
} |
2)
After
updating the URL, now we need to process the request and update “Context.Item” value since we don’t have “Products”
item under the sites, and this can be done by adding a new processor to resolve
such kind of request.
To do that we need to add a new processor in the <httpRequestBegin> to be inherited from HttpRequestProcessor class. In this processor we need to update this
property “Context.Item” with the matched item based on the current
request.
So in our above example the request will be:
Request: http://site1/Products/Product1
Context item : Sitecore.Context.Database.GetItem( “/sitecore/content/Global/Products/Product1”)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/" xmlns:set="http://www.sitecore.net/xmlconfig/set/"> | |
<sitecore> | |
<pipelines> | |
<httpRequestBegin> | |
<processor patch:after="processor[@type='Sitecore.Pipelines.HttpRequest.ItemResolver, Sitecore.Kernel']" type="YourNamespace.SharedContentResolver, YourAssembly"> | |
</processor> | |
</httpRequestBegin> | |
</pipelines> | |
<linkManager> | |
<providers> | |
<add name="sitecore"> | |
<patch:attribute name="type">YourNamespace.CustomLinkProvider, YourAssembly</patch:attribute> | |
</add> | |
</providers> | |
</linkManager> | |
</sitecore> | |
</configuration> |
3)
The last
step is make sure that your sitemap.xml is working as expected any update any
code login accordingly.
0 comments:
Post a Comment