c# - 通过 C# 将 Sitecore 项目 Url 设置为第三方服务器

标签 c# sitecore

在特定情况下,我必须通过来自云服务器的 RESTful 请求获取某些数据,然后将它们与正常的 Sitecore 项目一起处理和呈现。我通过以下代码将数据从 RESTful 请求转换为 Sitecore Item:

 private static Item Itemize(HubSpotResult hubSpotResult)
        {

            Database webDb = Sitecore.Configuration.Factory.GetDatabase("web");

            TemplateItem template = webDb.GetItem("/sitecore/templates/User Defined/Pages/HubSpotBlogs");
            var id = ID.NewID;
            var templateId = template.ID;
            var titleFieldId = ID.NewID;
            var dateFieldId = ID.NewID;
            var navigationTitleFieldId = ID.NewID;
            var def = new ItemDefinition(id,"HubSpotBlog", templateId, ID.Null);

            var fields = new FieldList();
            fields.Add(titleFieldId, "Title");
            fields.Add(dateFieldId, "Date");
            fields.Add(navigationTitleFieldId, "NavigationTitle");
            var data = new ItemData(def, Language.Parse("en"),new Sitecore.Data.Version(1), fields);
            var dateTime = GetPublicationDate(hubSpotResult.publish_date).ToString();
            var sitecoreStyleDateTime = DateUtil.ParseDateTime(dateTime,DateTime.Now);
            Item item;
            using (new SecurityDisabler())
            {
                item = new Item(id, data, webDb);
                item.Editing.BeginEdit();
                item.Fields["Date"].Value =DateUtil.ToIsoDate(sitecoreStyleDateTime.Date);
                item.Fields["Title"].Value = hubSpotResult.html_title;
                item.Fields["NavigationTitle"].Value = hubSpotResult.html_title;
                Sitecore.Data.Fields.LinkField link = item.Fields["NavigationTitle"];

                link.Url = hubSpotResult.url;
                link.Text = hubSpotResult.html_title;
                item.Editing.EndEdit();
            }


            return item;
        }

目前,我无法获取以这种方式创建的项目的 Url,因为 Sitecore 树上不存在这些项目,但是要呈现链接,我需要将项目的 Url 设置为指向到云服务器。 有没有一个好的方法来设置 Url,以便 LinkManager.GetItemUrl(item) 可以获取云服务器的 url,并呈现链接或项目的标题?

最佳答案

执行此操作的方法是使用自定义的 LinkProvider 覆盖 LinkProvider。当它看到基于您用来创建“虚拟”项目的模板的项目时,您可以使用该项目上的数据来确定要创建的链接应该是什么。如果该项目不属于该模板,那么您可以将其传递给基本 LinkProvider

public class MyLinkProvider : Sitecore.Links.LinkProvider
{
    public override string GetItemUrl(Item item, UrlOptions options)
    {

        var templateID = new ID("<your template Id >");

        if (item == null)
        {
            return string.Empty;
        }

        Assert.ArgumentNotNull(options, "options");

        if (item.TemplateID == templateID) 
        {
            // Build your custom url here
            return "custom url";
        }
        else 
        {
            return base.GetItemUrl(item, options);
        }
    }
}

然后通过包含文件修补它:

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
    <sitecore>
        <linkManager>
            <providers>
                <add name="sitecore">
                    <patch:attribute name="type">MyApplication.MyLinkProvider, MyApplication</patch:attribute>
                </add>
            </providers>
        </linkManager>
    </sitecore>
</configuration>

这将使普通项目以标准 Sitecore 方式生成,并且您可以选择如何构建项目链接。

关于c# - 通过 C# 将 Sitecore 项目 Url 设置为第三方服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39835271/

相关文章:

c# - 选定区域以剪切图像

c# - 在可扩展工厂中使用泛型?

asp.net - Sitecore 用于创建项目的默认语言

javascript - Sitecore 7.2 在添加到组件区域时在组件上触发 JavaScript

c# - 具有多个来源的 Sitecore 下拉列表

c# - 从窗口类名获取窗口句柄

C#如何通过标签数组在字典键中搜索

c# - C# 中的字符串格式化以获得相同的间距

javascript - Sitecore 富文本编辑器 - 添加按钮

sitecore - 浏览 Sitecore 内容树时出现 NullReferenceException