public override void Process(GetModelArgs args) { if (args.Result == null) { try { Type type = GetFromField(args.Rendering, args); if (Context.StaticContext.Classes.ContainsKey(type)) { ISitecoreContext context = new SitecoreContext(); var result = context.GetCurrentItem(type); args.Result = result; } } catch (MapperException ex) { //do nothing } catch (System.TypeLoadException ex) { //do nothing } } }
protected void Page_Load(object sender, EventArgs e) { ISitecoreContext context = new SitecoreContext(); var home = context.GetCurrentItem<Home>(); Title.Text = home.Title; Text.Text = home.Text; CreateBy.Text = home.CreatedBy; CreatedOn.Text = home.Created.ToShortDateString(); Path.Text = home.Path; }
public void DropRead() { ISitecoreContext context = new SitecoreContext(); DemoItem current = context.GetCurrentItem<DemoItem>(); if (current.Drop != null) { string otherName = current.Drop.Name; } }
public void DropSave() { ISitecoreContext context = new SitecoreContext(); DemoItem current = context.GetCurrentItem<DemoItem>(); OtherItem other = context.GetItem<OtherItem>("/sitecore/content/home/someOtherItem"); current.Drop = other; context.Save(current); }
protected void Page_Load(object sender, EventArgs e) { ISitecoreContext context = new SitecoreContext(); Home item = context.GetCurrentItem<Home>(); title.Text = item.Title; body.Text = item.Body; date.Text = item.Date.ToString("dd MMM yyyy"); }
public void DoWork() { var context = new SitecoreContext(); using (new VersionCountDisabler()) { var current = context.GetCurrentItem<MyModel>(); //you will need to evaluate the query before you exit the VersionCountDisabler scope var results = current.Query.ToList(); } }
/// <summary> /// Handles the Load event of the Page control. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param> protected void Page_Load(object sender, EventArgs e) { ISitecoreContext context = new SitecoreContext(); BasePageModel = context.GetCurrentItem<BasePageModel>(); HomeModel = context.GetHomeItem<HomeModel>(); LoadMetaData(); RegisterGoogleAnalyticsScript(); if (BasePageModel.Id == HomeModel.Id) { body.Attributes.Add("class", "homepage"); } }
public void MultiTreeRead() { ISitecoreContext context = new SitecoreContext(); DemoItem current = context.GetCurrentItem<DemoItem>(); foreach (OtherItem other in current.Multi) { string title = other.Title; } foreach (OtherItem other in current.Tree) { string title = other.Title; } }
public void MultiTreeRead() { ISitecoreContext context = new SitecoreContext(); DemoItem current = context.GetCurrentItem<DemoItem>(); OtherItem forRemoval = current.Multi.First(); current.Multi.Remove(forRemoval); OtherItem toAdd = context.GetItem<OtherItem>("/sitecore/content/home/someOtherItem"); current.Multi.Add(toAdd); context.Save(current); }
public void GetCurrentItem_NoParameters() { //Assign var db = Sitecore.Configuration.Factory.GetDatabase("master"); var context = Context.Create(Utilities.CreateStandardResolver()); context.Load(new SitecoreAttributeConfigurationLoader("Glass.Mapper.Sc.Integration")); var path = "/sitecore/content/Tests/SitecoreContext/GetCurrentItem/Target"; var scContext = new SitecoreContext(); Sitecore.Context.Item = db.GetItem(path); //Act var result = scContext.GetCurrentItem<StubClass>(); //Assert Assert.AreEqual(Sitecore.Context.Item.ID, result.Id); }
/// <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 = new SitecoreContext(Context); var model = _context.GetCurrentItem(type); TypeDescriptor.GetProperties(finalControlType).Find("Model", false).SetValue(finalControl, model); this.Controls.Add(finalControl); base.CreateChildControls(); }
public void GetCurrentItem_NoParameters() { //Assign using (Db database = new Db { new Sitecore.FakeDb.DbItem("Target") }) { var context = Context.Create(Utilities.CreateStandardResolver()); context.Load(new OnDemandLoader<SitecoreTypeConfiguration>(typeof(StubClass))); var path = "/sitecore/content/Target"; var scContext = new SitecoreContext(); Sitecore.Context.Item = database.GetItem(path); //Act var result = scContext.GetCurrentItem<StubClass>(); //Assert Assert.AreEqual(Sitecore.Context.Item.ID, result.Id); } }
// GET: Home public ActionResult HelloWorld() { IHomeModel context = SitecoreContext.GetCurrentItem <IHomeModel>(); return(View(context)); }
/// <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 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) return null; var context = Context.Contexts.ContainsKey(ContextName) ? Context.Contexts[ContextName] : null; if (context == null) throw new MapperException("Failed to find context {0}".Formatted(ContextName)); //this is really aggressive if (!context.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); context.Load(loader); } ISitecoreContext scContext = new SitecoreContext(context); if (renderingItem.DataSource.IsNotNullOrEmpty()) { var item = scContext.Database.GetItem(renderingItem.DataSource); return scContext.CreateType(type, item, false, false, null); } return scContext.GetCurrentItem(type); }
public void GetCurrentItem_FourParameters() { //Assign using (Db database = new Db { new Sitecore.FakeDb.DbItem("Target") }) { var context = Context.Create(Utilities.CreateStandardResolver()); context.Load(new OnDemandLoader<SitecoreTypeConfiguration>(typeof(StubClass))); string param1 = "1para"; string param2 = "2para"; string param3 = "3para"; string param4 = "4para"; var path = "/sitecore/content/Target"; var scContext = new SitecoreContext(); Sitecore.Context.Item = database.GetItem(path); //Act var result = scContext.GetCurrentItem<StubClass, string, string, string, string>(param1, param2, param3, param4); //Assert Assert.AreEqual(Sitecore.Context.Item.ID, result.Id); Assert.AreEqual(param1, result.Param1); Assert.AreEqual(param2, result.Param2); Assert.AreEqual(param3, result.Param3); Assert.AreEqual(param4, result.Param4); } }
protected void Page_Load(object sender, EventArgs e) { var context = new SitecoreContext(); Model = context.GetCurrentItem <HomePage>(); }
public object GetObject(string model) { if (model.IsNullOrEmpty()) return null; var type = Type.GetType(model, true); if (type == null) return null; var context = Context.Contexts[ContextName]; if (context == null) throw new MapperException("Failed to find context {0}".Formatted(ContextName)); if (context.TypeConfigurations.ContainsKey(type)) { ISitecoreContext scContext = new SitecoreContext(context); var result = scContext.GetCurrentItem(type); return result; } return null; }
/// <summary> /// Returns the Context Item as strongly typed /// </summary> /// <typeparam name="T"></typeparam> /// <returns></returns> public T GetContextItem <T>(bool isLazy = false, bool inferType = false) where T : class { return(SitecoreContext.GetCurrentItem <T>(isLazy, inferType)); }