6.12. Implement ResourceΒΆ
Follow these step to create the plugin, item model and converter for a resource.
In Visual Studio, add the following class:
using System.Collections.Generic; using Sitecore.DataExchange; namespace DataExchange.Providers.RESTful.Plugins.Settings { public class ResourceSettings : IPlugin { public string Url { get; set; } public string Method { get; set; } public IEnumerable<RequestHeaderSettings> Headers { get; set; } public IEnumerable<RequestParameterSettings> Parameters { get; set; } public PagingSettings Paging { get; set; } } }
Add the following class:
using Sitecore.Services.Core.Model; namespace DataExchange.Providers.RESTful.Models.ItemModels.Settings { public class ResourceItemModel : ItemModel { public const string ResourceUrl = Templates.Resource.FieldNames.Url; public const string Method = Templates.Resource.FieldNames.Method; public const string Headers = Templates.Resource.FieldNames.Headers; public const string Parameters = Templates.Resource.FieldNames.Parameters; public const string Paging = Templates.Resource.FieldNames.Paging; } }
Note
ResourceUrl is used instead of Url because
public const string Url = "ItemUrl"
already exists inSitecore.Services.Core.Model.ItemModel
.Add the following class:
using System.Collections.Generic; using Sitecore.DataExchange; using Sitecore.DataExchange.Converters; using Sitecore.DataExchange.Extensions; using Sitecore.DataExchange.Repositories; using Sitecore.Services.Core.Model; using DataExchange.Providers.RESTful.Models.ItemModels.Settings; using DataExchange.Providers.RESTful.Plugins.Settings; namespace DataExchange.Providers.RESTful.Converters.Settings { public class ResourceConverter : BaseItemModelConverter<ResourceSettings> { public ResourceConverter(IItemModelRepository repository) : base(repository) { this.SupportedTemplateIds.Add(Templates.Resource.TemplateId); } protected override ConvertResult<ResourceSettings> ConvertSupportedItem(ItemModel source) { var resourceSettings = new ResourceSettings { Url = base.GetStringValue(source, ResourceItemModel.ResourceUrl), Method = base.GetStringValue(source, ResourceItemModel.Method), Headers = base.ConvertReferencesToModels<RequestHeaderSettings>(source, ResourceItemModel.Headers) ?? new List<RequestHeaderSettings>(), Parameters = base.ConvertReferencesToModels<RequestParameterSettings>(source, ResourceItemModel.Parameters) ?? new List<RequestParameterSettings>(), Paging = base.ConvertReferenceToModel<PagingSettings>(source, ResourceItemModel.Paging) }; if (resourceSettings.Url == null) Context.Logger.Error("No Url was specified in resource settings. (item: {0}, field: {1})", source.GetItemId(), Templates.Resource.FieldNames.Url); if (resourceSettings.Method == null) Context.Logger.Error("No method was specified in resource settings. (item: {0}, field: {1})", source.GetItemId(), Templates.Resource.FieldNames.Method); return this.PositiveResult(resourceSettings); } } }
Important
v1.4.1 or earlier: The
Sitecore.DataExchange.ConvertResult
class was introduced in Data Exchange Framework 2.0, and theConverter
classes were updated to use theConvertResult
class to track positive and negative results.using System.Collections.Generic; using Sitecore.DataExchange; using Sitecore.DataExchange.Converters; using Sitecore.DataExchange.Extensions; using Sitecore.DataExchange.Repositories; using Sitecore.Services.Core.Model; using DataExchange.Providers.RESTful.Models.ItemModels.Settings; using DataExchange.Providers.RESTful.Plugins.Settings; namespace DataExchange.Providers.RESTful.Converters.Settings { public class ResourceConverter : BaseItemModelConverter<ResourceSettings> { public ResourceConverter(IItemModelRepository repository) : base(repository) { this.SupportedTemplateIds.Add(Templates.Resource.TemplateId); } public override ResourceSettings Convert(ItemModel source) { var resourceSettings = new ResourceSettings { Url = base.GetStringValue(source, ResourceItemModel.ResourceUrl), Method = base.GetStringValue(source, ResourceItemModel.Method), Headers = base.ConvertReferencesToModels<RequestHeaderSettings>(source, ResourceItemModel.Headers) ?? new List<RequestHeaderSettings>(), Parameters = base.ConvertReferencesToModels<RequestParameterSettings>(source, ResourceItemModel.Parameters) ?? new List<RequestParameterSettings>(), Paging = base.ConvertReferenceToModel<PagingSettings>(source, ResourceItemModel.Paging) }; if (resourceSettings.Url == null) Context.Logger.Error("No Url was specified in resource settings. (item: {0}, field: {1})", source.GetItemId(), Templates.Resource.FieldNames.Url); if (resourceSettings.Method == null) Context.Logger.Error("No method was specified in resource settings. (item: {0}, field: {1})", source.GetItemId(), Templates.Resource.FieldNames.Method); return resourceSettings; } } }
Tip
Use the
ConvertReferenceToModel<T>
andConvertReferencesToModels<T>
methods from the base class for converters (Sitecore.DataExchange.Converters.BaseItemModelConverter<TTo>
) for Link Types to convert reference values to item models.Important
See Tip and Note from Implement Tenant Settings for more information about
templates.cs
.