Mari Jørgensen
Jun 5, 2013
  3614
(1 votes)

Did you know of the UnifiedFileSummarySaved?

Recently I had a customer that wanted to list file attachments on their site. The idea was to use Find and use custom file summary properties as base for the list filter values.

One of the key points was that if “Brand” was not set, the file should not be listed at all on the site. Basically, if file summary was updated I needed to re-index the file.

Those of you who have been working with EPiServer for some time know that the EPiServer Datafactory exposes several events for pages and files. In EPiServer 7 we have got a new event which fires when the file summary is changed. Here is how I made use of it:

Creating a separate VPP with a custom file summary is easy enough - add a new VPP to EPiServerFramework file:

<add showInFileManager="true" virtualName="Manuals" virtualPath="~/Manuals/"
      bypassAccessCheck="false" maxVersions="5" useRouting="true"
      customFileSummary="~/CustomSummary.config" name="SiteManuals"
      type="EPiServer.Web.Hosting.VirtualPathVersioningProvider, EPiServer" />

I then added the CustomSummary.config file, which resulted in the following file summary edit view:

filesummary

Next step was hooking up to the event, and the preferred way is to use a InitializableModule.

[InitializableModule]
    [ModuleDependency(typeof(EPiServer.Web.InitializationModule))]
    public class FileEventHookUp : IInitializableModule
    {
        private bool _eventsAttached = false;

        public void Initialize(InitializationEngine context)
        {
            if (!_eventsAttached)
            {
                // Attach event handler to event that fires when file summary changes
                UnifiedFile.UnifiedFileSummarySaved += FileSummarySaved;
                _eventsAttached = true;
            }
        }

        void FileSummarySaved(UnifiedFile sender, UnifiedVirtualPathEventArgs e)
        {
            var fields = sender.Summary.Dictionary;

            if (fields.Contains("Brand"))
            {
                string brandName = fields["Brand"] as string;
                if (!string.IsNullOrEmpty(brandName))
                {
                    var document = new Document()
                        {
                            Name = fields["Name"] as string,
                            Brand = brandName,
                            Description = fields["Description"] as string,
                            Type = fields["Type"] as string,
                            PermanentLink = sender.PermanentLinkVirtualPath,
                            Changed = sender.Changed
                        };

                    SearchClient.Instance.Index(document);
                }
            }
        }

        public void Uninitialize(InitializationEngine context)
        {
            // Detach event handlers
            UnifiedFile.UnifiedFileSummarySaved -= FileSummarySaved;
        }

        public void Preload(string[] parameters)
        {
            throw new NotImplementedException();
        }
    }

The Document class is a simple DTO class used for storing the data in Find.

As a side note: Those of you who have seen a preview of what is coming in the next release of EPiServer know that handling files will become much easier. Smilefjes

Jun 05, 2013

Comments

valdis
valdis Jun 5, 2013 10:29 PM

btw, you can get rid of _eventsAttached field by just before attaching to an event, detach the handler ;)

Frederik Vig
Frederik Vig Jun 6, 2013 11:39 AM

Good read! One note, important that you don't throw a NotImplementedException in the Preload method, since this will cause problems and bugs in ASP.NET.

Please login to comment.
Latest blogs
Opti ID overview

Opti ID allows you to log in once and switch between Optimizely products using Okta, Entra ID, or a local account. You can also manage all your use...

K Khan | Jul 26, 2024

Getting Started with Optimizely SaaS using Next.js Starter App - Extend a component - Part 3

This is the final part of our Optimizely SaaS CMS proof-of-concept (POC) blog series. In this post, we'll dive into extending a component within th...

Raghavendra Murthy | Jul 23, 2024 | Syndicated blog

Optimizely Graph – Faceting with Geta Categories

Overview As Optimizely Graph (and Content Cloud SaaS) makes its global debut, it is known that there are going to be some bugs and quirks. One of t...

Eric Markson | Jul 22, 2024 | Syndicated blog

Integration Bynder (DAM) with Optimizely

Bynder is a comprehensive digital asset management (DAM) platform that enables businesses to efficiently manage, store, organize, and share their...

Sanjay Kumar | Jul 22, 2024

Frontend Hosting for SaaS CMS Solutions

Introduction Now that CMS SaaS Core has gone into general availability, it is a good time to start discussing where to host the head. SaaS Core is...

Minesh Shah (Netcel) | Jul 20, 2024

Optimizely London Dev Meetup 11th July 2024

On 11th July 2024 in London Niteco and Netcel along with Optimizely ran the London Developer meetup. There was an great agenda of talks that we put...

Scott Reed | Jul 19, 2024