public DocumentContent GetDocument(string documentId) { if (string.IsNullOrEmpty(documentId)) { throw new ArgumentNullException(nameof(documentId)); } var containerClient = GetDocumentsContainer(); var blobName = $"{documentId}.docx"; var blobClient = containerClient.GetBlobClient(blobName); DocumentContent document = null; try { var blobDownloadInfo = DownloadContentItem(blobClient); document = ContentItemFactory.BuildDocument(blobClient.Uri, blobDownloadInfo); } catch (RequestFailedException ex) { if (ex.Status != 404) { throw ex; } } return(document); }
/// <summary> /// The <see cref="CreateDocument(string, string, Stream)"/> operation creates a new document. /// The new document gets a unique name. /// </summary> /// <param name="templateName">The name of the template used for this document.</param> /// <param name="mappingName">The name of the mapping used to transform input.</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 document.</returns> /// <remarks> /// An <see cref="ArgumentNullException"/> will be thrown if any of the parameters are null or empty. /// A <see cref="RequestFailedException"/> will be thrown if a failure occurs. /// </remarks> public async Task <DocumentContent> CreateDocument(string templateName, string mappingName, Stream contents) { // Check if (string.IsNullOrEmpty(templateName)) { throw new ArgumentNullException(nameof(templateName)); } if (string.IsNullOrEmpty(mappingName)) { throw new ArgumentNullException(nameof(mappingName)); } if (contents == null || contents.Length <= 0) { throw new ArgumentNullException(nameof(contents)); } var templateVersion = await GetLatestTemplateVersion(templateName); if (templateVersion == null) { throw new ArgumentException($"Template {templateName} not found."); } var mappingVersion = await GetLatestMappingVersion($"{templateName}_{templateVersion}_{mappingName}"); if (mappingVersion == null) { throw new ArgumentException($"Mapping {mappingName} not found."); } var documentId = DateTime.Now.Ticks.ToString(); var containerClient = GetDocumentsContainer(); //var name = $"{templateName}_{templateVersion}_{mappingName}_{mappingVersion}_{documentId}"; var blobFileName = $"{documentId}.docx"; var blobClient = containerClient.GetBlobClient(blobFileName); var metadata = new Dictionary <string, string> { [TEMPLATE_NAME_KEY] = templateName, [TEMPLATE_VERSION_KEY] = templateVersion, [MAPPING_NAME_KEY] = mappingName, [MAPPING_VERSION_KEY] = mappingVersion, [DOCUMENT_ID] = documentId }; var response = await blobClient.UploadAsync(contents, metadata : metadata); var blobContentInfo = response.Value; return(ContentItemFactory.BuildDocument(blobClient.Uri, blobContentInfo, metadata, contents)); }