public TemplateContent GetTemplate(string templateName, string templateVersion = null)
 {
     if (string.IsNullOrEmpty(templateName))
     {
         throw new ArgumentNullException(nameof(templateName));
     }
     if (string.IsNullOrEmpty(templateVersion))
     {
         return(GetLatestTemplate(templateName));
     }
     else
     {
         var containerClient = GetTemplatesContainer();
         var blobItem        = containerClient
                               .GetBlobs(BlobTraits.Metadata, BlobStates.Snapshots, templateName)
                               .FirstOrDefault(o => o.Metadata[TEMPLATE_VERSION_KEY] == templateVersion);
         if (blobItem == null)
         {
             return(null);
         }
         var blobClient = containerClient.GetBlobClient(blobItem.Name);
         var contents   = DownloadContentItem(blobClient, blobItem);
         return(ContentItemFactory.BuildTemplate(blobClient.Uri, blobItem, contents));
     }
 }
        /// <summary>
        /// The <see cref="CreateTemplate(string, Stream)"/> operation creates a new template.
        /// If the template with the same name already exists, the operation creates a new version.
        /// </summary>
        /// <param name="templateName">The name of template to create.</param>
        /// <param name="contents">The <see cref="Stream"/> that contains a valid Word document.</param>
        /// <returns>A <see cref="ContentItem"/> describing the state of the created template.</returns>
        /// <remarks>
        /// An <see cref="ArgumentNullException"/> will be thrown if templateName or contents are null or empty.
        /// A <see cref="RequestFailedException"/> will be thrown if a failure occurs.
        /// </remarks>
        public async Task <TemplateContent> CreateTemplate(string templateName, Stream contents)
        {
            if (string.IsNullOrEmpty(templateName))
            {
                throw new ArgumentNullException(nameof(templateName));
            }
            if (contents == null || contents.Length == 0)
            {
                throw new ArgumentNullException(nameof(contents));
            }

            var containerClient = GetTemplatesContainer();
            var blobFileName    = $"{templateName}.docx";
            var blobClient      = containerClient.GetBlobClient(blobFileName);

            var templateVersion = await TryCreateSnapshot(blobClient, TEMPLATE_VERSION_KEY);

            var metadata = new Dictionary <string, string>
            {
                [TEMPLATE_NAME_KEY]    = templateName,
                [TEMPLATE_VERSION_KEY] = templateVersion
            };

            var response = await blobClient.UploadAsync(contents, metadata : metadata);

            var blobContentInfo = response.Value;

            return(ContentItemFactory.BuildTemplate(blobClient.Uri, blobContentInfo, metadata, contents));
        }
 public TemplateContent GetLatestTemplate(string templateName)
 {
     if (string.IsNullOrEmpty(templateName))
     {
         throw new ArgumentNullException(nameof(templateName));
     }
     try
     {
         var containerClient  = GetTemplatesContainer();
         var blobFileName     = $"{templateName}.docx";
         var blobClient       = containerClient.GetBlobClient(blobFileName);
         var blobDownloadInfo = DownloadContentItem(blobClient);
         return(ContentItemFactory.BuildTemplate(blobClient.Uri, blobDownloadInfo));
     }
     catch (RequestFailedException ex)
     {
         if (ex.Status != 404)
         {
             throw ex;
         }
     }
     return(null);
 }