How to implement custom save logic

From Sense/Net Wiki
Jump to: navigation, search
  •  
  •  
  •  
  •  
  • 100%
  • 6.2
  • Enterprise
  • Community
  • Planned

Overview

It is many times desired to execute a custom programmed logic when a content is saved right before its data is persisted to the repository. Sense/Net provides multiple possibilities to achieve this.

Details

Below is a list of different possibilities to implement custom save logic in Sense/Net:

1. Implement a custom Content Handler

The most obvious choice in Sense/Net would be to implement a custom content handler for the type and override the Save logic. You can see an example for this in the following article:

2. Implement a NodeObserver

A NodeObserver works very similar to a content handler: you also get events you can override. The difference is that you don't need to attach the logic to a single type - this can come handy if a content handler already exists and you don't want to change it (or you can't change it because it is built-in). But therefore you will also have to make sure your logic is only executed at the right cases. Read more info on how to implement a node observer here:

3. Attach to contentview events

When a content is being saved some events are raised by the content view that is displaying the content. You can attach to these events and implement custom logic. Read more info on these events here:

An example would be to attach to the commandbuttons event. This will be emitted whenever a Command Buttons control is present and any of its buttons is clicked by the user. You could place a script in the code like the following:

<script runat="server">
   protected override void OnInit(EventArgs e)
   { 
      base.OnInit(e); 
      this.CommandButtonsAction += new EventHandler<SenseNet.Portal.UI.CommandButtonsEventArgs>(contentView_CommandButtonsAction); 
   }
   protected void contentView_CommandButtonsAction(object sender, SenseNet.Portal.UI.CommandButtonsEventArgs e)
   {
      // custom logic
      if (e.ButtonType != SenseNet.Portal.UI.Controls.CommandButtonType.Cancel)
      { 
         e.Cancel = true; 
         e.ContentView.UpdateContent(); 
         e.ContentView.Content.Save(); 
      }
   }
</script>

4. Attach to contenthandler events

Similarly to attaching to the content view events you could also attach to content handler events. These events are similar to the events of the NodeObserver. Here is an example you could paste in the ascx view to display a thank you message after the content is modified. First modify the view according to the following markup:

<asp:PlaceHolder ID="evaluate" runat="server">
   [every defined control comes here]
</asp:PlaceHolder>
<asp:PlaceHolder ID="thankyou" runat="server" Visible="false">
   Thank you
</asp:PlaceHolder>
<script runat="server">
   protected override void OnInit(EventArgs e)
   {
      base.OnInit(e);
      this.Content.ContentHandler.Modified += new EventHandler<SenseNet.ContentRepository.Storage.Events.NodeEventArgs>(ContentHandler_Modified); 
   }
 
   protected void ContentHandler_Modified(object sender, SenseNet.ContentRepository.Storage.Events.NodeEventArgs e)
   { 
      var thankyou = this.FindControl("thankyou") as PlaceHolder; 
      thankyou.Visible=true; 
      var evaluate = this.FindControl("evaluate") as PlaceHolder; 
      evaluate.Visible=false; 
   }
</script>

Related links

References

There are no external references for this article.