1

We are implementing a solution that requires importing media items like images inside Sitecore Media Library. Found it is achievable with Old WebApi, however nothing mentioned equivalent in Sitecore ItemService Restfull API guide.

https://sdn.sitecore.net/upload/sitecore7/75/developer's_guide_to_sitecore.services.client_sc75-a4.pdf

Knowing that we are working on Sitecore 9.0.1, Is there a way to achieve this using ItemService Restfull API?

I found the same problem on Sitecore 8 referred to in the following link and the suggested solution was to use the old webapi on Sitecore v8 platform. However my question is: Would this still be the solution in case of Sitecore 9 ?

Creating Media Items using the RESTful ItemService API

  • Is Sitecore Powershell an option? – Hishaam Namooya Jul 12 '18 at 7:50
  • do you mean remoting ? I have to find away to trigger the powershell in c# code – user3964 Jul 12 '18 at 13:00
  • I recently did one where I have the script calling the C# code. The steps are: Put the images on the server. Execute the script. – Hishaam Namooya Jul 12 '18 at 13:03
  • what I need is to execute the script from c# code. – user3964 Jul 12 '18 at 14:45
  • Please see answer below – Hishaam Namooya Jul 12 '18 at 15:00
1

For a similar requirement, I have used both Sitecore Powershell and C#.

Here is the code in C#

namespace Sitecore.Extension.ImageUpload
{
    using System.Collections.Generic;
    using System.IO;
    using System.Text;
    using Sitecore.Configuration;
    using Sitecore.Data.Items;
    using Sitecore.Resources.Media;

    public static class MediaUploader
    {
        public static string TriggerUpload(string directory, string sitecorePath)
        {
            var sb = new StringBuilder();

            var filesWithPath = DirSearch(directory);

            foreach (var filePath in filesWithPath.Keys)
            {
                sb.AppendLine($"Processing {filePath}");

                var myFile = AddFile(filePath, sitecorePath, filesWithPath[filePath]);

                sb.AppendLine(myFile != null
                    ? $"[SUCCES] File {myFile.Name} added successfully"
                    : $"[FAIL] Fail to add file with path {filePath}");
            }

            return sb.ToString();
        }

        public static MediaItem AddFile(string fileName, string sitecorePath, string path)
        {
            path = path.Replace("\\\\", "\\");

            // Create the options
            MediaCreatorOptions options = new MediaCreatorOptions
            {
                FileBased = false,
                IncludeExtensionInItemName = false,
                OverwriteExisting = false,
                Versioned = false,
                Destination = sitecorePath + "/" + fileName.Replace(".jpg", string.Empty).Replace(".png", string.Empty).Replace(".jpeg", string.Empty),
                Database = Factory.GetDatabase("master")
            };

            // Now create the file
            var creator = new MediaCreator();

            var mediaItem = creator.CreateFromFile(path, options);

            return mediaItem;
        }

        private static Dictionary<string, string> DirSearch(string sDir)
        {
            var files = new Dictionary<string, string>();

            foreach (string f in Directory.GetFiles(sDir))
            {
                files.Add(Path.GetFileName(f), f);
            }

            foreach (string d in Directory.GetDirectories(sDir))
            {
                DirSearch(d);
            }

            return files;
        }
    }
}

And the Powershell Script is

$paths = @(
"$AppPath/images/mysite/banners=/sitecore/media library/my site/banners",
"$AppPath/images/mysite/main banners=/sitecore/media library/my site/main banners"
)

foreach($path in $paths){

    $splitter = $path -split "="

    "Executing for $splitter[0]"

    New-UsingBlock (New-Object Sitecore.SecurityModel.SecurityDisabler) {
        New-UsingBlock (New-Object Sitecore.Data.DatabaseCacheDisabler) {
            New-UsingBlock (New-Object Sitecore.Data.Events.EventDisabler) {
                New-UsingBlock (New-Object Sitecore.Data.BulkUpdateContext) {
                    $results = [Sitecore.Extension.ImageUpload.MediaUploader]::TriggerUpload($splitter[0], $splitter[1])
                    $results
                }
            }
        }
    }

    "-----------------------------------------------"
}

In the script, you need to set the path of the image directory where it resides on the server and the destination path.

'image path on server'='destination of images in Sitecore'

Once completed, you need to clear the Sitecore Cache via the cache.aspx page

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.