11

マルチサイト ソリューション (サイト 1 とサイト 2) があり、(カスタムの LinkProvider で) URL を取得しているアイテムが現在のコンテキスト サイトに属しているかどうかを判断できる必要があります ( Sitecore.Context.Site)、または別のサイトの一部です。これを行う良い方法はありますか?

基本的に、アイテムがどのサイトに関連付けられているかを見つけることができればよいだけです。その値と現在のコンテキスト サイトを比較できます。

4

8 に答える 8

10

SiteInfo属するサイトの定義を含むオブジェクトを返す Item クラスの拡張メソッドを作成することをお勧めします。

残念ながら、すべてのコードを含むラップトップをここに持っていないので、Visual Studio に入力してビルドすることを確認しましたが、動作することはかなり確信しています。

public static class Extensions
{
    public static Sitecore.Web.SiteInfo GetSite(this Sitecore.Data.Items.Item item)
    {
        var siteInfoList = Sitecore.Configuration.Factory.GetSiteInfoList();

        foreach (Sitecore.Web.SiteInfo siteInfo in siteInfoList)
        {
            if (item.Paths.FullPath.StartsWith(siteInfo.RootPath))
            {
                return siteInfo;
            }
        }

        return null;
    }
}

これGetSite()で、すべてのオブジェクトでメソッドを呼び出して、そのアイテムItemの を取得できます。それを使用して、たとえば次のようにして、SiteInfo一致するかどうかを確認できます。Sitecore.Context.Site

SiteInfo siteInfo = itemYouNeedToCheck.GetSite();
bool isContextSiteItem = Sitecore.Context.Site.SiteInfo.Equals(siteInfo);

編集:次のように、もっと短くすることもできると思いました:

public static Sitecore.Web.SiteInfo GetSite(this Sitecore.Data.Items.Item itemYouNeedToCheck)
{
    return Sitecore.Configuration.Factory.GetSiteInfoList()
        .FirstOrDefault(x => itemYouNeedToCheck.Paths.FullPath.StartsWith(x.RootPath));
}

だからあなたが一番好きなものを選んでください:)

于 2013-01-07T17:52:05.587 に答える
8
/// <summary>
/// Get the site info from the <see cref="SiteContextFactory"/> based on the item's path.
/// </summary>
/// <param name="item">The item.</param>
/// <returns>The <see cref="SiteInfo"/>.</returns>
public static SiteInfo GetSiteInfo(this Item item)
{
  return SiteContextFactory.Sites
    .Where(s => !string.IsNullOrWhiteSpace(s.RootPath) && item.Paths.Path.StartsWith(s.RootPath, StringComparison.OrdinalIgnoreCase))
    .OrderByDescending(s => s.RootPath.Length)
    .FirstOrDefault();
}
于 2015-10-16T01:32:12.107 に答える
7

Ruud van Falier の回答に賛成し、数回のテストの後、特定のシナリオでのみ機能することに気付きました。投票をキャンセルできなかったので、ここのコードにいくつかの変更を加えました。

    public static SiteInfo GetSite(this Item item)
    {
        var siteInfoList = Sitecore.Configuration.Factory.GetSiteInfoList();

        SiteInfo currentSiteinfo = null;
        var matchLength = 0;
        foreach (var siteInfo in siteInfoList)
        {
            if (item.Paths.FullPath.StartsWith(siteInfo.RootPath, StringComparison.OrdinalIgnoreCase) && siteInfo.RootPath.Length > matchLength)
            {
                matchLength = siteInfo.RootPath.Length;
                currentSiteinfo = siteInfo;
            }
        }

        return currentSiteinfo;
    }

そのため、他のビルトイン サイトは通常、実際のサイト構成に到達する前にコンテンツ パスと一致する「/sitecore/content」のような短いパスを持っているという問題がありました。したがって、このコードは最適な一致を返そうとしています。

于 2015-06-04T00:36:13.097 に答える
3

Sitecore 9.3+ を使用している場合は、IItemSiteResolver代わりに依存性注入を使用することをお勧めします。

IItemSiteResolver _siteResolver;
public MyClass(Sitecore.Sites.IItemSiteResolver siteResolver) {
    _siteResolver = siteResolver;
}

public void DoWork(Item item) {
    Sitecore.Web.SiteInfo site = _siteResolver.ResolveSite(item);
    ...
}
于 2020-10-07T11:13:30.680 に答える
0

これは、私がマルチサイト ソリューションに使用するものです。

FormatWith は、string.Format の単なるヘルパーです。

 public static SiteInfo GetSite(this Item item)
    {
        List<SiteInfo> siteInfoList = Factory.GetSiteInfoList();
        SiteInfo site = null;
        foreach (SiteInfo siteInfo in siteInfoList)
        {
            var siteFullPath = "{0}{1}".FormatWith(siteInfo.RootPath, siteInfo.StartItem);
            if (string.IsNullOrWhiteSpace(siteFullPath))
            {
                continue;
            }
            if (item.Paths.FullPath.StartsWith(siteFullPath, StringComparison.InvariantCultureIgnoreCase))
            {
                site = siteInfo;
                break;
            }
        }
        return site;
    }
于 2013-07-31T20:07:17.920 に答える
0

依存関係を避けるために、単体テストの目的で、この情報を web.config から直接抽出する方法を作成しました。

    public static SiteInfoVM GetSiteInfoForPath(string itemPath)
    {
        var siteInfos = GetSiteInfoFromXml();

        return siteInfos
            .Where(i => i.RootPath != "/sitecore/content" && itemPath.StartsWith(i.RootPath))
            //.Dump("All Matches")
            .OrderByDescending(i => i.RootPath.Length).FirstOrDefault();
    }

    static List<SiteInfoVM> GetSiteInfoFromXml()
    {

        XmlNode sitesNode = Sitecore.Configuration.ConfigReader.GetConfigNode("sites");//.Dump();
        var result = sitesNode.Cast<XmlNode>()
        .Where(xn => xn.Attributes != null && xn.Attributes["rootPath"] != null
        //&& (xn.Attributes["targetHostName"]!=null ||  xn.Attributes["name"].Value)
        )
        .Select(xn => new {
            Name = xn.Attributes["name"].Value,
            RootPath = xn.Attributes["rootPath"].Value,
            StartItem = xn.Attributes["startItem"].Value,
            Language = xn.Attributes["language"] != null ? xn.Attributes["language"].Value : null,
            TargetHostName = (xn.Attributes["targetHostName"] != null) ? xn.Attributes["targetHostName"].Value : null,
            SiteXml = xn.OuterXml
        })
        .Select(x => new SiteInfoVM(x.Name, x.RootPath, x.StartItem, x.Language, x.TargetHostName, x.SiteXml))
        .ToList();
        return result;
    }


    public class SiteInfoVM
    {

        public SiteInfoVM(string name, string rootPath, string startItem, string lang, string tgtHostName, string siteXml)
        {
            Name = name;
            TargetHostName = tgtHostName;
            RootPath = rootPath;
            StartItem = startItem;
            Language = lang;
            SiteXml = siteXml;


        }
        public string Name { get; set; }
        public string RootPath { get; set; }
        public string StartItem { get; set; }
        public string Language { get; set; }
        public string TargetHostName { get;set; }
        public string SiteXml { get; set; }
    }
于 2015-10-16T01:36:28.157 に答える