Thursday, May 21, 2015

EPiServer - Convert LinkItemCollection to list of Links

    /// <summary>  
     /// Converts a LinkItemCollection to a list of hyper links.  
     /// </summary>  
     /// <param name="linkItemCollection">Link items to be converted.</param>  
     /// <returns>List of hyper links; or an empty one if collection is null or empty.</returns>  
     public static List<HyperLink> ToHyperLinks(LinkItemCollection linkItemCollection)  
     {  
       if (linkItemCollection == null || linkItemCollection.Count <= 0)  
       {  
         return new List<HyperLink>();  
       }  
       List<HyperLink> links = new List<HyperLink>();  
       foreach (LinkItem linkItem in linkItemCollection)  
       {  
         if (linkItem == null || string.IsNullOrEmpty(linkItem.Text) || string.IsNullOrEmpty(linkItem.Href))  
         {  
           continue;  
         }  
         HyperLink link = new HyperLink();  
         link.Text = linkItem.Text;  
         link.NavigateUrl = linkItem.Href;  
         link.ToolTip = linkItem.Title;  
         link.Target = linkItem.Target;  
         links.Add(link);  
       }  
       return links;  
     }  

No comments:

Post a Comment