
Raymond Ray (Customer) asked a question.
Using Sitefinity Renderer how can i select that uploaded document dynamically on using restClient.CreateItem();
DTO
- using Progress.Sitefinity.RestSdk.Dto;
-
- namespace Renderer.Dto
- {
- [MappedSitefinityType("Telerik.Sitefinity.DynamicTypes.Model.FormsandTemplates.FormAndTemplate")]
- public class FormsAndTemplatesDto : SdkItem
- {
- public string Title { get; set; }
- public string Content { get; set; }
- public string ItemDefaultUrl { get; set; }
-
- public string[] Category { get; set; }
- public IEnumerable<DocumentDto> TemplateFile { get; set; }
- }
- }
Controller
- var formTemplateItem = new FormsAndTemplatesDto
- {
- Title = worksheet.Cells[row, 4].Value.ToString(),
- Category = categoryIds,
- };
-
- // Upload the new document
- var document = await MediaHelper.UploadDocument(
- restClient,
- fileStream,
- libraryId,
- fileName,
- Path.GetFileNameWithoutExtension(fileName)
- );
-
- formTemplateItem.TemplateFile = new List<DocumentDto> { document };
- .
- .
- .
- //Outside Loop
- formTemplateItems.Add(formTemplateItem);
-
-
- foreach (var formTemplateItem in formTemplateItems)
- {
-
- var existingItemsResponse = await this.restClient.GetItems<FormsAndTemplatesDto>(new GetAllArgs()
- {
- Fields = new[] { "*" },
- Filter = $"Title='{formTemplateItem.Title}'",
- Take = 1
- });
-
- var existingItems = existingItemsResponse.Items;
-
- if (existingItems.Count > 0)
- {
- // Update
- var existingItem = existingItems[0];
- formTemplateItem.Id = existingItem.Id;
- await restClient.UpdateItem(formTemplateItem);
- }
- else
- {
- // Create
- await restClient.CreateItem(formTemplateItem);
- }
- }
-
-
MediaHelper.cs - source (here)
- using System;
- using Microsoft.AspNetCore.StaticFiles;
- using Newtonsoft.Json.Linq;
- using Progress.Sitefinity.RestSdk;
- using Progress.Sitefinity.RestSdk.Clients.Sites.Dto;
- using Progress.Sitefinity.RestSdk.Dto;
- using Newtonsoft.Json;
- using Newtonsoft.Json.Linq;
- using Microsoft.AspNetCore.StaticFiles;
-
- public static class MediaHelper
- {
- //Source-https://community.progress.com/s/article/net-core-how-to-upload-file-in-sitefinity-from-the-renderer-application
- public static async Task<DocumentDto> UploadDocument(IRestClient restClient, Stream file, Guid libraryId, string fileName, string documentTitle, string alternativeText = "")
- {
- var headers = GetUploadHeaders(fileName, libraryId, documentTitle, alternativeText);
-
- var args = new CreateArgs()
- {
- Data = GetBase64Content(file),
- ContentType = GetContentType(fileName),
- Encoding = "base64",
- AdditionalHeaders = headers,
- };
-
- // Return the created DocumentDto
- return await restClient.CreateItem<DocumentDto>(args);
- }
-
- private static Dictionary<string, string> GetUploadHeaders(string fileName, Guid parentId, string title, string alt = "")
- {
- var additionalHeaders = new Dictionary<string, string>();
- var headersJson = JObject.FromObject(new
- {
- Title = title,
- AlternativeText = alt,
- ParentId = parentId,
- DirectUpload = true,
- }).ToString(Formatting.None);
- additionalHeaders.Add("X-Sf-Properties", headersJson);
- additionalHeaders["X-File-Name"] = fileName;
-
- return additionalHeaders;
- }
-
- private static string GetBase64Content(Stream stream)
- {
- byte[] byteArray;
- using (BinaryReader br = new BinaryReader(stream))
- {
- byteArray = br.ReadBytes((int)stream.Length);
- }
-
- return Convert.ToBase64String(byteArray);
- }
-
- private static string GetContentType(string fileName)
- {
- string contentType;
- new FileExtensionContentTypeProvider().TryGetContentType(fileName, out contentType);
- return contentType ?? "application/octet-stream";
- }
-
- }
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
- formTemplateItem.TemplateFile = new List<DocumentDto> { document };
And my line 185 is this
- await restClient.CreateItem(formTemplateItem);
I have tried a different approach but still not able to work
- formTemplateItem.TemplateFile = new[] { document };
- formTemplateItem.TemplateFile = new List<DocumentDto>
- {
- new DocumentDto { Id = document.Id }
- };
From within the controller, if I remove this line
- formTemplateItem.TemplateFile = new List<DocumentDto> { document };
Then ill be able to createItem, but the document will not be selected at all.
If you are in fact trying to related a document to a content item you need to use the restClient.RelateItem() method as shown in the following code sample.
https://github.com/Sitefinity/sitefinity-aspnetcore-mvc-samples/blob/master/src/migrate-data-to-cms/Samples/Relations/RelateMedia.cs#L36