Farhin Shaikh
Farhin Shaikh

Reputation: 225

Episerver :Get properties of link to generate an absolute Url

I have an existing block type that has a property of type PageReference that points to internal pages.

The requirement is to replace the existing property with a Url property that can point to either internal or external content.

For this I have created a schedule job to migrate the property value for existing blocks in the production environment.

I am able to get the instances of the block type. I need to set the newly added Url property's value to the page being referenced by the old PageReference property.

//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);

Now I want to iterate over the block instances and set the Url property.

Upvotes: 1

Views: 732

Answers (2)

Farhin Shaikh
Farhin Shaikh

Reputation: 225

        //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);
            }
        }
        
         

Upvotes: 0

Ted Nyberg
Ted Nyberg

Reputation: 7391

If your old property is a PageReference property, you should be able to convert it to a permanent link and assign it to your new Url property (implicit cast of string to Url is supported).

Something like the following:

YourContent.NewUrlProperty = 
    ServiceLocator.Current.GetInstance<IPermanentLinkMapper>()               
    .Find(YourContent.OldPageReferenceProperty)
    .PermanentLinkUrl

Upvotes: 1

Related Questions