public ActionResult PageTitle() { var pageData = _scContext.GetCurrentItem <IPageTitle>(); var viewModel = new PageTitleViewModel { Title = _builder.BuildHtmlString(pageData, p => p.Title).ToString(), Summary = _builder.BuildHtmlString(pageData, p => p.Summary).ToString() }; return(this.React("PageTitle", viewModel)); }
public string GetEndBodyScripts() { StringBuilder endBodyScripts = new StringBuilder(); endBodyScripts.Append(_settings.SiteSettings.EndBodyScripts); /* page specific */ IScripts pageScripts = _currentContext.GetCurrentItem <IScripts>(); endBodyScripts.Append(pageScripts.EndBodyScripts); return(endBodyScripts.ToString()); }
public void CurrentItemInList_MarkedActive() { IBaseItem item1, item2, item3; item1 = Substitute.For <IBaseItem>(); item2 = Substitute.For <IBaseItem>(); item3 = Substitute.For <IBaseItem>(); Guid id1, id2, id3; id1 = Guid.NewGuid(); id2 = Guid.NewGuid(); id3 = Guid.NewGuid(); item1.Id.Returns(id1); item2.Id.Returns(id2); item3.Id.Returns(id3); _context.GetHomeItem <IBaseItem>().Children.Returns(new List <IBaseItem> { item1, item2, item3 }); _context.GetCurrentItem <IBaseItem>().Id.Returns(id2); ViewResult result = _sut.Index() as ViewResult; var resultCollection = result.Model as IEnumerable <Models.NavElement>; resultCollection.Should().HaveCount(3); var list = resultCollection.ToArray(); list[0].Active.Should().BeFalse(); list[1].Active.Should().BeTrue(); list[2].Active.Should().BeFalse(); }
private IGlassBase GetInstanceFromContext(Rendering rendering, ISitecoreContext sitecoreContext) { string datasource = rendering.DataSource; Type glassBaseType = typeof(GlassBase); if (!datasource.IsNotNullOrEmpty()) { return((IGlassBase)sitecoreContext.GetCurrentItem(glassBaseType, inferType: true)); } Item item = sitecoreContext.Database.GetItem(datasource); return((IGlassBase)sitecoreContext.CreateType(glassBaseType, item, true, true, null, null)); }
public string GetCanonicalLink() { string canonicalLink = ""; ICanonical currentPage = _currentContext.GetCurrentItem <ICanonical>(); if (currentPage != null && currentPage.CanonicalLink != null && !string.IsNullOrEmpty(currentPage.CanonicalLink.Url)) { UrlString url = new UrlString(currentPage.CanonicalLink.Url); url.HostName = _sitecoreSiteLogic.TargetHostName; canonicalLink = "<link rel=\"canonical\" href=\"" + url.ToString() + "\"/>"; } return(canonicalLink); }
// GET: Navbar public ActionResult Index() { var home = _sitecoreContext.GetHomeItem <IBaseItem>(); var current = _sitecoreContext.GetCurrentItem <IBaseItem>(); List <NavElement> list = new List <NavElement>(); foreach (var child in home.Children) { list.Add(new NavElement { Text = child.Name, Href = child.Url, Active = child.Id == current.Id }); } return(View("~/Views/Navbar.cshtml", list)); }
public CallToActionViewModel( ITextTranslator textTranslator, ISignInViewModel signInViewModel, IRegisterPopOutViewModel registerPopOutViewModel, ISiteRootContext siteRootContext, IAuthenticatedUserContext authenticatedUserContext, ISitecoreContext sitecoreContext) { TextTranslator = textTranslator; SignInViewModel = signInViewModel; RegisterPopOutViewModel = registerPopOutViewModel; SiteRootContext = siteRootContext; AuthenticatedUserContext = authenticatedUserContext; CurrentItem = sitecoreContext.GetCurrentItem <IArticle>(); }
/// <summary> /// redirects all article urls starting with /article /// </summary> /// <param name="path"></param> public void Get(string year, string month, string day, string title) { IArticle a = SitecoreContext.GetCurrentItem <IArticle>(); if (a == null) { return; } string newPath = ArticleSearch.GetArticleCustomPath(a); if (!HttpContext.Current.Response.IsRequestBeingRedirected) { HttpContext.Current.Response.RedirectPermanent(newPath, true); } }
/// <summary> /// Called by the ASP.NET page framework to notify server controls that use composition-based implementation to create any child controls they contain in preparation for posting back or rendering. /// </summary> protected override void CreateChildControls() { Type type = Type.GetType(TypeName); Type razorControlType = typeof(AbstractRazorControl <>); Type finalControlType = razorControlType.MakeGenericType(type); WebControl finalControl = Activator.CreateInstance(finalControlType) as WebControl; ISitecoreContext _context = Sc.SitecoreContext.GetFromHttpContext(Context); var model = _context.GetCurrentItem(type); TypeDescriptor.GetProperties(finalControlType).Find("Model", false).SetValue(finalControl, model); this.Controls.Add(finalControl); base.CreateChildControls(); }
public SearchPageParser(string pageId, ISitecoreContext context, ISearchRuleParser ruleParser) { IGlassBase page = null; if (!string.IsNullOrEmpty(pageId)) { page = context.GetItem <IGlassBase>(pageId, inferType: true); } if (page == null) { page = context.GetCurrentItem <IGlassBase>(inferType: true); } if (!(page is I_Listing_Configuration) && RenderingContext.CurrentOrNull != null) { page = context.GetItem <IGlassBase>(RenderingContext.Current.Rendering.DataSource, inferType: true); } Initialize(page, ruleParser); }
public void GetCurrentRenderingDatasource_NestingEnabled_NoDirectDatasourceOrStaticItem_WithNeverNesting_ReturnsContextItem() { var props = new RenderingProperties(_rendering); using (ShimsContext.Create()) { var fakeItem = CreateFakeItem(_nestedItemDatasource._Id); // Setup rendering params: no direct datasource, static item is set _rendering.DataSource.Returns(ci => null); _rendering.Properties.Returns(ci => props); props["ItemId"] = null; // simulates no StaticItem _renderingContext.ContextItem.Returns(fakeItem); // A nested datasource item IS set... _sitecoreContext.GetCurrentItem <IGlassBase>(inferType: true).Returns(_contextItem); var datasource = _renderingService.GetCurrentRenderingDatasource <IGlassBase>(DatasourceNestingOptions.Never); Assert.AreEqual(_contextItem._Id, datasource._Id); } }
public override void Process(GetModelArgs args) { var key = "GetModelFromView"; var watch = Stopwatch.StartNew(); try { if (!IsValidForProcessing(args)) { return; } string path = GetViewPath(args); if (string.IsNullOrWhiteSpace(path)) { return; } string cacheKey = modelCacheManager.GetKey(path); Type modelType = modelCacheManager.Get(cacheKey); if (modelType == typeof(NullModel)) { // The model has been attempted before and is not useful return; } // The model type hasn't been found before or has been cleared. if (modelType == null) { modelType = GetModel(args, path); modelCacheManager.Add(cacheKey, modelType); if (modelType == typeof(NullModel)) { // This is not the type we are looking for return; } } ISitecoreContext scContext = SitecoreContextFactory.GetSitecoreContext(); Rendering renderingItem = args.Rendering; object model = null; if (renderingItem.DataSource.HasValue()) { var item = scContext.Database.GetItem(renderingItem.DataSource); model = scContext.CreateType(modelType, item, false, false, null); } else if (renderingItem.RenderingItem.DataSource.HasValue()) { var item = scContext.Database.GetItem(renderingItem.RenderingItem.DataSource); model = scContext.CreateType(modelType, item, false, false, null); } else if (renderingItem.Item != null) { /** * Issues #82: * Check Item before defaulting to the current item. */ model = scContext.CreateType(modelType, renderingItem.Item, false, false, null); } else { model = scContext.GetCurrentItem(modelType); } args.Result = model; } finally { Sitecore.Diagnostics.Log.Debug("GetModelFromView {0} {1}".Formatted(watch.ElapsedMilliseconds, args.Rendering.RenderingItem.ID), this); } }
/// <summary> /// Gets the object. /// </summary> /// <param name="model">The model.</param> /// <param name="db">The db.</param> /// <returns></returns> /// <exception cref="Glass.Mapper.MapperException">Failed to find context {0}.Formatted(ContextName)</exception> public virtual object GetObject(string model, Database db, Rendering renderingItem) { if (model.IsNullOrEmpty()) { return(null); } //must be a path to a Model item if (model.StartsWith("/sitecore")) { var target = db.GetItem(model); if (target == null) { return(null); } string newModel = target[ModelTypeField]; return(GetObject(newModel, db, renderingItem)); } //if guid must be that to Model item Guid targetId; if (Guid.TryParse(model, out targetId)) { var target = db.GetItem(new ID(targetId)); if (target == null) { return(null); } string newModel = target[ModelTypeField]; return(GetObject(newModel, db, renderingItem)); } var type = Type.GetType(model, false); if (type == null || renderingModelType.IsAssignableFrom(type)) { return(null); } ISitecoreContext scContext = SitecoreContextFactory.GetSitecoreContext(); //this is really aggressive if (!scContext.GlassContext.TypeConfigurations.ContainsKey(type)) { //if the config is null then it is probably an ondemand mapping so we have to load the ondemand part IConfigurationLoader loader = new OnDemandLoader <SitecoreTypeConfiguration>(type); scContext.GlassContext.Load(loader); } if (renderingItem.DataSource.IsNotNullOrEmpty()) { var item = scContext.Database.GetItem(renderingItem.DataSource); return(scContext.CreateType(type, item, false, false, null)); } if (renderingItem.RenderingItem.DataSource.HasValue()) { var item = scContext.Database.GetItem(renderingItem.RenderingItem.DataSource); return(scContext.CreateType(type, item, false, false, null)); } /** * Issues #82: * Check Item before defaulting to the current item. */ if (renderingItem.Item != null) { return(scContext.CreateType(type, renderingItem.Item, false, false, null)); } return(scContext.GetCurrentItem(type)); }
public override object GetResult(ValueProviderArgs args) { return(_sitecoreContext.GetCurrentItem(args.RequestedType, true, true)); }
public ActionResult Index() { var item = _context.GetCurrentItem <Page>(); return(View("~/Views/GlassExample/Index.cshtml", item)); }
public override AgendaModel GetModel() { _context = new SitecoreContext(); return(_context.GetCurrentItem <AgendaModel>()); }
public override ICallToActionBlocks GetModel() { _context = new SitecoreContext(); return(_context.GetCurrentItem <ICallToActionBlocks>()); }
public T Get <T>() where T : class, IGlassBase { return(RenderingContext.HasDataSource ? GlobalService.GetItem <T>(RenderingContext.GetDataSource()) : SitecoreContext.GetCurrentItem <T>()); }
public T GetCurrentItem() { return(_context.GetCurrentItem <T>()); }
public override void Process(GetModelArgs args) { if (!IsValidForProcessing(args)) { return; } string path = GetViewPath(args); if (string.IsNullOrWhiteSpace(path)) { return; } string cacheKey = modelCacheManager.GetKey(path); Type modelType = modelCacheManager.Get(cacheKey); if (modelType == typeof(NullModel)) { // The model has been attempted before and is not useful return; } // The model type hasn't been found before or has been cleared. if (modelType == null) { var fullPath = HttpContext.Current.Server.MapPath(path); if (File.Exists(fullPath)) { modelType = GetModel(args, path); } else { modelType = typeof(NullModel); } modelCacheManager.Add(cacheKey, modelType); if (modelType == typeof(NullModel)) { // This is not the type we are looking for return; } } ISitecoreContext scContext = SitecoreContextFactory.GetSitecoreContext(); Rendering renderingItem = args.Rendering; object model = null; if (renderingItem.DataSource.HasValue()) { var item = scContext.Database.GetItem(renderingItem.DataSource); model = scContext.CreateType(modelType, item, false, false, null); } else if (renderingItem.RenderingItem.DataSource.HasValue()) { var item = scContext.Database.GetItem(renderingItem.RenderingItem.DataSource); model = scContext.CreateType(modelType, item, false, false, null); } else if (renderingItem.Item != null) { /** * Issues #82: * Check Item before defaulting to the current item. */ model = scContext.CreateType(modelType, renderingItem.Item, false, false, null); } else { model = scContext.GetCurrentItem(modelType); } args.Result = model; }
public T GetCurrentItem<T>() where T : class { return _sitecoreContext.GetCurrentItem<T>(); }
public AccountNavViewModel(ISitecoreContext sitecoreContext) { _currentItem = sitecoreContext.GetCurrentItem <IGlassBase>(); NavigationItems = _currentItem._Parent._ChildrenWithInferType.OfType <I___BasePage>(); }
public SearchPageParser(ISitecoreContext context, ISearchRuleParser ruleParser) { Initialize(context.GetCurrentItem <IGlassBase>(inferType: true), ruleParser); }
public T GetCurrentRenderingDatasource <T>(DatasourceNestingOptions options = DatasourceNestingOptions.Default) where T : class, IGlassBase { var rendering = GetCurrentRendering(); if (rendering == null) { return(null); } Guid dataSourceGuid; if (!string.IsNullOrEmpty(rendering.DataSource)) { // Depending on if the datasource is a GUID vs Path, use the correct overload return(Guid.TryParse(rendering.DataSource, out dataSourceGuid) ? _context.GetItem <T>(dataSourceGuid, inferType: true) : _context.GetItem <T>(rendering.DataSource, inferType: true)); } // Try to get from the Rendering StaticItem (without getting the ContextItem) var propertyItemId = rendering[RenderingItemIdPropertyName]; T propertyItem = null; if (!string.IsNullOrEmpty(propertyItemId)) { propertyItem = Guid.TryParse(propertyItemId, out dataSourceGuid) ? _context.GetItem <T>(dataSourceGuid, inferType: true) : _context.GetItem <T>(propertyItemId, inferType: true); } // Vary the fall-back logic (Always and Never recreate the behavior of Default, but with/without their respective fallback logic) switch (options) { case DatasourceNestingOptions.Default: var staticItem = rendering.Item; if (staticItem != null) { return(_context.GetItem <T>(staticItem.ID.Guid, inferType: true)); } break; case DatasourceNestingOptions.Always: if (propertyItem != null) { return(propertyItem); } var nestedItem = RenderingContext.CurrentOrNull?.ContextItem; if (nestedItem != null) { return(_context.GetItem <T>(nestedItem.ID.Guid, inferType: true)); } break; case DatasourceNestingOptions.Never: if (propertyItem != null) { return(propertyItem); } break; } // Finally just fall back to the context item return(_context.GetCurrentItem <T>(inferType: true)); }
public string GetMetatags() { StringBuilder metaTags = new StringBuilder(); string ogTitle = String.Empty; string metaDescription = String.Empty; IPageMetadata metadata = _currentContext.GetCurrentItem <IPageMetadata>(); if (metadata != null) { if (metadata.NoIndex) { metaTags.AppendFormat(SiteConstants.Metadata.MetaName, SiteConstants.Metadata.Robots, SiteConstants.Metadata.NoIndex); } if (metadata.NoODP) { metaTags.AppendFormat(SiteConstants.Metadata.MetaName, SiteConstants.Metadata.Robots, SiteConstants.Metadata.NoODP); } if (metadata.NoYdir) { metaTags.AppendFormat(SiteConstants.Metadata.MetaName, SiteConstants.Metadata.Robots, SiteConstants.Metadata.NoYdir); } string metaTitle = String.Empty; if (!string.IsNullOrEmpty(metadata.BrowserTitle)) { metaTitle = metadata.BrowserTitle; ogTitle = metadata.BrowserTitle; } else { metaTitle = metadata.Title; ogTitle = metadata.Title; } metaDescription = metadata.MetaDescription; UrlString url = new UrlString(metadata.Url); url.HostName = _sitecoreSiteLogic.TargetHostName; metaTags.AppendFormat(SiteConstants.Metadata.MetaTitleTag, metaTitle); if (!string.IsNullOrEmpty(metadata.MetaKeywords)) { metaTags.AppendFormat(SiteConstants.Metadata.MetaName, SiteConstants.Metadata.Keywords, metadata.MetaKeywords); } if (!string.IsNullOrEmpty(metadata.MetaDescription)) { metaTags.AppendFormat(SiteConstants.Metadata.MetaName, SiteConstants.Metadata.Description, metadata.MetaDescription); } //Facebook Global if (_sitecoreSiteLogic.SocialSettings != null) { if (!string.IsNullOrEmpty(_sitecoreSiteLogic.SocialSettings.OGSiteName)) { metaTags.AppendFormat(SiteConstants.Metadata.MetaProperty, SiteConstants.Metadata.OGSiteName, _sitecoreSiteLogic.SocialSettings.OGSiteName); } if (!string.IsNullOrEmpty(_sitecoreSiteLogic.SocialSettings.FBAdmins)) { metaTags.AppendFormat(SiteConstants.Metadata.MetaProperty, SiteConstants.Metadata.FBAdmins, _sitecoreSiteLogic.SocialSettings.FBAdmins); } } if (metadata.OGImage != null) { metaTags.AppendFormat(SiteConstants.Metadata.MetaProperty, SiteConstants.Metadata.OGImage, metadata.OGImage.Src); } if (!string.IsNullOrEmpty(metadata.OGType)) { metaTags.AppendFormat(SiteConstants.Metadata.MetaProperty, SiteConstants.Metadata.OGType, metadata.OGType); } //Facebook if (!string.IsNullOrEmpty(metadata.OGTitle)) { metaTags.AppendFormat(SiteConstants.Metadata.MetaProperty, SiteConstants.Metadata.OGTitle, metadata.OGTitle); } else if (!String.IsNullOrEmpty(ogTitle)) { metaTags.AppendFormat(SiteConstants.Metadata.MetaProperty, SiteConstants.Metadata.OGTitle, ogTitle); } if (!string.IsNullOrEmpty(metadata.OGDescription)) { metaTags.AppendFormat(SiteConstants.Metadata.MetaProperty, SiteConstants.Metadata.OGDescription, metadata.OGDescription); } else if (!String.IsNullOrEmpty(metaDescription)) { metaTags.AppendFormat(SiteConstants.Metadata.MetaProperty, SiteConstants.Metadata.OGDescription, metaDescription); } metaTags.AppendFormat(SiteConstants.Metadata.MetaProperty, SiteConstants.Metadata.OGUrl, url.ToString()); //twitter if (_sitecoreSiteLogic.SocialSettings != null) { if (!string.IsNullOrEmpty(_sitecoreSiteLogic.SocialSettings.TwitterSite)) { metaTags.AppendFormat(SiteConstants.Metadata.MetaProperty, SiteConstants.Metadata.TwitterSite, _sitecoreSiteLogic.SocialSettings.TwitterSite); } } if (metadata.TwitterImage != null) { metaTags.AppendFormat(SiteConstants.Metadata.MetaProperty, SiteConstants.Metadata.TwitterImage, metadata.TwitterImage.Src); } if (!string.IsNullOrEmpty(metadata.TwitterCard)) { metaTags.AppendFormat(SiteConstants.Metadata.MetaProperty, SiteConstants.Metadata.TwitterCard, metadata.TwitterCard); } if (!string.IsNullOrEmpty(metadata.TwitterTitle)) { metaTags.AppendFormat(SiteConstants.Metadata.MetaProperty, SiteConstants.Metadata.TwitterTitle, metadata.TwitterTitle); } else if (!String.IsNullOrEmpty(ogTitle)) { metaTags.AppendFormat(SiteConstants.Metadata.MetaProperty, SiteConstants.Metadata.TwitterTitle, ogTitle); } if (!string.IsNullOrEmpty(metadata.TwitterDescription)) { metaTags.AppendFormat(SiteConstants.Metadata.MetaProperty, SiteConstants.Metadata.TwitterDescription, metadata.TwitterDescription); } else if (!String.IsNullOrEmpty(metaDescription)) { metaTags.AppendFormat(SiteConstants.Metadata.MetaProperty, SiteConstants.Metadata.TwitterDescription, metaDescription); } metaTags.AppendFormat(SiteConstants.Metadata.MetaProperty, SiteConstants.Metadata.TwitterUrl, url.ToString()); } return(metaTags.ToString()); }
public override BasePageModel GetModel() { _context = new SitecoreContext(); return(_context.GetCurrentItem <BasePageModel>()); }
public override PersonOverviewModel GetModel() { _context = new SitecoreContext(); return(_context.GetCurrentItem <PersonOverviewModel>()); }
public T GetCurrentItem <T>(bool isLazy = false, bool inferType = false) where T : class { return(_sitecoreContext.GetCurrentItem <T>(isLazy, inferType)); }
public T GetContextItem <T>() where T : class, IGlassBase { return(_sitecoreContext.GetCurrentItem <T>()); }
public override ICarrouselSlides GetModel() { _context = new SitecoreContext(); return(_context.GetCurrentItem <ICarrouselSlides>()); }