Create images

To create an image, perform the following:

  1. Check whether the image already exists.
    Before you create the image, you must check whether an image with the same ID already exists.

  2. Create the image.
    If the image does not exist, you create a new image with the specified ID.

  3. Specify the parent album of the image.
    Specify the album where the image belongs to.

  4. Set the required properties. When creating a new image, set the following properties:

    • Title
    • LastModified
    • DateCreated
    • UrlName
    • MediaFileUrlName

    You can also set any other properties.

  5. Upload the image data.
    Upload the image data to the database.

  6. Save the image.
    Save all changes that you have made to the image.

  7. Publish the image.
    You publish the image using the workflow manager.

The example below shows you how to create an image with predefined ID.

NOTE: The ID argument is assigned to the master version of the image. For more information about the different versions of an image, see For developers: Content lifecycle.

EXAMPLE: Create an image with predefined ID.
The following code samples create an image with the specified IDTitle, and image data.

Native API

C#
using System;
using System.Linq;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using Telerik.Sitefinity.Workflow;
using Telerik.Sitefinity.Libraries.Model;
using System.IO;
using Telerik.Sitefinity.Modules.Libraries;

namespace SitefinityWebApp
{
   public class CreateImageNativeAPI
   {
       // NOTE: The imageExtension parameter must contain '.', for example '.jpeg'
       private void CreateImageWithNativeAPI(Guid masterImageId, Guid parentAlbumId, string imageTitle, Stream imageStream, string imageFileName, string imageExtension)
       {
           LibrariesManager librariesManager = LibrariesManager.GetManager();
           Image image = librariesManager.GetImages().Where(i => i.Id == masterImageId).FirstOrDefault();

           if (image == null)
           {
               //The album post is created as master. The masterImageId is assigned to the master version.
               image = librariesManager.CreateImage(masterImageId);

               //Set the parent album.
               Album album = librariesManager.GetAlbums().Where(i => i.Id == parentAlbumId).SingleOrDefault();
               image.Parent = album;

               //Set the properties of the album post.
               image.Title = imageTitle;
               image.DateCreated = DateTime.UtcNow;
               image.PublicationDate = DateTime.UtcNow;
               image.LastModified = DateTime.UtcNow;
               image.UrlName = Regex.Replace(imageTitle.ToLower(), @"[^\w\-\!\$\'\(\)\=\@\d_]+", "-");
               image.MediaFileUrlName = Regex.Replace(imageFileName.ToLower(), @"[^\w\-\!\$\'\(\)\=\@\d_]+", "-");
               librariesManager.RecompileAndValidateUrls(image);

               //Upload the image file.
               // The imageExtension parameter must contain '.', for example '.jpeg'
               librariesManager.Upload(image, imageStream, imageExtension);

               //Save the changes.
               librariesManager.SaveChanges();

               //Publish the Albums item. The live version acquires new ID.
               var bag = new Dictionary<string, string>();
               bag.Add("ContentType", typeof(Image).FullName);
               WorkflowManager.MessageWorkflow(masterImageId, typeof(Image), null, "Publish", false, bag);
           }
       }

   }
}

First, you get an instance of the LibrariesManager class. You check whether an image with the same ID already exists. Then, to create the image, you must call the CreateImage method of the LibrariesManager class. You can create an image with either predefined or auto-generated ID depending which overload of the method you use. The method returns the master version of the image. Then, you get an instance of the specified parent album. To associate the image with the album, you set the album to the Parent property of the image object. Then, you set the properties of the master version of the image. We recommend to set at least the following properties: Title, UrlNameLastModifiedPublicationDateDateCreated, and MediaFileUrlName. Then, to upload the image data, you call the Upload method of the LibrariesManager class and pass the master version of the image, the stream of the selected file, and its file extension as parameters. To save the changes, you call the SaveChanges method of the manager. Finally, to publish the image, you call the MessageWorkflow method of the WorkflowManager class and pass the required parameters.

Fluent API

C#
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using Telerik.Sitefinity;
using Telerik.Sitefinity.Workflow;
using Telerik.Sitefinity.Libraries.Model;
using System.IO;

namespace SitefinityWebApp
{
   public class CreateImageFluentAPI
   {
// NOTE: The imageExtension parameter must contain '.', for example '.jpeg'    
       private Guid CreateImageFluentAPI(Guid imageId, Guid parentAlbumId, string imageTitle, Stream imageStream, string imageFileName, string imageExtension)
       {
           //Check whether the parent Album exists.
           var count = 0;
           imageId = Guid.NewGuid();

           App.WorkWith().Albums().Where(i => i.Id == parentAlbumId).Count(out count);

           if (count > 0)
           {
               //The album post is created as master. The masterImageId is assigned to the master version.
               App.WorkWith().Album(parentAlbumId).CreateImage().CheckOut()
                   //Set the properties of the album post.
                   .Do(image =>
                   {
                       //Set the properties.
                       imageId = image.Id;
                       image.Title = imageTitle;
                       image.DateCreated = DateTime.UtcNow;
                       image.PublicationDate = DateTime.UtcNow;
                       image.LastModified = DateTime.UtcNow;
                       image.Urls.Clear();
                       image.UrlName = Regex.Replace(imageTitle.ToLower(), @"[^\w\-\!\$\'\(\)\=\@\d_]+", "-");
                   })
                   //  .CheckOut() -- This is unnecessary 

                   //Upload the image file.	    // The imageExtension parameter must contain '.', for example '.jpeg'
                   .UploadContent(imageStream, imageExtension)
                   //Save the changes.
                   .Publish()
                   .SaveChanges();
           }

           return imageId;
       }
// This is the sample in the documentation
       private Guid CreateImageWithFluentAPI(Guid imageId, Guid parentAlbumId, string imageTitle, Stream imageStream, string imageFileName, string imageExtension)
       {
           //Check whether the parent Album exists.
           var count = 0;
           imageId = Guid.NewGuid();

           App.WorkWith().Albums().Where(i => i.Id == parentAlbumId).Count(out count);

           if (count > 0)
           {
               //The album post is created as master. The masterImageId is assigned to the master version.
               App.WorkWith().Album(parentAlbumId).CreateImage().CheckOut()
                   //Set the properties of the album post.
                   .Do(image =>
                   {
                       //Set the properties.
                       imageId = image.Id;
                       image.Title = imageTitle;
                       image.DateCreated = DateTime.UtcNow;
                       image.PublicationDate = DateTime.UtcNow;
                       image.LastModified = DateTime.UtcNow;
                       image.Urls.Clear();
                       image.UrlName = Regex.Replace(imageTitle.ToLower(), @"[^\w\-\!\$\'\(\)\=\@\d_]+", "-");
                   })

                   //Upload the image file.
                   .UploadContent(imageStream, imageExtension)
                   .CheckIn()
                   //Save the changes.
                   .Publish()
                   .SaveChanges();
           }

           return imageId;
       }
   }
}

First, you check whether an image with the same ID already exists. Then, you check whether the specified parent album exists. If it exists, you get the singular album facade of the parent album. To create the image, you call the CreateImage method of the singular album facade. The ID argument is assigned to the ID property of the master version of the item. You check out the item. Then, you set the properties of the image by calling the Do method of the facade. To upload the image data, you call the UploadContent method of the facade and pass the image, the stream of the selected file and its extension as arguments. To check in the image, you call the CheckInmethod of the facade. Then, you save the changes. Finally, to publish the image in live state, you call the MessageWorkflow method of the WorkflowManager class and pass the required parameters.

Want to learn more?
Enhance your Sitefinity skills by enrolling in free training sessions. Become Sitefinity certified through Progress Education Community to strengthen your professional credentials.
New to Sitefinity?