Change the uploaded file template in your library in Sitecore

I did some research on this, but I'm not sure that I understand all the parts that need to be solved in the next task.

My client needs a special template that will be used instead of the automatically detected media templates in the Media Library if they are downloaded to a specific folder. The template has special fields. The template can also store files of various types (PDF files, formats specific to manufacturers, executable files).

For development purposes, we are currently uploading a file and then doing a template switch, but what really needs to happen is that the file will be loaded into this type of template in the first place. I was wondering if there is a way to connect to the download process to make sure that a special template is used when under a specific path in the Media Library? If so, where do I start?

+3
source share
4 answers

We recently had to do something similar. On the same line as techphoria414, I would connect to the pipeline to save the download. Then, to make it more versatile and reusable, use Sitecore's configuration parsing to link everything to your handler. Here we are done.

"":

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);
                }
            }
        }
    }
}

:

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

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>

, .

, !

+9

, , Sitecore.Resources.Media.MediaCreator(handels mediaitem creation) . , () - -.

, , sheerUI, . .. Sitecore, web.config

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

/ , .

SheerUI, : http://learnsitecore.cmsuniverse.net/en/Developers/Articles/2009/10/My-First-Sitecore-XAML-Application.aspx

+1

: save handler. , . : , , , .

+1

- . , .

:

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

:

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

, args.Folder Sitecore .

This is not as important for developers as we are, but some administrators rely on this tool as part of their workflow if they do not have full access to the site.

+1
source

All Articles