Frage

I've done some research into this but not sure I understand all the pieces that need to go into the following problem.

My client needs a special template to be used instead of the auto detected media templates in the Media Library if they upload to a certain Folder. The template has special fields. The template can also house different types of files (PDFs, vendor specific formats, executables).

For development purposes we are currently uploading the file and then doing a template switch afterwards but what really needs to happen is that file be uploaded to that template type in the first place. I was wondering if there was a way to hook into the upload process to make sure the special template is used when underneath a certain path in the Media Library? If so, where should I start?

War es hilfreich?

Lösung

We recently had to do something similar. Along the same line as techphoria414, I'd tap into the upload save pipeline. Then, to make it a bit more generic and reusable, use the power of Sitecore's configuration parsing to hook everything up to your handler. Here's what we ended up with.

Main class with required "Process" method:

public class ChangeTemplate
{
    public string Name { get; set; }
    public string Path { get; set; }
    public List<ChangedMediaTemplate> Templates { get; set; }

    public ChangeTemplate()
    {
        Templates = new List<ChangedMediaTemplate>();
    }

    public void Process(UploadArgs args)
    {
        var db = Sitecore.Context.ContentDatabase;

        var uploadPath = db.GetItem(args.Folder).Paths.ContentPath;
        if (!uploadPath.StartsWith(Path))
        {
            // Not uploading to designated folder
            return;
        }

        foreach (var item in args.UploadedItems)
        {
            // Need to change template for this item?
            var changedTemplate = Templates.Where(t => t.Old.Equals(item.Template.FullName)).FirstOrDefault();
            if (changedTemplate != null)
            {
                var newTemplate = db.Templates[changedTemplate.New];
                try
                {
                    item.ChangeTemplate(newTemplate);
                }
                catch (Exception e)
                {
                    Log.Error("Unable to change {0} template on upload of {1} to {2}.".FormatWith(Name, item.Name, uploadPath), e, this);
                }
            }
        }
    }
}

Minor supporting class:

public class ChangedMediaTemplate
{
    public string Old { get; set; }
    public string New { get; set; }
}

And then the config:

<processors>
    <uiUpload>
      <processor patch:after="*[@type='Sitecore.Pipelines.Upload.Save, Sitecore.Kernel']" mode="on" type="Foo.Project.SitecoreX.Pipelines.Upload.ChangeTemplate, Foo.Project.Classes">
        <Name>Product Images</Name>
        <Path>/sitecore/media library/Images/Foo/products</Path>
        <Templates hint="list">
          <Template type="Foo.Project.SitecoreX.Pipelines.Upload.ChangedMediaTemplate, Foo.Project.Classes">
            <Old>System/Media/Unversioned/Image</Old>
            <New>User Defined/Foo/Product/Image/Unversioned/Product Image</New>
          </Template>
          <Template type="Foo.Project.SitecoreX.Pipelines.Upload.ChangedMediaTemplate, Foo.Project.Classes">
            <Old>System/Media/Unversioned/Jpeg</Old>
            <New>User Defined/Foo/Product/Image/Unversioned/Product Jpeg</New>
          </Template>
          <Template type="Foo.Project.SitecoreX.Pipelines.Upload.ChangedMediaTemplate, Foo.Project.Classes">
            <Old>System/Media/Versioned/Image</Old>
            <New>User Defined/Foo/Product/Image/Versioned/Product Image</New>
          </Template>
          <Template type="Foo.Project.SitecoreX.Pipelines.Upload.ChangedMediaTemplate, Foo.Project.Classes">
            <Old>System/Media/Versioned/Jpeg</Old>
            <New>User Defined/Foo/Product/Image/Versioned/Product Jpeg</New>
          </Template>
        </Templates>
      </processor>
    </uiUpload>
</processors>

Modifying or adding new template rules becomes as simple as editing the config as needed.

Hope this helps!

Andere Tipps

Unfortunately, to my knowledge, the Sitecore.Resources.Media.MediaCreator(handels mediaitem creation) can not be overridden. So the only (easy) way is to change the templates for the entire media library.

Otherwise I think you need to make your own changes to the sheerUI - but i wouldn't recommend it. Anyhow.. the mediaitems Sitecore creates, are defined in web.config under

<mediaLibrary>
    <mediaTypes>
         <mediaType name="Any" extensions="*">...</mediaType>
         <mediaType name="Windows Bitmap image" extensions="bmp">...</mediaType>
          ....
    </mediaTypes>
</mediaLibrary>

There is a version/unversion template for each mediaitem you can change.

If you want to look into SheerUI I recommend you start here: http://learnsitecore.cmsuniverse.net/en/Developers/Articles/2009/10/My-First-Sitecore-XAML-Application.aspx

I would use an item:saving handler. If the item is a Media Item, and within a configured folder, then you can change its template. As always with item:saving, insert some checks very early in the method and exit quickly if you determine the item is not of concern.

I want to add something to ambrauer's answer above. It is a good solution but the code should be tweaked before use in production.

The following line:

var uploadPath = db.GetItem(args.Folder).Paths.ContentPath;

should be changed to:

if (args.Folder == null) return;
var uploadFolderItem = db.GetItem(args.Folder);
if (uploadFolderItem == null) return;
var uploadPath = uploadFolderItem.Paths.ContentPath;

The reason is that without the null check on args.Folder, the package upload tool in the Sitecore installation wizard breaks.

This is not critical to developers like us but some administrators rely on this tool as part of their workflow if they do not have full access to the site.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top