Raymond Ray (Customer) asked a question.

Renderer - How to select uploaded document dynamically on restClient.CreateItem();

Using Sitefinity Renderer how can i select that uploaded document dynamically on using restClient.CreateItem();

 

 

 

DTO

  1. using Progress.Sitefinity.RestSdk.Dto;
  2. namespace Renderer.Dto
  3. {
  4. [MappedSitefinityType("Telerik.Sitefinity.DynamicTypes.Model.FormsandTemplates.FormAndTemplate")]
  5. public class FormsAndTemplatesDto : SdkItem
  6. {
  7. public string Title { get; set; }
  8. public string Content { get; set; }
  9. public string ItemDefaultUrl { get; set; }
  10. public string[] Category { get; set; }
  11. public IEnumerable<DocumentDto> TemplateFile { get; set; }
  12. }
  13. }

Controller

  1. var formTemplateItem = new FormsAndTemplatesDto
  2. {
  3. Title = worksheet.Cells[row, 4].Value.ToString(),
  4. Category = categoryIds,
  5. };
  6. // Upload the new document
  7. var document = await MediaHelper.UploadDocument(
  8. restClient,
  9. fileStream,
  10. libraryId,
  11. fileName,
  12. Path.GetFileNameWithoutExtension(fileName)
  13. );
  14. formTemplateItem.TemplateFile = new List<DocumentDto> { document };
  15. .
  16. .
  17. .
  18. //Outside Loop
  19. formTemplateItems.Add(formTemplateItem);
  20. foreach (var formTemplateItem in formTemplateItems)
  21. {
  22. var existingItemsResponse = await this.restClient.GetItems<FormsAndTemplatesDto>(new GetAllArgs()
  23. {
  24. Fields = new[] { "*" },
  25. Filter = $"Title='{formTemplateItem.Title}'",
  26. Take = 1
  27. });
  28. var existingItems = existingItemsResponse.Items;
  29. if (existingItems.Count > 0)
  30. {
  31. // Update
  32. var existingItem = existingItems[0];
  33. formTemplateItem.Id = existingItem.Id;
  34. await restClient.UpdateItem(formTemplateItem);
  35. }
  36. else
  37. {
  38. // Create
  39. await restClient.CreateItem(formTemplateItem);
  40. }
  41. }

MediaHelper.cs - source (here)

  1. using System;
  2. using Microsoft.AspNetCore.StaticFiles;
  3. using Newtonsoft.Json.Linq;
  4. using Progress.Sitefinity.RestSdk;
  5. using Progress.Sitefinity.RestSdk.Clients.Sites.Dto;
  6. using Progress.Sitefinity.RestSdk.Dto;
  7. using Newtonsoft.Json;
  8. using Newtonsoft.Json.Linq;
  9. using Microsoft.AspNetCore.StaticFiles;
  10. public static class MediaHelper
  11. {
  12. //Source-https://community.progress.com/s/article/net-core-how-to-upload-file-in-sitefinity-from-the-renderer-application
  13. public static async Task<DocumentDto> UploadDocument(IRestClient restClient, Stream file, Guid libraryId, string fileName, string documentTitle, string alternativeText = "")
  14. {
  15. var headers = GetUploadHeaders(fileName, libraryId, documentTitle, alternativeText);
  16. var args = new CreateArgs()
  17. {
  18. Data = GetBase64Content(file),
  19. ContentType = GetContentType(fileName),
  20. Encoding = "base64",
  21. AdditionalHeaders = headers,
  22. };
  23. // Return the created DocumentDto
  24. return await restClient.CreateItem<DocumentDto>(args);
  25. }
  26. private static Dictionary<string, string> GetUploadHeaders(string fileName, Guid parentId, string title, string alt = "")
  27. {
  28. var additionalHeaders = new Dictionary<string, string>();
  29. var headersJson = JObject.FromObject(new
  30. {
  31. Title = title,
  32. AlternativeText = alt,
  33. ParentId = parentId,
  34. DirectUpload = true,
  35. }).ToString(Formatting.None);
  36. additionalHeaders.Add("X-Sf-Properties", headersJson);
  37. additionalHeaders["X-File-Name"] = fileName;
  38. return additionalHeaders;
  39. }
  40. private static string GetBase64Content(Stream stream)
  41. {
  42. byte[] byteArray;
  43. using (BinaryReader br = new BinaryReader(stream))
  44. {
  45. byteArray = br.ReadBytes((int)stream.Length);
  46. }
  47. return Convert.ToBase64String(byteArray);
  48. }
  49. private static string GetContentType(string fileName)
  50. {
  51. string contentType;
  52. new FileExtensionContentTypeProvider().TryGetContentType(fileName, out contentType);
  53. return contentType ?? "application/octet-stream";
  54. }
  55. }

From my understanding, this line of code is to assign the object to that formTemplateItem itself, but for some reason i kept on getting this error

" Progress.Sitefinity.RestSdk.Exceptions.ErrorCodeException: Specified method is not supported. "....... at line 185

 

  1. formTemplateItem.TemplateFile = new List<DocumentDto> { document };

And my line 185 is this

 

  1. await restClient.CreateItem(formTemplateItem);

I have tried a different approach but still not able to work

 

  1. formTemplateItem.TemplateFile = new[] { document };
  1. formTemplateItem.TemplateFile = new List<DocumentDto>
  2. {
  3. new DocumentDto { Id = document.Id }
  4. };

 

From within the controller, if I remove this line

  1. formTemplateItem.TemplateFile = new List<DocumentDto> { document };

Then ill be able to createItem, but the document will not be selected at all.