Error executing template "/Designs/Scanpan/Paragraph/RaptorRecommendations.cshtml"
System.Net.WebException: The remote server returned an error: (401) Unauthorized.
at System.Net.WebClient.DownloadDataInternal(Uri address, WebRequest& request)
at System.Net.WebClient.DownloadString(Uri address)
at CompiledRazorTemplates.Dynamic.RazorEngine_a46985e9c24a4793bf51eaebd9f52c11.Execute() in E:\dynamicweb.net\Solutions\scanpan-live\Files\Templates\Designs\Scanpan\Paragraph\RaptorRecommendations.cshtml:line 46
at RazorEngine.Templating.TemplateBase.RazorEngine.Templating.ITemplate.Run(ExecuteContext context, TextWriter reader)
at RazorEngine.Templating.RazorEngineService.RunCompile(ITemplateKey key, TextWriter writer, Type modelType, Object model, DynamicViewBag viewBag)
at RazorEngine.Templating.RazorEngineServiceExtensions.<>c__DisplayClass16_0.<RunCompile>b__0(TextWriter writer)
at RazorEngine.Templating.RazorEngineServiceExtensions.WithWriter(Action`1 withWriter)
at Dynamicweb.Rendering.RazorTemplateRenderingProvider.Render(Template template)
at Dynamicweb.Rendering.TemplateRenderingService.Render(Template template)
at Dynamicweb.Rendering.Template.RenderRazorTemplate()
1 @inherits RazorTemplateBase<RazorTemplateModel<Template>>
2
3 @using System
4 @using System.Collections.Generic
5 @using System.Linq
6 @using Dynamicweb.Ecommerce.Products
7 @using Dynamicweb.Ecommerce.Variants
8 @using Newtonsoft.Json;
9 @using Dynamicweb.Rendering;
10 @using System.Web;
11 @using System.Net;
12 @using Dynamicweb.Ecommerce.International
13 @using NameValueCollection = System.Collections.Specialized.NameValueCollection
14
15 @{
16 string raptorApi = GetString("Item.RaptorApi");
17 string productId = HttpContext.Current.Request.QueryString["productid"];
18 string categoryId = HttpContext.Current.Request.QueryString["categoryid"];
19 string cookieId = HttpContext.Current.Request.QueryString["cookieid"];
20
21 // Build new url depended on the query string parameters
22 UriBuilder urlBuilderRaptor = new UriBuilder(raptorApi);
23 NameValueCollection valuesFromQueryString = HttpUtility.ParseQueryString(urlBuilderRaptor.Query);
24 if (!string.IsNullOrEmpty(productId))
25 {
26 // Change product id
27 valuesFromQueryString.Remove("productid");
28 valuesFromQueryString.Add("productid", productId);
29 }
30 if (!string.IsNullOrEmpty(cookieId))
31 {
32 // Change cookie id
33 valuesFromQueryString.Remove("cookieid");
34 valuesFromQueryString.Add("cookieid", cookieId);
35 }
36 if (!string.IsNullOrEmpty(categoryId))
37 {
38 // Change category id
39 valuesFromQueryString.Remove("categoryid");
40 valuesFromQueryString.Add("categoryid", categoryId);
41 }
42 urlBuilderRaptor.Query = valuesFromQueryString.ToString();
43 raptorApi = urlBuilderRaptor.ToString();
44
45 // Load json from raptor query string
46 string jsonRaptorContent = new WebClient().DownloadString(raptorApi);
47 dynamic raptorItems = JsonConvert.DeserializeObject(jsonRaptorContent);
48 bool disableEcommerce = bool.Parse(Pageview.Area.Item["DisableEcommerce"].ToString());
49 var productService = new ProductService();
50 var languageService = new LanguageService();
51 IEnumerable<Product> productsInShop = productService.GetAllProductsByShopId("SHOP1", false);
52 int counter = 0;
53 }
54
55 <section class="product-list js-raptor-product-list" data-raptortype="@(raptorApi.Split('/')[5])">
56 @if (!string.IsNullOrEmpty(GetString("Item.Headline")))
57 {
58 <h1 class="product-list__raptor-headline">@GetString("Item.Headline")</h1>
59 }
60 <div class="product-list__container">
61
62 @foreach (var item in raptorItems)
63 {
64 string productNumber = item.RecommendedId;
65 Product productInShop = productsInShop.FirstOrDefault(prod => prod.Number.Equals(productNumber));
66
67 if (productInShop != null)
68 {
69 Product product = productService.GetProductById(productInShop.Id, productInShop.VariantId, languageService.GetDefaultLanguageId());
70
71 if (product != null && product.VariantCombinations.FirstOrDefault(x => x.Product.Number.Equals(productNumber)).Product != null)
72 {
73 Product variant = product.VariantCombinations.First(x => x.Product.Number.Equals(productNumber)).Product;
74 // Check that variant has price and is in stock
75 // Render max 16 items
76 if (variant.Price.Price <= 0 || variant.Stock <= 0 || counter >= 16)
77 {
78 continue;
79 }
80
81 string imagePath = "/Files/Templates/Designs/Scanpan/assets/images/noimage.gif";
82
83 string imageUrl = imagePath;
84 string imageUrl2x = imagePath;
85
86 if (Pageview.Area.ID == 1)
87 {
88 // Media product images
89 string mainImage = variant.ProductFieldValues.GetProductFieldValue("ProductImage").Value.ToString();
90
91 if (!string.IsNullOrEmpty(mainImage))
92 {
93 imagePath = string.Format("https://cdn.scanpan.dk/Perfion/Image.aspx?id={0}", mainImage);
94 imageUrl = imagePath + "&size=250x160";
95 imageUrl2x = imagePath + "&size=500x320";
96 }
97 }
98 else
99 {
100 var imageHandlerUrl = "/Files/Billeder/MediaDB/Originals/ProductImage/";
101 var imageHandlerFallback = "&altFmImage_path=/Files/Templates/Designs/Scanpan/assets/images/noimage.gif";
102 var imageHandlerSettings = "?height=160&mode=boxpad&format=jpg&quality=80";
103 var imageHandlerSettings2x = "?height=320&mode=boxpad&format=jpg&quality=80";
104
105 imageUrl = imageHandlerUrl + productNumber + ".jpg" + imageHandlerSettings;
106 imageUrl2x = imageHandlerUrl + productNumber + ".jpg" + imageHandlerSettings2x;
107 }
108
109 counter++;
110 string productLink = "/" + Pageview.Area.Culture + "/shop?ProductId=" + product.Id + "&VariantId=" + variant.VariantId;
111 // Build custom variant text due to some variants missing variant text
112 List<string> variantTexts = new List<string>() {
113 variant.ProductFieldValues.GetProductFieldValue("SIZ").Value.ToString(),
114 variant.ProductFieldValues.GetProductFieldValue("VOL").Value.ToString(),
115 variant.ProductFieldValues.GetProductFieldValue("COL").Value.ToString()
116 };
117 string variantText = string.Join(", ", variantTexts.Where(i => !string.IsNullOrEmpty(i)));
118
119 <article class="product-list__item js-filter-product js-raptor-item">
120 <a class="product-list__image-container js-raptor-item-click" href="@productLink" data-productid="@variant.Number">
121 <img class="product-list__image" srcset="@imageUrl, @imageUrl2x 2x" alt="@variant.Name">
122 </a>
123
124 <h3 class="product-list__name" title="@variant.Name">@variant.Name</h3>
125 <div class="product-list__offer">
126 @if (!disableEcommerce)
127 {
128 double oldPrice = Convert.ToDouble(variant.ProductFieldValues.GetProductFieldValue("UnitListPrice").Value) * 1.25;
129 string oldPriceFormatted = oldPrice.ToString("N");
130 string price = variant.Price.Price.ToString();
131 string priceFormatted = variant.Price.Price.ToString("N");
132
133 <div class="product-list__price">
134 @if (oldPrice.ToString() != price)
135 {
136 <div class="product-list__price__current">@priceFormatted</div>
137 <div class="product-list__price__old">@oldPriceFormatted</div>
138 }
139 else
140 {
141 <div class="product-list__price__current">@priceFormatted</div>
142 <div class="product-list__price-spacer"> </div>
143 }
144 </div>
145 }
146 <span class="product-list__sizes">@variantText</span>
147 </div>
148
149 @if (!disableEcommerce)
150 {
151 <a class="product-list__raptor-button js-raptor-item-click" href="@productLink" data-productid="@variant.Number">@Translate("RaptorReadMoreButton")</a>
152 <a class="product-list__raptor-buy-button js-product-ajax-button" href="@productLink&cartcmd=add">@Translate("RaptorBuyButton")</a>
153 }
154 </article>
155 }
156 }
157 }
158
159 </div>
160 </section>
161