A critical vulnerability was discovered in React Server Components (Next.js). Our systems remain protected but we advise to update packages to newest version. Learn More

Search part of file name in File Manager

Vote:
 

When searching for files in the File Manager I can search for 'small*' and would find any file starting with 'small', eg 'smallcat.png'. If I need to find every file with the word 'cat' in its filename, however, I can't seem to find a way to do that. 'cat' returns 0 results and so does '*cat' and even '*cat*'.

How do one search for part of a filename?

#60428
Aug 08, 2012 13:00
Vote:
 

Anyone? The question is:

Is it possible to search part of filename using the file manager search?

#60461
Aug 10, 2012 10:19
Vote:
 

You could implement a custom search provider, like this:

    [Export(typeof(ISearchProvider))]
    public class CustomFileSearchProvider : ISearchProvider
    {
        private static ILog _log = LogManager.GetLogger(typeof(CustomFileSearchProvider));

        public string Area
        {
            get
            {
                return "CMS";
            }
        }

        public string Category
        {
            get
            {
                return "Filenames";
            }
        }
        public IEnumerable<SearchResult> Search(Query query)
        {
            var list = new List<SearchResult>();
            foreach (string virtualDir in (IEnumerable<string>)VirtualPathHandler.Instance.VirtualPathToNameMapping.Keys)
            {
                try
                {
                    UnifiedDirectory unifiedDirectory = HostingEnvironment.VirtualPathProvider.GetDirectory(virtualDir) as UnifiedDirectory;
                    if (unifiedDirectory != null)
                    {
                        var filez = unifiedDirectory.Files;
                        var files = new List<UnifiedFile>();
                        GetAllFiles(unifiedDirectory, files);
                        list.AddRange(from file in files where file.Name.Contains(query.SearchQuery) select CreateSearchResult(file.VirtualPath, file.Name));
                    }
                }
                catch (Exception ex)
                {
                    _log.Warn((object)"There was an error when trying to perform a search for filenames.", ex);
                }
            }
            return list;
        }

        private void GetAllFiles(UnifiedDirectory start, List<UnifiedFile> files)
        {
            foreach (UnifiedFile file in start.GetFiles())
            {
                files.Add(file);
            }
            foreach (UnifiedDirectory dir in start.GetDirectories())
            {
                GetAllFiles(dir, files);
            }
        }

        private SearchResult CreateSearchResult(string path, string name)
        {
            return new SearchResult(UriSupport.ResolveUrlFromUIBySettings(string.Format("edit/default.aspx?plugin={0}&selectedfile={1}", (object)PlugInDescriptor.Load("EPiServer.UI.Hosting.VirtualPathControl", "EPiServer.UI").ID, (object)HttpUtility.UrlEncode(path))), name, null)
            {
                IconCssClass = this.GetIcon(name)
            };
        }

        private string GetIcon(string fileName)
        {
            return "epi-resourceIcon epi-resourceIcon-" + Path.GetExtension(fileName).TrimStart(new char[1]
      {
        '.'
      });
        }
    }

    

#60472
Aug 10, 2012 14:07
Vote:
 

Thanks a bunch! I'll try that!

#60473
Aug 10, 2012 14:54
Vote:
 

How can I configure Episerver to use my custom files search provider? Could you point me to any documentation on this?

#60504
Aug 13, 2012 13:24
Vote:
 
#60506
Aug 13, 2012 13:38
This thread is locked and should be used for reference only. Please use the Episerver CMS 7 and earlier versions forum to open new discussions.
* 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.