Tuesday, October 13, 2015

EpiServer - Find Any Descendants Of <T> (Type Generic)

Easy To use
  public static IList<T> GetDescendants<T>(this ContentReference pageLink, ILanguageSelector languageSelector) where T : PageData  
     {  
       if (pageLink.CompareToIgnoreWorkID(ContentReference.RootPage))  
       {  
         throw new NotSupportedException("The root page cannot be converted to type " + typeof(T).Name);  
       }  
       IList<T> descendants = new List<T>();  
       var descendantsReferences = ContentRepository.GetDescendents(pageLink).ToList();  
       if (!descendantsReferences.IsNullOrEmpty())  
       {  
         foreach (var pageRef in descendantsReferences)  
         {  
           var page = languageSelector == null ? pageRef.ToPageReference().GetPage<T>() : pageRef.ToPageReference().GetPage<T>(languageSelector);  
           if (page != null)  
             descendants.Add(page);  
         }  
       }  
       return descendants;  
     }  

  

IEnumerable<PageData> test5 = _repo.GetChildren<PageData>(ContentReference.RootPage);  
       foreach (PageData pageData in test5)  
       {  
         FindDescendantsOfType<PuffPage>(pageData, descendantsType);  
       }

  private static void FindDescendantsOfType<T>(PageData page, ICollection<T> descendantsType) where T : class  
     {  
       var children = ServiceLocator.Current.GetInstance<IContentLoader>().GetChildren<PageData>(page.PageLink);  
       foreach (var child in children)  
       {  
         if (child is T)  
         {  
           descendants.Add(child as T);  
         }  
         FindDescendantsOfType(child, descendants);  
       }  
     }  

Get Children Of Type


 public static IEnumerable<SitePageData> GetChildrenOfType<T>(SitePageData currentPage, ContentReference topNode)  
       where T : SitePageData  
     {  
       var pages = ContentLoader.GetChildren<T>(topNode);  
       return FilterForVisitor  
         .Filter(pages)  
         .OfType<SitePageData>()  
         .Where(x => x.IsVisibleOnSite() && x.VisibleInMenu);  
     }  

Get Items Inkluding Top node


 public static List<NavigationItem> GetNavigationItemsInkludingTopNode(SitePageData currentPage, ContentReference topNode)   
     {  
       var topPage = ContentLoader.Get<SitePageData>(topNode);  
       var topLevelPages = GetChildrenOfType<SitePageData>(currentPage, topNode);  
       var pages = topLevelPages.ToList();  
       pages.Insert(0,topPage);  
       return GetNavItemList(pages, currentPage).ToList();  
     }  



 protected static void GetDescendantsOfType<T>(PageData page, ICollection<T> descendants) where T : class
        {
            var contentRepository = ServiceLocator.Current.GetInstance<IContentRepository>();
            var children = contentRepository.GetChildren<PageData>(page.ContentLink);
            foreach (var child in children)
            {
                if (child is T)
                {
                    descendants.Add(child as T);
                }
                GetDescendantsOfType(child, descendants);
            }
        }

C# - Strip prefixes from link method


   /// <summary>  
     /// Strips your link with array of prefixes to remove.  
     /// </summary>  
     /// <param name="link"></param>  
     /// <param name="prefixesToStrip"></param>  
     /// <returns></returns>  
     public static string StripLinkPrefixes(string link, string[] prefixesToStrip)  
     {  
       var _link = link.ToLower().Trim();  
       foreach (var prefixtoStrip in prefixesToStrip)  
       {  
         _link = _link.StartsWith(prefixtoStrip.ToLower().Trim()) ? _link.Substring(prefixtoStrip.Length) : _link;  
       }  
       return _link;  
     }  

Monday, October 12, 2015

Simple ASP.NET Output Examples



From view to var:
 var tab = Server.HtmlDecode(string.Join("", Enumerable.Repeat("&nbsp;", 4)));  



 <%= CurrentBlock.LinkUrl == null ? string.Empty : Server.HtmlEncode("</a>") %>   

Friday, September 25, 2015

SEO Javascript - Calculate Title Google Pixel Length

Optimal
  1. Best practice: Maximum title is between 466px and 469px (depending which browser & OS)

Code Example here:
https://jsfiddle.net/2at25hjh/

Html:

 <script src="https://code.jquery.com/jquery-2.1.4.js"></script>  
  <title>SEO Calculate The Title Google Pixel Length</title>  
 <body>  
 <input type="text" id="title" size="50" placeholder="Enter your page title here...">  
  <div id="placeholder"></div>  
 </body>  

Javascript:


 $(document).ready(function(){  
      $("#title").change(function(){  
           $(this).getGoogleWidth( $(this) );  
      });  
      $.fn.getGoogleWidth = function(obj){  
           var text = obj.val();  
           var google_css = 'color: #12C;cursor: pointer;display: inline;font-family: arial, sans-serif;font-size: 16px;font-weight: normal;height: auto;line-height: 19px;list-style-image: none;list-style-position: outside;list-style-type: none;margin-bottom: 0px;margin-left: 0px;margin-right: 0px;margin-top: 0px;overflow-x: visible;overflow-y: visible;padding-bottom: 0px;padding-left: 0px;padding-right: 0px;padding-top: 0px;text-align: -webkit-auto;text-decoration: underline;text-overflow: clip;visibility: visible;white-space: nowrap;width: auto;';       
                google_css = google_css.split(";");  
           var placeholder;  
           if( $("#placeholder").size() == 0 ){  
                placeholder = $('<div id="placeholder"/>');  
                obj.after(placeholder);  
           } else {  
                placeholder = $("#placeholder");  
           }  
           // Make new obj  
           var newObj = $("<span/>");  
                newObj.html(text);  
           // add style  
           for(var i=0; i<google_css.length; i++)  
           {  
                var c = google_css[i].split(":");                 
                //If we have values then add  
       if( c[0]!="" && c[1]!="" ){   
         newObj.css(c[0],c[1]);   
       }  
           }  
           placeholder.append( newObj );  
           var width      = newObj.width();  
           placeholder.append(" (width: "+ width +"px) <br />");  
      };  
 });  

Thursday, September 17, 2015

Nuget - Update all Nugets command

Get Nuget package update after new solution fetch
Command:
Update-Package -Reinstall

Monday, August 17, 2015

Visual Studio - Fix slow symbol loading in debug

Here is how I solved the "slow symbol loading" problem in Visual Studio 2012:

Go to Tools -> Options -> Debugging -> General

CHECK the checkmark next to "Enable Just My Code".

Go to Tools -> Options -> Debugging -> Symbols

Click on the "..." button and create/select a new folder somewhere on your local computer to store cached symbols. I named mine "Symbol caching" and put it in Documents -> Visual Studio 2012.

Click on "Load all symbols" and wait for the symbols to be downloaded from Microsoft's servers, which may take a while. Note that Load all symbols button is only available while debugging.

UNCHECK the checkmark next to "Microsoft Symbol Servers" to prevent Visual Studio from remotely querying the Microsoft servers.

Click "OK".

From now on, symbol loading should be much faster.

Note that if you make any changes/downloads to Microsoft assemblies, you may need to go back into the Symbols dialog box and "Load all symbols" again.

Wednesday, August 5, 2015

C# - Test - Accessing Web.config in Test Context

When having for instance a unit test in one project and the need to get settings from web.config in another project. We can actually copy the web.config when building the unit test project to retrieve the properties like this. Insert this in Build Events in project settings.
 copy "$(SolutionDir)\Somesite.Web\Web.config" "$(ProjectDir)$(OutDir)$(TargetFileName).config"