Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • David Brendel 786 posts 2949 karma points c-trib
    Aug 07, 2013 @ 12:27
    David Brendel
    0

    Cancel saving of Stylesheet in Umbraco 6.1.3

    Hi all,

    I'm currently trying to cancel the saving of a stylesheet file. For this i want to add a new event handler that gets called bevor saving of a stylesheet.

    I tried it in two ways:

    First one:

    StyleSheet.BeforeSave += Stylesheet_BeforeSave;
    private void Stylesheet_BeforeSave(StyleSheet sender, SaveEventArgs e)
            {
                //Do what you need to do. In this case logging to the Umbraco log
                //Log.Add(LogTypes.Debug, sender.Id, "Test some properties of stylesheet " + sender.Text + ": " + sender.nodeObjectType);
    
                //cancel the publishing if you want.
                LockEntry myLock = this._db.SingleOrDefault<LockEntry>(sender.Id);
                if (null != myLock)
                {
                    Log.Add(LogTypes.Debug, sender.Id, "the stylesheet " + sender.Text + " has a lock");
                    if (myLock.UserId != User.GetCurrent().Id)
                    {
                        Log.Add(LogTypes.Debug, sender.Id, "the stylesheet " + sender.Text + " is locked by another user");
                        e.Cancel = true;
                    }
                    else
                    {
                        Log.Add(LogTypes.Debug, sender.Id, "the stylesheet " + sender.Text + " is about to be saved");
                        this._db.Delete(myLock);
                        sender.saveCssToFile();
                    }
                }
                else
                {
                    Log.Add(LogTypes.Debug, sender.Id, "the stylesheet " + sender.Text + " has no lock: save file");
                    sender.saveCssToFile();
                }
            }
    

    This makes use of the old event system. The event gets actually called. I set the cancel property of the SaveEventArgs but the Stylesheet gets saved.

    Second one:

    FileService.SavingStylesheet += (sender, e) =>
                {
                    LockEntry myLock = this._db.SingleOrDefault<LockEntry>(e.SavedEntities.First().Id);
                    if (e.CanCancel && myLock != null)
                    {
                        if (myLock.UserId != User.GetCurrent().Id)
                        {
                            e.Cancel = true;
                        }
                    }
                };
    

    This never gets called.

    Any suggestions on how to cancel the saving? Try to create a basic lock feature as a prototype like the MyLock package does for content items.

    Any help welcome.

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies