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!

Mari Jørgensen
Jun 5, 2013
  3846
(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
Render ContentArea without wrapping them in surrounding div

CustomContentAreaRenderer is a specialized class that overrides the default ContentAreaRenderer. It customizes the rendering behavior for content...

sunylcumar | May 18, 2025

Indexing a content item programatically

public bool IndexContent(int contentId, bool contentOnly, bool childrenOnly, string language) { // Retrieve the content var contentReference = new...

sunylcumar | May 18, 2025

Add a new menu item to the Admin Menu in Optimizely CMS

Create a new Controller called CustomMenuController and decorate with [Authorize(Roles ="CMSAdmins")] so that it will be accessed by admins only...

sunylcumar | May 18, 2025

Display page/block thumbnail based on selected site in multi-site solution

In previous blog we described how to control the visibility of the blocks or properties based on the current site in multisite solution. We can use...

Tomek Juranek | May 16, 2025

Understanding the Infrastructure Powering AI Agents for Marketing

The marketing world is increasingly captivated by the potential of AI agents. However, it's crucial to recognize that these agents are not simply...

Patrick Lam | May 15, 2025

Meet the Newest OMVPs – Winter 2025 Cohort

We're excited to officially welcome the latest winter cohort of Optimizely Most Valuable Professionals (OMVPs) - a group of passionate tech...

Satata Satez | May 14, 2025