London Dev Meetup Rescheduled! Due to unavoidable reasons, the event has been moved to 21st May. Speakers remain the same—any changes will be communicated. Seats are limited—register here to secure your spot!

How to get a Friendly URL of each image in Asset Library?

Vote:
0

Hi,

Our client would like a Asset library report and would like the report to have a friendly URL generated for each Image and file in the Asset Library in CMS Episerver 6 R2.

Is this at all possible? If so do you know what code is best to use?

Many thanks

JOn

#144028
Feb 04, 2016 15:58
Vote:
0

Scheduled job

Use UnifiedFile and UnifiedDirectory.GetFiles / GetDirectories

http://world.episerver.com/documentation/class-library/?documentId=cms/7/25a9113a-09bc-b669-f00b-976d8a0abe16

UrlRewriteProvider.ConvertToExternal to get paths

see

http://world.episerver.com/Forum/Developer-forum/EPiServer-CMS-6-R2/Thread-Container/2012/9/Absolute-external-URL-property/

Output using EPiServer logging...(log4net)?

#144030
Feb 04, 2016 16:06
Vote:
0

Use a scheduled job instead of a user pressed button to avoid timeouts. 

Set scheduled job to run as an admin kind of user to avoid any access rights problem. E.g.

if (HttpContext.Current == null)
        {
            PrincipalInfo.CurrentPrincipal = new GenericPrincipal(
                new GenericIdentity("Scheduled DummyTask"),
                new[] { "Administrators" });
        }

#144031
Edited, Feb 04, 2016 16:08
Vote:
0
        StringBuilder sb = new StringBuilder();
        sb.AppendLine("Doc Report");
        var files = new List<UnifiedFile>();

        UnifiedDirectory docsDirectory = null;
        UnifiedDirectory globalDirectory = null;
        UnifiedDirectory pageFilesDirectory = null;

        docsDirectory = HostingEnvironment.VirtualPathProvider.GetDirectory("~/Documents/") as UnifiedDirectory;
        this.GetAllFiles(docsDirectory, files);

        globalDirectory = HostingEnvironment.VirtualPathProvider.GetDirectory("~/Global/") as UnifiedDirectory;
        this.GetAllFiles(globalDirectory, files);

        pageFilesDirectory = HostingEnvironment.VirtualPathProvider.GetDirectory("~/PageFiles/") as UnifiedDirectory;
        this.GetAllFiles(pageFilesDirectory, files);

        Int32 iCount = 0;

        foreach (var file in files)
        {
            try
            {
                sb.AppendLine("<a href='https://" + Request.Url.Host + file.VirtualPath + "'>https://" + Request.Url.Host + file.VirtualPath + "</a><br/>");
                iCount += 1;
            }
            catch (Exception exception)
            {
                //logger.ErrorFormat("Error exporting file {0}. Error was: {1}", file.VirtualPath, exception.ToString());
            }
        }
        sb.AppendLine("Total Count: " + iCount.ToString());

        StartIndexOnItemLit.Text = sb.ToString();

}

 void GetAllFiles(UnifiedDirectory start, List<UnifiedFile> files)
    {
        foreach (UnifiedFile file in start.GetFiles())
        {
            if (file.Extension.ToLower() == ".jpg" || file.Extension.ToLower() == ".jpeg" || file.Extension.ToLower() == ".png" || file.Extension.ToLower() == ".gif")
            {
                files.Add(file);
            }
        }
        foreach (UnifiedDirectory dir in start.GetDirectories())
        {
            GetAllFiles(dir, files);
        }
    }
#144034
Feb 04, 2016 16:21
Vote:
0

Yup! :) Sweet! Works?

#144035
Feb 04, 2016 16:29
Vote:
0

Like a charm :) - Many thanks

Jon

#144037
Feb 04, 2016 16:31
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.