2016-05-29 116 views
2

我有一把umbraco 7.4.3项目。 我需要以编程方式为umbraco后台创建的每个特定对象创建媒体文件夹。在一把umbraco创建文件夹 - 一把umbraco 7

例如: 我在后台创建酒店,我去我的重载功能“Umbraco.Core.Services.ContentService.Saved”里面这个函数我试图创建媒体文件夹(同名我的新酒店名称)放在酒店图像内的名为“hotels”的现有媒体文件夹下。

enter image description here

回答

4

不要超载任何的服务功能。您应该创建一个从ApplicationEventHandler派生的类并覆盖ApplicationStarted方法。在那里,你可以连接到ContentService.Saving(或Saved)事件,然后直接使用Services.Media.CreateMedia()创建媒体项目。有关更多详细信息,请参阅https://our.umbraco.org/documentation/Reference/Events/

例如为:

using Umbraco.Core; 
using Umbraco.Core.Events; 
using Umbraco.Core.Logging; 
using Umbraco.Core.Models; 
using Umbraco.Core.Services; 

namespace MyProject.EventHandlers 
{ 
    public class RegisterEvents : ApplicationEventHandler 
    { 
     protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext) 
     { 
      //Listen for when content is being saved 
      ContentService.Saving += ContentService_Saving;  
     } 

     /// <summary> 
     /// Listen for when content is being saved, check if it is a new 
     /// Hotel item and create new Media Folder. 
     /// </summary> 
     private void ContentService_Saving(IContentService sender, SaveEventArgs<IContent> e) 
     {     
      IMedia parentFolder; // You need to look this up. 
      foreach (var content in e.SavedEntities 
       //Check if the content item type has a specific alias 
       .Where(c => c.Alias.InvariantEquals("Hotel")) 
       //Check if it is a new item 
       .Where(c => c.IsNewEntity())) 
      { 
       Services.Media.CreateMedia(e.Name, parentFolder, "Folder"); 
      } 
     } 
    } 
} 

注:我还没有测试此代码;你可能需要调试它;并由您指定父文件夹。