1

Post updated on 05/03 for more information:

Developing a content migration tool using C# console app, using HTTP CRUD operations to access/add/update Sitecore items . To create Media items used HTTP post method, with following References:

I am guessing I need to find right API resource (correct API method with respective parameters) to make the media items creation successful.

https://doc.sitecore.com/xp/en/developers/93/sitecore-experience-manager/the-restful-api-for-the-itemservice.html

https://doc.sitecore.com/xp/en/sdnarchive/upload/sdn5/modules/sitecore%20item%20web%20api/sitecore_item_web_api_developer_guide_sc65-66-usletter.pdf

enter image description here

Addition info: I am able to create non-media items using same HTTP post method by passing values in API request body. Tried same approach with media items , getting below error

Getting NO HTTP resource error, I have checked and ensured the following folder is present in Sitecore CM: sitecore/media library/XXX

Error: {"Message":"No HTTP resource was found that matches the request URI 'https://xxxcore-content.yyyyy.com/sitecore/api/ssc/sitecore/media library/XXX/?name=TestMediaTwo'."}

Tried creating media items differently (will add more info soon) and got below error. The request entity's media type 'multipart/form-data' is not supported for this resource.

Another reference: https://www.anyaconsultancy.com/blog/Sitecore-Item-Web-API-CRUD-Operations/

1 Answer 1

0

There are 2 ways to create media items in Sitecore programmatically.

  1. Create media items using APIs : You can use the Sitecore.Resources.Media.MediaCreator and Sitecore.Resources.Media.MediaCreatorOptions classes to create media items from files. For example, to create the media item /Sitecore/Media Library/Images/Sample in the Master database from the file C:\temp\sample.jpg:
Sitecore.Resources.Media.MediaCreatorOptions options =

new Sitecore.Resources.Media.MediaCreatorOptions();

options.Database = Sitecore.Configuration.Factory.GetDatabase("master");

options.Language = Sitecore.Globalization.Language.Parse(

Sitecore.Configuration.Settings.DefaultLanguage);

options.Versioned = Sitecore.Configuration.Settings.Media.UploadAsVersionableByDefault;

options.Destination = "/sitecore/media library/images/Sample";

options.FileBased = Sitecore.Configuration.Settings.Media.UploadAsFiles;

Sitecore.Resources.Media.MediaCreator creator =

new Sitecore.Resources.Media.MediaCreator();

Sitecore.Data.Items.MediaItem sample =

creator.CreateFromFile(@"C:\temp\sample.jpg",options);
  1. Using Sitecore.Resources.Media.MediaManager.Creator: The MediaCreator is also available through the static implementation, Sitecore.Resources.Media.MediaManager.Creator, so if you like statics, you can use that one instead. The MediaCreator can also create media files from a MemoryStream:
// Remember to insert the appropriate try..catch..finally blocks.
// I have refrained from using them to compress the example.
Sitecore.Resources.Media.MediaCreatorOptions options = new Sitecore.Resources.Media.MediaCreatorOptions();
// Now use the options class as I did in the previous example
// ... insert code here ...
// ...
// Read the file from stream:
FileInfo fi = new System.IO.FileInfo(fileName);
FileStream fs = fi.OpenRead();
MemoryStream ms = new MemoryStream();
ms.SetLength(fs.Length);
fs.Read(ms.GetBuffer(), 0, (int)fs.Length);
ms.Flush();
fs.Close();
// Now create the file in Sitecore. This time I'll use the static implementation of the MediaCreator
Item mediaItem = Sitecore.Resources.Media.MediaManager.Creator.CreateFromStream(ms, fileName, options);
ms.Dispose();

Reference: Adding a file to the Sitecore Media Library programatically

Hope it helps!

4
  • Thank you for the answer and update on question.
    – Akshay D
    May 3, 2023 at 3:13
  • I will add some more info to post on my specific issues, I am working on migration tool using C# console app, where trying to HTTP Post method to create all required sitecore items including Media . referred following sitecore documentation . doc.sitecore.com/xp/en/developers/93/… @sumit-helaiya .
    – Akshay D
    May 3, 2023 at 16:16
  • updated post with more information.
    – Akshay D
    May 3, 2023 at 16:39
  • You need to change your code as per my suggestion and try it. Itemservice api is used to create items not for media item creation. You can add sitecore dll reference in your console app and try. May 4, 2023 at 15:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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