在 Umbraco 中创建文件夹 - Umbraco 7
Create folder in umbraco - Umbraco 7
我在 umbraco 7.4.3 中有项目。
我需要以编程方式为我在 umbraco 后台创建的每个特定对象创建媒体文件夹。
例如:
我在后台创建酒店,然后在这个函数中重载函数 "Umbraco.Core.Services.ContentService.Saved" 我正在尝试在名为 [=18= 的现有媒体文件夹下创建媒体文件夹(与我的新酒店名称同名) ]用于酒店内部图片。
不要超载任何服务功能。您应该创建一个派生自 ApplicationEventHandler
的 class 并覆盖 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");
}
}
}
}
注意:我没有测试过这段代码;您可能需要调试它;由您指定父文件夹。
我在 umbraco 7.4.3 中有项目。 我需要以编程方式为我在 umbraco 后台创建的每个特定对象创建媒体文件夹。
例如: 我在后台创建酒店,然后在这个函数中重载函数 "Umbraco.Core.Services.ContentService.Saved" 我正在尝试在名为 [=18= 的现有媒体文件夹下创建媒体文件夹(与我的新酒店名称同名) ]用于酒店内部图片。
不要超载任何服务功能。您应该创建一个派生自 ApplicationEventHandler
的 class 并覆盖 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");
}
}
}
}
注意:我没有测试过这段代码;您可能需要调试它;由您指定父文件夹。