A message from our CEO about the future of Stack Overflow and Stack Exchange. Read now.
25

This should give you what you want: @using (Html.Glass().BeginRenderLink(Model, m => m.MyLinkField, null, true) { @Html.Glass().RenderImage(Model, m => m.MyImageField, null, true) }


14

SitecoreChildrenAttribute - Maps children of the current item. Expects an IEnumerable<T> SitecoreFieldAttribute - Maps from a field on the current item SitecoreFieldFieldValueAttribute - Used to populate default values of a field's field SitecoreIdAttribute - Maps the Sitecore item ID. Expects the property to be a Sitecore.Data.ID or Guid ...


12

Yes, use the glassCache attribute. A value of anything other than false means the cache will be enabled for that site: https://github.com/mikeedwards83/Glass.Mapper/blob/master/Source/Glass.Mapper.Sc/SitecoreService.cs#L116


12

You can use the [SitecoreParent] attribute for that: [SitecoreParent] public virtual BasePage Parent { get; set; } It is also mentioned in the Tutorial 17 (http://www.glass.lu/mapper/sc/tutorials/tutorial17)


11

Instantiate a SitecoreContext and call the Cast<T>() method. ISitecoreContext context = SitecoreContext.GetFromHttpContext(); var myTemplateObject = context.Cast<MySite.Model.MyTemplate>(item);


10

So it's took me all of yesterday and most of today to figure this out. In the end it was nothing to do with my code or configuration at all. The log files showing some errors were a Red-Herring and leading me in the wrong direction. It is a Sitecore bug with 8.1 Update-2 (public reference: 108023). This blog post was really useful for identifying it: http:...


10

The problem was with MVC setup in the project not with Glass. This was the solution - http://samirvaidya.blogspot.hu/2015/10/the-type-expression-is-defined-in.html. The error was exactly The type 'Expression<>' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Core, Version=4.0.0.0, Culture=neutral, ...


9

Is you want to use the database represented by Sitecore.Context.Database then you should use SitecoreContext. The service SitecoreContentContext is a wrapper for the database Sitecore.Context.ContentDatabase. If the internal database used by the Glass service is null, you should check the appropriate static property. If you want to wrap a specific ...


9

You can put it to the Foundation layer. Any project in any layer can have references to the Foundation layer. Also, a Foundation project can have references to another Foundation project, but you need to follow this rule: Unlike the Feature layer, there is no strict convention on dependencies between modules in the foundation layer. This means that one ...


9

The fact that BeginRenderLink does not render link text inside the <a> tag is not bug. As Glass Mapper documentation says, BeginRenderLink is not really for displaying the default link text. You should use it if you want to add something custom inside your link. The BeginRenderLink can be used to render a link that should contain multiple HTML ...


8

You can't mix configuration methods in Glass Mapper. Your IPage model is using attribute configuration but your IGlassBase model is using fluent configuration. Remove the SitecoreField attributes from your IPage properties or add SitecoreField attributes to all of your IGlassBase properties and remove your fluent configuration map.


8

You need to use latest version of glass mapper: https://www.nuget.org/packages/Glass.Mapper.Sc/4.2.1.188 with Sitecore 8.2 In the new version you have: namespace Glass.Mapper.Sc { public class Utilities : Glass.Mapper.Utilities { public static bool IsPageEditor { get { return Sitecore.Context....


8

The DropList field only stores the name of the item selected. To accomplish what you are trying to do, you need to use a Droplink field instead. The Droplink field will store the ID of the selected item. From there, you can use the ID to find the selected item in Sitecore and convert it to a strongly-typed class that has the ClassName property that you ...


8

This is Sitecore's default language mapping. It recognizes hu as Hungarian and se as Swedish. Happens in the StripLanguage processor in the <preprocessRequest> pipeline. If you never need language embedding in the URL, you can disable it. Alternatively, define languageEmbedding to be never as options on your LinkManager. Edited to add. There's also ...


8

If Cachable attribute equals true, it means that Glass Mapper will apply cache for this object. I've decompiled the Glass.Mapper.Sc.dll, where CacheKeyGenerator is defined and we can see that it doesn't consider the language in the key for cache: namespace Glass.Mapper.Sc.Caching { public class CacheKeyGenerator : ICacheKeyGenerator { public string ...


8

No, Glass Mapper can't map Template or TemplateItem out of the box. However, it's easy to create field mappers so that it can. Field Mappers To map Template, create the following field mapper: public class SitecoreFieldTemplateMapper : AbstractSitecoreFieldMapper { public SitecoreFieldTemplateMapper() : base(typeof(Template)) { } ...


7

The problem could be that you use AutoMapping. If the field names are not exactly the same as the property names then Glass can't solve these. I would try to use [SitecoreField("First Text Line")] attribute for FirstTextLine property, if your field name in Sitecore is First Text Line. Here is the documentation about AutoMap - http://www.glass.lu/Blog/...


7

You can update the GlassV3Item.tt file and make use of the Custom Data property in TDS to set whether a field should not be lazy loaded. Let's add some comments to keep this clean. Add this near the top of the file with the rest of the setting comments (Line 38 on clean tt file) /* dontloadlazy=true Sets the field setting to DontLoadLazily */ Add a ...


7

There are a couple good ways to do this Provide a different interface when the page is in edit mode. Image with retina/non-retina support using Glass: @if (Sitecore.Context.PageMode.IsExperienceEditorEditing) { <div class="container-fluid" style="width: 100%;"> <h3>Logo Images</h3> <div class="col-sm-6"> ...


6

Looks like SitecoreContext.GetFromHttpContext() is obsolete. The recommend way is SitecoreContextFactory.Default.GetSitecoreContext() My preferred way is new SitecoreContext() So casting can be done like: var model = SitecoreContextFactory.Default.GetSitecoreContext().Cast<T>(item); var model = new SitecoreContext().Cast<T>(item);


6

The Sitecore.CodeGenerator project can convert serialized Sitecore items into GlassMapper classes, and it contains support for the Rainbow serialization format (used by Unicorn 3). The project page has step-by-step instructions on how to install and configure it.


6

I was able to figure this one out, with the help of Tamás Tárnok above. The solution was to add [SitecoreType(TemplateId = "Your_Sitecore_Template_Id")] to the implemented class. More information is below. https://stackoverflow.com/questions/18974495/glass-mapper-and-creating-sitecore-items-using-interfaces


6

I added @using Glass.Mapper.Sc.Web.Mvc on Top and it solved my problem


6

I wanted to add this as the other answers are close, but not 100% IMO. First - SitecoreContext and SitecoreService have overloaded constructors. So you have to use a factory registration to be able to resolve those. Otherwise you will be dependent the constructor that the container will select, if it is able to select one. This is how I register the ...


6

No, there is no option to get all the descendants with a simple Glass Mapper attribute. You could use SitecoreQuery attribute in theory: [SitecoreQuery(".//*")] I'm not saying that getting all descendants is always wrong, but you really need to be careful when you want to use descendants. I guess that's the reason why it's not there as one of the Glass ...


6

You need to create a model first for your media items, e.g.: [SitecoreClass] public class MediaFile { [SitecoreField("Attachment")] public virtual Stream File {get; set; } [SitecoreField("MimeType")] public virtual string MimeType { get; set; } [SitecoreField("Title")] public virtual string Title { get; set; } [SitecoreField("...


6

In version 5 config.EnableLazyLoadingForCachableModels setting was removed and "IsLazy"-setting was removed from all model property configuration, like [SitecoreChildren(IsLazy = false)] IEnumerable<ISitecoreItem> Children{get;set;} You should just remove all these settings from your models, because of in V5 lazy loading was improved (it is enabled ...


6

This will work for the basic params; @using Sitecore.Resources.Media <div style="background-image:url(@HashingUtils.ProtectAssetUrl(string.Format("{0}?{1}", Model.Logo.Src, new MediaUrlOptions { MaxWidth = 200 })))">Hello</div>


5

So after discussing with Nathanael Mann, it appears that this is indeed a defect in the Mapping code in Glass and not an issue with the implementation. I'll be adding a defect on the Github page and closing this question with this answer.


5

No, there's not a setting to disable lazy loading globally. However, it is possible to disable lazy loading on all of your models' properties with some custom code. Eager-Loading Attribute Configuration Loader This is a custom attribute configuration loader that will iterate over all the attribute configurations in your assemblies and disable lazy loading ...


Only top voted, non community-wiki answers of a minimum length are eligible