Issue with permanentlinkstore

Vote:
 
var url = new UrlBuilder(slide.Href);

           if (PermanentLinkMapStore.ToMapped(url))
            {

                var cr = PermanentLinkUtility.GetContentReference(url);
            }

The above code gives warning that PermanentLinkUtility.GetContentReference(UrlBuilder)' is obsolete: 'This method only supports classic/mapped url's which are no longer used since 8.0 - use PermanentLinkUtility.GetGuid(url) to get the unique ID from an URL'

When i try to change if condition like

if (PermanentLinkUtility.GetGuid(url))

Then it gives error Cannot convert EpiServer.UrlBuilder to string

What is alternative to this?

I am on Episerver 9.2

#174760
Feb 03, 2017 12:04
Vote:
 

What about using UrlResolver

var content = UrlResolver.Current.Route(new UrlBuilder(slide.Href));

return content != null ? content.ContentLink : ContentReference.EmptyReference;

#174777
Feb 03, 2017 15:37
Vote:
 

I am getting error Cannot implicitly convert type Episerver.core.contentreference to system.collection.genericelist<episerver.core.pagedata>

on this line

return content != null ? content.ContentLink : ContentReference.EmptyReference;
#174784
Feb 03, 2017 16:05
Vote:
 

My whole code is like this

public static List<PageData> ToPages(this LinkItemCollection linkItemCollection)
        {
            var pages = new List<PageData>();
            if (linkItemCollection == null) return pages;
            

            foreach (LinkItem linkItem in linkItemCollection)
            {
                string linkUrl;
                               
                
                if (!PermanentLinkMapStore.TryToMapped(linkItem.Href, out linkUrl))
                    continue;

                if (string.IsNullOrEmpty(linkUrl))
                    continue;

                var pageReference = PageReference.ParseUrl(linkUrl);

                if (PageReference.IsNullOrEmpty(pageReference))
                    continue;

                pages.Add(DataFactory.Instance.GetPage((pageReference)));


            }

            return pages;
        }
#174785
Feb 03, 2017 16:11
Vote:
 
Try this

public static List<T> ToPages<T>(this LinkItemCollection linkItemCollection) where T : PageData
{
var pages = new List<T>();

if (linkItemCollection == null || linkItemCollection.Count.Equals(0))
return new List<T>();

var urlResolver = ServiceLocator.Current.GetInstance<UrlResolver>();


foreach (var linkItem in linkItemCollection)
{
if (String.IsNullOrEmpty(linkItem.Href))
continue;


var urlBuilder = new UrlBuilder(linkItem.Href);

var page = urlResolver.Route(urlBuilder) as T;

if (page != null && (!page.IsDeleted && page.CheckPublishedStatus(PagePublishedStatus.Published)))
{
pages.Add(page);
}

}
#174787
Feb 03, 2017 16:17
Vote:
 

I get error
Error CS0161 'LinkCollectionExtentions.ToPages<T>(LinkItemCollection)': not all code paths return a value

Looks like we are not returning anything

#174788
Feb 03, 2017 16:26
Vote:
 

ok just returned pages and got rid of error. Will try and let you know

#174789
Feb 03, 2017 16:42
Vote:
 

My other class referencing this is throwing error now.

  protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            var lic = Persons.Value as LinkItemCollection;

            repeatPerson.DataSource = lic.ToPages(); // this code throws error
            repeatPerson.DataBind();
        }

Any idea?

#174790
Feb 03, 2017 16:56
Vote:
 

I think you need to do like this

 repeatPerson.DataSource = lic.ToPages<PageData>(); // this code throws error
            repeatPerson.DataBind();
#174798
Feb 03, 2017 19:11
Vote:
 

Just last one thing. What is replacement for below

 var url = new UrlBuilder(linkItem.Href);
                if (PermanentLinkMapStore.ToMapped(url)) //get warning here
#174800
Feb 03, 2017 22:41
Vote:
 

This would be the equivalent

var page = urlResolver.Route(urlBuilder) as T;

Let urlresolver make the mapping between internal objekt and inernal in a linklist.

#174807
Feb 05, 2017 1:09
Vote:
 

Here is my code where i tried to use your suggestion. how we make use of page variable to replace permanentlinkmapstore?

protected string Mymethod(object dataItem)
        {
            var linkItem = dataItem as LinkItem;
            if (linkItem == null) return "";

            try
            {
                var url = new UrlBuilder(linkItem.Href);
                var urlResolver = ServiceLocator.Current.GetInstance<UrlResolver>();
                var page = urlResolver.Route(url) as PageData;

                
                if (PermanentLinkMapStore.ToMapped(url))
                {
                    var success = EPiServer.Global.UrlRewriteProvider.ConvertToExternal(url, null, Encoding.UTF8);
                    return url.ToString();
                }

                return linkItem.Href;
            }
            catch (Exception)
            {
                //some logic            }
            
        }
#174808
Feb 05, 2017 10:54
Vote:
 

Try this

protected string Mymethod(object dataItem)
{
			var linkItem = dataItem as LinkItem;
            if (linkItem == null) return "";
			
			var content = UrlResolver.Current.Route(new UrlBuilder(linkItem.Href));
            return content != null ? UrlResolver.Current.GetUrl(content.ContentLink) : linkItem.Href;

}
#174815
Feb 06, 2017 7:26
Vote:
 

Thanks a lot. How to mark as answer? I dont see any option. It seem only i mark my replies are answers.

#174816
Feb 06, 2017 8:22
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.