November Happy Hour will be moved to Thursday December 5th.

Content Management API - Uploading Media Assets via the API into CMS 12

Vote:
 

We’re currently working on a project for one of our clients whereby as part of this we need to migrate large sets of content pages and media assets from Sitecore over to Optimizely CMS 12.

As part of this we’ll be using the new Content Management APIs as apposed to the ContentRepositories method available in V11, which makes this much easier.

However, when it came to work with the media content and use the API to import the this into the CMS 12 I couldn’t get this to work, even when following Optimizely’s documentation (https://docs.developers.optimizely.com/content-cloud/v1.6.0-content-management-api/docs/api-fundamentals)  I search loads of forums, Stack Overflow, and the only post I could find was this (https://world.optimizely.com/forum/developer-forum/CMS/Thread-Container/2021/11/importing-assets-and-media-files-to-optimizely-through-api/) which gave me a starting point, however this was for commerce and for V11 not 12.

I have since now, been able to mock up a solution, so thought it would be good to share this,

my code:

public async Task<HttpResponseMessage> CreateMediaAsync(string message, string token)
        {
            var response = new HttpResponseMessage();
            string endPoint = $"{_baseUri}";

            var filePath =
                @"C:\<PATH_TO_YOUR_FILE>\";

            var path = new DirectoryInfo(filePath);
            var files = path.GetFiles();
            string filepath = files.FirstOrDefault().FullName;

            var request = new HttpRequestMessage()
            {
                RequestUri = new Uri(endPoint),
                Method = HttpMethod.Post,
            };
            request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", token);

            var multipartContent = new MultipartFormDataContent();

            multipartContent.Add(new StringContent(message), "content");
            multipartContent.Add(new ByteArrayContent(File.ReadAllBytes(filepath)), "file", Path.GetFileName(filepath));
            
            request.Content = multipartContent;

            try
            {
                response = await _client.SendAsync(request);
                if (response.IsSuccessStatusCode) return response;
            }
            catch (Exception ex)
            {
                _logger.Log(LogLevel.Error, "There was an error trying GET json payload.", ex);
                Dispose();
                throw;
            }

            return response;
        }

New Imported media asset using the Content Managment API.



One thing to note, this is only supported in Content Managment API 3.4.x


Hope this helps anyone else that has come across the same problem.

#291831
Edited, Nov 17, 2022 10:03
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.