Episerver :获取 link 的属性以生成绝对 Url

Episerver :Get properties of link to generate an absolute Url

我有一个现有的块类型,它的 属性 类型 PageReference 指向内部页面。

要求将现有的 属性 替换为可以指向内部或外部内容的 Url 属性。

为此,我创建了一个计划作业来迁移生产环境中现有块的 属性 值。

我能够获取块类型的实例。我需要将新添加的 Url 属性 的值设置为旧 PageReference 属性.

引用的页面
//implementation
var contentTypeRepository = ServiceLocator.Current.GetInstance<EPiServer.DataAbstraction.IContentTypeRepository>();
var contentModelUsage = ServiceLocator.Current.GetInstance<IContentModelUsage>();
var repository = ServiceLocator.Current.GetInstance<IContentRepository>();

//Teaser Item Block
var blockItem = contentTypeRepository.Load(typeof(TeaserItemBlock));

// get usages, also includes versions
IList<ContentUsage> usages = contentModelUsage.ListContentOfContentType(blockItem);

现在我想遍历块实例并设置 Url 属性。

如果您的旧 属性 是 PageReference 属性,您应该能够将其转换为永久 link 并将其分配给您的新 [=12] =] 属性(支持将字符串隐式转换为 Url)。

类似于以下内容:

YourContent.NewUrlProperty = 
    ServiceLocator.Current.GetInstance<IPermanentLinkMapper>()               
    .Find(YourContent.OldPageReferenceProperty)
    .PermanentLinkUrl
        //implementation
        var contentTypeRepository = ServiceLocator.Current.GetInstance<EPiServer.DataAbstraction.IContentTypeRepository>();
        var contentModelUsage = ServiceLocator.Current.GetInstance<IContentModelUsage>();
        var repository = ServiceLocator.Current.GetInstance<IContentRepository>();

        //Teaser Item Block
        var blockItem = contentTypeRepository.Load(typeof(TeaserItemBlock));

        // get usages, also includes versions
        IList<ContentUsage> usages = contentModelUsage.ListContentOfContentType(blockItem);


        foreach (var block in usages.Select(contentUsage => contentUsage.ContentLink.ToReferenceWithoutVersion()).Distinct().Select(contentReference => repository.Get<TeaserItemBlock>  (contentReference)))
        {

            if (!string.IsNullOrEmpty(block.GetPropertyValue("Link")))//This is the Page reference property name
            {
                    var writableCloneBlockItem = (TeaserItemBlock)block.CreateWritableClone();

                    // Creating a page reference by the page id
                    PageReference pageReference = new PageReference(block.Link);
                    
                    // Creating instance for "ContentLoader" class by ServiceLocator - dependency injection
                    IContentLoader contentLoader = ServiceLocator.Current.GetInstance<IContentLoader>();

                    // Getting the actual requested page
                    PageData requstedPage = contentLoader.Get<PageData>(pageReference);
                                        
                    writableCloneBlockItem.Url = requstedPage.LinkURL;
                    writableCloneBlockItem.Property["Link"].Value = null;
                    repository.Save((IContent)writableCloneBlockItem, EPiServer.DataAccess.SaveAction.Publish, EPiServer.Security.AccessLevel.Read);
            }
        }