在Orchard中自动运行批量操作

时间:2014-07-29 09:29:36

标签: admin orchardcms orchardcms-1.7 orchardcms-1.8

如果您自定义自动部件,则可以选择在每次保存时重新创建网址。

此选项下的帮助文字显示:

"编辑内容时自动重新生成 此选项将导致在您编辑现有内容并再次发布时自动重新生成Url,否则它将始终保留旧路由,或者您必须在Autoroute管理员中执行批量更新。"

我已经四处寻找,但我找不到任何地方" Autoroute admin"。

真的存在吗?

这是一项从未实施的提议功能?

即使没有管理员页面,是否还想进行批量更新?

由于

在@joshb建议之后编辑...

我试图在我的控制器中实现批量操作。

  var MyContents = _contentManager.Query<MyContentPart, MyContentPartRecord>().List().ToList();

  foreach (var MyContent in MyContents) {
        var autoroutePart = recipe.ContentItem.As<AutoroutePart>();
        autoroutePart.UseCustomPattern = false;
        autoroutePart.DisplayAlias = _autorouteService.GenerateAlias(autoroutePart);
        _contentManager.Publish(autoroutePart.ContentItem);
  }

以这种方式,它为包含给定部分MyContentPart的类型重新创建所有别名。 通过更多工作,可以将此代码封装在Alias UI中的命令或新选项卡中。 完成当前项目后,我将尝试...

3 个答案:

答案 0 :(得分:0)

在模块部分启用Alias UI将为您提供管理路径的管理部分,但我不确定它提供的批量更新类型

答案 1 :(得分:0)

您可以创建一个模块并实现执行批量更新的命令。如果您喜欢创建模块,那么不应该做太多工作。您需要实现DefaultOrchardCommandHandler并注入IContentManager以获取您感兴趣的所有部分。

答案 2 :(得分:0)

如果ContentItem已经发布(就像我的情况那样),那么发布它将不会做任何事情。

相反,可以在AutorouteService上调用PublishAlias方法。我最终得到了一个控制器,就像这样:

using Orchard;
using Orchard.Autoroute.Models;
using Orchard.Autoroute.Services;
using Orchard.ContentManagement;
using Orchard.Localization;
using Orchard.Security;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;

namespace MyNamespace.MyModule.Controllers {

    public class AutorouteBulkUpdateController : Controller {

        private readonly IOrchardServices _orchardServices;
        private readonly IAutorouteService _autorouteService;
        private Localizer T { get; set; }

        public AutorouteBulkUpdateController(IOrchardServices orchardServices, IAutorouteService autorouteService) {
            _orchardServices = orchardServices;
            _autorouteService = autorouteService;
            T = NullLocalizer.Instance;
        }

        public ActionResult Index() {

            if (!_orchardServices.Authorizer.Authorize(StandardPermissions.SiteOwner, T("Not authorized to manage settings"))) {
                return new HttpUnauthorizedResult();
            }

            int count = 0;
            IEnumerable<AutoroutePart> contents;

            do {
                //contents = _orchardServices.ContentManager.Query<AutoroutePart>(VersionOptions.Latest, new string[] { "Page" }).Slice(count * 100, 100).ToList();
                contents = _orchardServices.ContentManager.Query<AutoroutePart>(VersionOptions.Latest).Slice(count * 100, 100).ToList();

                foreach (var autoroutePart in contents) {

                    var alias = _autorouteService.GenerateAlias(autoroutePart);

                    if (autoroutePart.DisplayAlias != alias) {
                        autoroutePart.UseCustomPattern = false;
                        autoroutePart.DisplayAlias = alias;
                        _autorouteService.PublishAlias(autoroutePart);
                    }
                }

                _orchardServices.TransactionManager.RequireNew();
                _orchardServices.ContentManager.Clear();

                count += 1;

            } while (contents.Any());

            return null;
        }
    }
}