public List <TargetedItemModel> GetTargetedItemsModel(string ItemsAlias = "targetedItems", string ItemImageAlias = "targetedItemImage", string ItemPageAlias = "targetedItemPage", string ItemNameAlias = "targetedItemName", string ItemDescriptionAlias = "targetedItemDescription") { List <TargetedItemModel> model = new List <TargetedItemModel>(); var targetedItems = _homePage.Value <IEnumerable <IPublishedElement> >(ItemsAlias); foreach (var item in targetedItems) { int imageId = item.Value <IPublishedContent>(ItemImageAlias).Id; var mediaItem = _uHelper.Media(imageId); string imageUrl = mediaItem.Url(); int pageId = item.Value <IPublishedContent>(ItemPageAlias).Id; IPublishedContent linkedToPage = _uHelper.Content(pageId); string linkUrl = linkedToPage.Url(); string name = item.Value <string>(ItemNameAlias); string category = item.Value <string>(ItemDescriptionAlias); model.Add(new TargetedItemModel(name, category, imageUrl, linkUrl)); } return(model); }
public ActionResult ArchiveByYearMonth(int year, int month, int?page) { IPublishedContent homePage = umbracoHelper.TypedContentAtRoot().FirstOrDefault(x => x.ContentType.Alias.Equals("home")); IPublishedContent blogPage = homePage.Children.Where(x => x.DocumentTypeAlias == "blogHome").FirstOrDefault(); var pageNumber = page ?? 1; List <BlogPost> blogResults = blogPage.Descendants <BlogPost>().ToList(); List <BlogPreview> blogPreviews = new List <BlogPreview>(); foreach (BlogPost post in blogResults.Where(r => r.CreateDate.Year == year && r.CreateDate.Month == month).OrderByDescending(x => x.CreateDate)) { int imageId = post.GetPropertyValue <int>("summaryImage"); var mediaItem = umbracoHelper.Media(imageId); blogPreviews.Add(new BlogPreview(post.Name, post.SummaryText, mediaItem.Url, post.Url, post.CreateDate, post.CreatorName)); } var date = new DateTime(year, month, 1); var urlSegment = string.Format("/blog/{0}/{1}", date.ToString("yyyy"), date.ToString("MM")); var infoTitle = date.ToString("MM yyyy") + " Archives"; var pageInfo = new HtmlMeta("SITE NAME", infoTitle, "", "http://www.example.com", "@OGSiteName", "@TwitterName"); BlogSearchResults searchResults = new BlogSearchResults(date.ToString("MMMM yyyy"), "Archives", urlSegment, blogPreviews.ToPagedList(pageNumber, PAGESIZE)); var viewModel = new SearchResultsViewModel { PageInfo = pageInfo, Results = searchResults }; return(View(viewModel)); }
public string GetUmbracoMediaUrl(IPublishedContent page, string propertyName) { int imageId = page.GetPropertyValue <int>(propertyName); var mediaItem = umbracoHelper.Media(imageId); return(mediaItem.Url); }
private string getNodeIdFromUdi(string media) { Current.Logger.Info(this.GetType(),"getNodeIdFromUdi(string media): value in media: "+media); var id = string.Empty; try { var udi = GuidUdi.Parse(media); //var typedContent = udi.ToPublishedContent(); var umbracoContext = UmbracoContext.Current == null ? EnsureUmbracoContext() : UmbracoContext.Current; var umbracoHelper = new UmbracoHelper(umbracoContext, Current.Services); if (udi.ToString().ToLower().Contains("document")) { var typedContent = umbracoHelper.Content(udi); id = typedContent?.Id.ToString(); } else if (udi.ToString().ToLower().Contains("media")) { var typedContent = umbracoHelper.Media(udi); id = typedContent?.Id.ToString(); } } catch (Exception msg) { Current.Logger.Error(this.GetType(), "Error while processing getNodeIdFromUdi(string media): value in media: " + media, msg); } return id; }
// Every page should have an image that represets that specific page. /* 1. Take image from the property "facebookShareImage" * 2. Else: take from the header/slider (if there is any) * 3. else: take the first image from the content * 4. else: take default share-image from our template * */ public static string PageMainImage(IPublishedContent page) { var umbraco = new UmbracoHelper(UmbracoContext.Current); var previewImage = ""; if (page.HasValue("facebookShareImage")) { previewImage = umbraco.Media(page.GetPropertyValue <string>("facebookShareImage")).Url; } if (string.IsNullOrEmpty(previewImage)) { var pageContent = PageMainContent(page); if (pageContent != null) { // Get the first image from the content previewImage = Regex.Match(pageContent, "<img.*?src=[\"'](.+?)[\"'].*?>", RegexOptions.IgnoreCase).Groups[1].Value; } } if (string.IsNullOrEmpty(previewImage)) { previewImage = "/img/default-social-share.jpg"; } return(previewImage.Split('?').First()); }
public List <FeaturedItem> GetFeaturedItemsModel() { //IPublishedContent homePage = CurrentPage.AncestorOrSelf(1).DescendantsOrSelf().Where(x => x.DocumentTypeAlias.Equals(HOME_PAGE_DOC_TYPE_ALIAS, StringComparison.OrdinalIgnoreCase)).FirstOrDefault(); IPublishedContent homePage = _currentPage.AncestorOrSelf(_homeDocTypeAlias); List <FeaturedItem> model = new List <FeaturedItem>(); ArchetypeModel featuredItems = homePage.GetPropertyValue <ArchetypeModel>(_featuredItemsAlias); foreach (ArchetypeFieldsetModel fieldSet in featuredItems) { //name, categoryAlias, image, page string name = fieldSet.GetValue <string>(_nameAlias); string category = fieldSet.GetValue <string>(_categoryAlias); int imageId = fieldSet.GetValue <int>(_imageAlias); int pageId = fieldSet.GetValue <int>(_pageAlias); var mediaItem = _uHelper.Media(imageId); string imageUrl = mediaItem.Url; IPublishedContent linkedToPage = _uHelper.TypedContent(pageId); string linkUrl = linkedToPage.Url; model.Add(new FeaturedItem() { Name = name, Category = category, ImageUrl = imageUrl, LinkUrl = linkUrl }); } return(model); }
/// <summary> /// Gets the media URL. /// </summary> /// <param name="nodeId">The node identifier.</param> /// <returns></returns> protected string GetMediaUrl(int nodeId) { UmbracoHelper helper = new UmbracoHelper(UmbracoContext.Current); dynamic content = helper.Media(nodeId); return(content.Url); }
public List <FeaturedItem> GetFeaturedItemsModel() { List <FeaturedItem> model = new List <FeaturedItem>(); //IPublishedContent homePage = Umbraco.AssignedContentItem.AncestorOrSelf(1); const int HOME_PAGE_POSITION_IN_PATH = 1; int homePageId = int.Parse(_currentPage.Path.Split(',')[HOME_PAGE_POSITION_IN_PATH]); IPublishedContent homePage = _uHelper.Content(homePageId); // get the value of hp - featured ArchetypeModel featuredItems = homePage.GetPropertyValue <ArchetypeModel>("featuredItems"); foreach (ArchetypeFieldsetModel fieldset in featuredItems) { var imageId = fieldset.GetValue <string>("image"); var mediaItem = _uHelper.Media(imageId); string imageUrl = mediaItem.Url; var pageId = fieldset.GetValue <string>("page"); IPublishedContent linkedToPage = _uHelper.TypedContent(pageId); string linkUrl = ""; if (linkedToPage != null) { linkUrl = linkedToPage.Url; } model.Add(new FeaturedItem(fieldset.GetValue <string>("name"), fieldset.GetValue <string>("category"), imageUrl, linkUrl)); } return(model); }
private string GetUmbracoMedia(UmbracoHelper helper, string value) { var media = helper.Media(value); return(media == null ? string.Empty : $"{HttpContext.Current.Request.Url.Scheme}://{HttpContext.Current.Request.Url.Authority}{media.Url}"); }
/// <summary> /// Get absolute url of Umbraco media file /// </summary> /// <param name="uMedia"></param> /// <returns></returns> protected string GetAbsoluteUrl(uModels.Media uMedia) { var umbHelper = new UmbracoHelper(UmbracoContext.Current); var content = umbHelper.Media(uMedia.Id); var imagerUrl = content.Url; return(imagerUrl); }
/// <summary> /// Get absolute url of Umbraco media file /// </summary> /// <param name="uMedia"></param> /// <returns></returns> public static string GetAbsoluteUrl(Media uMedia) { var url = uMedia.GetUrl("Image", null); UmbracoHelper umbHelper = Umbraco.Web.Composing.Current.UmbracoHelper; var content = umbHelper.Media(uMedia.Id); var imagerUrl = content?.Url; return(imagerUrl); }
public static IEnumerable <OpenGraphMetaData> ToList(this Models.OpenGraph opengraph) { var umbHelper = new UmbracoHelper(UmbracoContext.Current); var model = new List <OpenGraphMetaData>(); if (opengraph == null) { return(model); } if (opengraph.ImageId != null) { var media = umbHelper.Media(opengraph.ImageId); var mediaUrl = media.Url; if (!string.IsNullOrEmpty(mediaUrl)) { var url = UmbracoContext.Current .HttpContext.Request.Url?.GetLeftPart(UriPartial.Authority) .TrimEnd('/'); model.Add(new OpenGraphMetaData { Key = "og:image", Value = url + mediaUrl }); } } if (!string.IsNullOrWhiteSpace(opengraph.Title)) { model.Add(new OpenGraphMetaData { Key = "og:title", Value = opengraph.Title }); } if (!string.IsNullOrWhiteSpace(opengraph.Type)) { model.Add(new OpenGraphMetaData { Key = "og:type", Value = opengraph.Type }); } if (!string.IsNullOrWhiteSpace(opengraph.Description)) { model.Add(new OpenGraphMetaData { Key = "og:description", Value = opengraph.Description }); } model.AddRange(opengraph.Metadata); return(model); }
public static string GetMediaItemsFromCsvId(string csvMediaIds) { UmbracoHelper uHelper = new UmbracoHelper(UmbracoContext.Current); string mediaUrl = ""; var mediaItem = uHelper.Media(csvMediaIds); mediaUrl = mediaItem.umbracoFile; return(mediaUrl); }
private static string GetImage(IContent item) { if (!item.HasProperty(ImagePropName)) { return(""); } var umbHelper = new UmbracoHelper(UmbracoContext.Current); var content = umbHelper.Media(item.GetValue <string>(ImagePropName)); return(((string)content.Url).ToAbsoluteUrl()); }
private string getImageUrlFromMediaItem(string mediaId) { if (String.IsNullOrWhiteSpace(mediaId)) { return(String.Empty); } var umbracoHelper = new UmbracoHelper(UmbracoContext.Current); var image = umbracoHelper.Media(mediaId); return(image.url); }
public string GetImageUrl(int umbracoPageId, string propertyName) { var umbracoHelper = new UmbracoHelper(UmbracoContext.Current); IPublishedContent content = umbracoHelper.TypedContent(umbracoPageId); var imageId = content.GetPropertyValue(propertyName); var lull = content.GetProperty(propertyName); var aosdj = imageId.GetType(); var imageUrl = umbracoHelper.Media(imageId); var lpalspl = imageUrl.Content.Url; return(imageUrl); }
private static Article MapArticle(int nodeId) { var umbHelper = new UmbracoHelper(UmbracoContext.Current); var article = new Node(nodeId); return(!string.IsNullOrEmpty(article.Name) // unpublished articles returns null ? new Article { Id = article.Id, Url = article.Url, FeaturedImageUrl = ((string)umbHelper .Media(article.GetProperty <string>("listImage")).Url) .ToAbsoluteUrl(), Lead = article.GetProperty <string>("listShortDescArticle"), Title = article.GetProperty <string>("listArticleTitle"), } : null); }
public static string CroppedImageSource(CroppedImageModel model) { var umbracoHelper = new UmbracoHelper(UmbracoContext.Current); var image = umbracoHelper.Media(model.Id); if (model.SetCrop == null) { return(image.Url); } return(string.Format(System.Globalization.CultureInfo.GetCultureInfo("en-US"), "{0}?crop={1},{2},{3},{4}&cropmode=percentage&width={5}&height={6}", image.Url, model.SetCrop.Coordinates.X1, model.SetCrop.Coordinates.Y1, model.SetCrop.Coordinates.X2, model.SetCrop.Coordinates.Y2, model.SetCrop.Width, model.SetCrop.Height)); }
public static string GetMediaUrl(this IPublishedContent content, string propertyName) { if (content != null) { int mediaId = content.GetPropertyValue <int>(propertyName); if (mediaId != 0) { var umbracoHelper = new UmbracoHelper(UmbracoContext.Current); var media = umbracoHelper.Media(mediaId); if (media != null) { return(media.Url); } } } return(string.Empty); }
//private System.Drawing.Image DownloadImageFromUrl(string imageUrl) //{ // System.Drawing.Image image = null; // try // { // var webRequest = (HttpWebRequest)WebRequest.Create(imageUrl); // webRequest.AllowWriteStreamBuffering = true; // webRequest.Timeout = 30000; // var webResponse = webRequest.GetResponse(); // var stream = webResponse.GetResponseStream(); // image = System.Drawing.Image.FromStream(stream); // webResponse.Close(); // } // catch (Exception ex) // { // // log the exception // } // return image; //} public static IPublishedContent AddImageByFile(IMediaService mediaService, IContentTypeBaseServiceProvider contentTypeBaseServiceProvider, UmbracoHelper umbraco, string filename, string tempfile) { _mediaService = mediaService; string name = System.IO.Path.GetFileNameWithoutExtension(filename); // - place them inside of a folder! IMedia folder = CreateFolder(_foldername); // - create media item in this folder, save file data and persist var media = _mediaService.CreateMedia(name, folder, "image"); var buffer = System.IO.File.ReadAllBytes(tempfile); media.SetValue(contentTypeBaseServiceProvider, "umbracoFile", filename, new MemoryStream(buffer)); _mediaService.Save(media); // - remove temp file! if (System.IO.File.Exists(tempfile)) { System.IO.File.Delete(tempfile); } return(umbraco.Media(media.Id)); }
// Method Two private string GetMediaFromUmbracoHelper() { return(_helper.Media(2182)?.Url()); }
private IPublishedContent GetMediaContent(string udi) { return(uh.Media(GetIdForUdi(udi))); }
/// <summary> /// Gets thumbnail url. /// Checks if thumbnail is defined in post, if not - returns crop of main image, if it is absent - default thumbnail image crop. /// /// Priority: 1.Thumbnail 2.Image 3.Default site thumbnail /// /// TODO: checking for crops and creating files from coordinates, get solution to helper problem (use services) /// </summary> /// <param name="post"></param> /// <param name="cropName"></param> /// <param name="cropThumb">use thumbnail crop if set to true</param> /// <param name="useDefaultThumb">use default image if thumb is not found</param> /// <returns>thumbnail url</returns> public static String GetThumbnailUrl(IPublishedContent post, String cropName = "", bool cropThumb = false) { String result = ""; var umbracoHelper = new UmbracoHelper(UmbracoContext.Current); try { var thumbnailID = post.GetPropertyValue("thumbnail"); if (thumbnailID == null || thumbnailID == String.Empty) { //no thumbnail var pictureID = post.GetPropertyValue("image"); if (pictureID != String.Empty && pictureID != null) { var picture = umbracoHelper.Media(pictureID); if (cropName != "") { var thumbnail = picture.AsDynamic().imageCropper.crops.Find("@name", cropName); result = thumbnail.url; } } else { //return default thumb IMediaService mediaService = ApplicationContext.Current.Services.MediaService; IEnumerable<IMedia> buffer = mediaService.GetByLevel(1).Where(x => x.Name == "Default"); if (buffer.Any()) { IMedia defaultFolder = buffer.First(); buffer = mediaService.GetChildren(defaultFolder.Id); if (buffer.Any()) { buffer = buffer.Where(x => x.Name == "default-thumbnail"); if (buffer.Any() && cropName != "") { IMedia defaultThumb = buffer.First(); result = GetCrop(defaultThumb.GetValue<String>("imageCropper"), cropName); } } } } } else { var thumbnail = umbracoHelper.Media(thumbnailID); result = thumbnail.Url; if (cropThumb && cropName != "") { thumbnail = thumbnail.AsDynamic().imageCropper.crops.Find("@name", cropName); result = thumbnail.url; } } } catch (Exception e) { if (UmbracoContext.Current.IsDebug) { //throw e; } } return result; }
public string Test() { //creating an object from result class result result = new result(); var cs = Services.ContentService; // getting children of sports var content = cs.GetChildren(1082); dynamic obj = new JObject(); var objplayer = new JObject { { "player", new JObject { { "playerguid", "" }, { "playerid", "" }, { "playername", "" } } } }; int i = 0; int i2 = 0; int i3 = 0; int i4 = 0; int i5 = 0; int i6 = 0; int i7 = 0; foreach (var item in content) { var j = item.GetValue <Udi>("imagepost"); var smt = Umbraco.GetIdForUdi(j); var umbHelper = new UmbracoHelper(UmbracoContext.Current); var k = umbHelper.Media(smt); obj["sport" + i] = new JObject { { "SportName", item.GetValue <string>("namesport") }, { "SportPic", k.Url }, { "clubs", new JObject { } } }; var content2 = cs.GetChildren(item.Id); // getting clubs foreach (var item2 in content2) { obj["sport" + i]["clubs"]["club" + i2] = new JObject { { "ClubName", item2.GetValue <string>("clubName") }, { "teams", new JObject { } } }; var content3 = cs.GetChildren(item2.Id); foreach (var item3 in content3) //getting teams { var j1 = item3.GetValue <Udi>("imageTeam"); var smt1 = Umbraco.GetIdForUdi(j1); var k1 = umbHelper.Media(smt1); obj["sport" + i]["clubs"]["club" + i2]["teams"]["team" + i3] = new JObject { { "teamName", item3.GetValue <string>("nameTeam") }, { "teamPic", k1.Url }, { "matches", new JObject { } } }; var children = cs.GetChildren(item3.Id); //getting 2nodes from teams foreach (var item4 in children) { var matches = cs.GetChildren(item4.Id); if (item4.ContentType.Alias == "players") //if players get all players and store in array { var playerchildren = item4.Id; var playerall = cs.GetChildren(playerchildren); foreach (var item5 in playerall) { var key = item5.Key.ToString(); key = key.Replace("umb://document/", ""); key = key.Replace("-", ""); if (i6 == 0) { objplayer["player"] = new JObject { { "playerid", item5.Id }, { "playerguid", key }, { "playername", item5.GetValue <string>("namePlayer") } }; } else { objplayer["player" + item5.Id] = new JObject { { "playerid", item5.Id }, { "playerguid", key }, { "playername", item5.GetValue <string>("namePlayer") } }; } i6++; } } if (item4.ContentType.Alias == "matches") //if matches get selected players and compare { foreach (var item6 in matches) { var playerpick = item6.GetValue <string>("playerpicker"); string[] tokens = playerpick.Split(','); List <string> token = new List <string> { }; foreach (var item7 in tokens) { token.Add(tokens[i5].Replace("umb://document/", "")); i5++; } obj["sport" + i]["clubs"]["club" + i2]["teams"]["team" + i3]["matches"]["match" + i4] = new JObject { { "startDate", item6.GetValue <DateTime>("startDate") }, { "endDate", item6.GetValue <DateTime>("endDate") }, { "players", new JObject() } }; //compare start // objplayer jsonobject // token list //var hij = 0; //foreach something in objplayer //get player guid //compare to token if equal //push other values to obj foreach (var item8 in objplayer) { var playerguid = item8.Value["playerguid"].ToString(); if (token.Contains(playerguid)) { result.Name = playerguid; obj["sport" + i]["clubs"]["club" + i2]["teams"]["team" + i3]["matches"]["match" + i4]["players"]["player" + i7] = new JObject { { "playerid", item8.Value["playerid"].ToString() }, { "playername", item8.Value["playername"].ToString() } }; } i7++; } //bool result1 = root.equals(root2); //int comparison = root.compareto(root2); //result1 = root.equals(root2, stringcomparison.ordinal); //if (result1 == true) //{ // hij = result.id; //} //var smt = cs.getbyid(hij); //compare end i4++; } } } i3++; } i2++; } i++; } var output = JsonConvert.SerializeObject(obj); return(output); }
public static BlogEntry TranslateSearchResultToBlogEntry(IPublishedContent results) { var helper = new UmbracoHelper(UmbracoContext.Current); var post = new BlogEntry(); post.Title = results.GetPropertyValue<string>("uBlogsyContentTitle"); var auth = helper.Content(results.GetPropertyValue<string>("uBlogsyPostAuthor")); post.Author = auth.uBlogsyAuthorName; var date = results.GetPropertyValue<string>("uBlogsyPostDate"); post.Date = DateTime.Parse(date).ToString("D"); var image = results.GetPropertyValue<string>("uBlogsyPostImage"); if (!string.IsNullOrEmpty(image)) post.Image = helper.Media(image).umbracoFile; post.Summary = results.GetPropertyValue<string>("uBlogsyContentSummary"); post.Url = results.Url(); return post; }