I am storing cropping information on my Sitecore Media Library images in a field that was added to the /sitecore/templates/System/Media/Unversioned/Image template.
I would like to access this field along with all of the other properties that exist in the Glass.Mapper.Sc.Fields.Image complex field type so that I can continue to use GlassHtml.RenderImage() in my views.
My initial attempts to inherit from the class were unsuccessful - it appears to break the mapping behavior - so I am wondering if there is another way to extend this class with additional properties?
Here's what I've tried:
[SitecoreType(AutoMap = true)]
public class MyImage : Glass.Mapper.Sc.Fields.Image
{
public virtual string CropInfo { get; set; }
}
You will need to implement a custom data handler to map the additional field.
I would create a data handler that inherits from the standard Image data handler:
https://github.com/mikeedwards83/Glass.Mapper/blob/master/Source/Glass.Mapper.Sc/DataMappers/SitecoreFieldImageMapper.cs
Then customise GetField and SetField.
Once you have created the custom data handler you need to register it with the Windsor container. See tutorial 19 for how to do this:
http://glass.lu/docs/tutorial/sitecore/tutorial19/tutorial19.html
The important part:
public static void CastleConfig(IWindsorContainer container){
var config = new Config();
container.Register(
Component.For < AbstractDataMapper>().ImplementedBy<TweetsDataHandler>().LifeStyle.Transient
);
container.Install(new SitecoreInstaller(config));
}
Related
Is it possible to have more than one custom user profile and if it is how to set up web config file and how to manage custom profiles for two website under the same sitecore instance (same VS solution)?
We had one custom user profile and new requirement came about new website under the same sitecore instance but with the new custom user for the second website.
During development of second website we created second custom user profile and everything went fine, we change "inherits" attribute of system.web/profile node in the web.config file to point to second custom use profile and during development it was OK.
The problem now is that only one user profile can log in to the webistes:
if we set inherits attribute to "Namespace.Website.NamespaceA.CustomProfileA, Namespace.Website" only profileA will be able to log in to their domain and if we set it to "Namespace.Website.NamespaceB.CustomProfileB, Namespace.Website" only profileB will be able to login to its domain because the switcher will use this one.
All articles in the web describe how to set custom user profile, switcher and switchingProviders for just one custom user profile but there are no examples for my case.
Thanks,
Srdjan
Unfortunately, there does not seem to be a clean way to have multiple user profile classes created for you by the API. Typically, you will get the user profile via Sitecore.Context.User.Profile. The Context class is static and the methods that initialize the Profile property are private, so there's nowhere to insert your extra logic.
You could, however, create wrapper classes for the Profile. Start with a base class like this:
public abstract class CustomProfileBase
{
public CustomProfileBase(Sitecore.Security.UserProfile innerProfile)
{
Assert.ArgumentNotNull(innerProfile, nameof(innerProfile));
InnerProfile = innerProfile;
}
public Sitecore.Security.UserProfile InnerProfile { get; protected set; }
public virtual string GetCustomProperty(string propertyName)
{
return InnerProfile.GetCustomProperty(propertyName);
}
public virtual void SetCustomProperty(string propertyName, string value)
{
InnerProfile.SetCustomProperty(propertyName, value);
}
public virtual void Save()
{
InnerProfile.Save();
}
public virtual string Email
{
get { return InnerProfile.Email; }
set { InnerProfile.Email = value; }
}
// Other members omitted for brevity
}
This CustomProfileBase class would have a member that wraps each of the public members of Sitecore.Security.UserProfile. Then, you would create your site specific profile like this:
public class SiteOneProfile : CustomProfileBase
{
public SiteOneProfile(UserProfile innerProfile) : base(innerProfile)
{
}
public string CustomPropertyOne
{
get { return GetCustomProperty("CustomPropertyOne"); }
set { SetCustomProperty("CustomPropertyOne", value); }
}
}
Then you would use it from a controller or elsewhere like so:
var profile = new SiteOneProfile(Sitecore.Context.User.Profile);
model.property = profile.CustomPropertyOne;
Update
When using this approach, you would just leave the inherits attribute in the config with its default value. Also, the profile should not have an effect on the ability to login. If you are still having issues with that, please update your question with details of the error you get when logging in.
I have a bunch of items in View that aren't fields on the model and just items. I really want to user RenderImage so I don't have to re-invent all the html code, but it really wants an item or and item of the GlassView type.
Is there a simple way to just force feed an item into GlassView.RenderImage?
How about sitecore's Render Image? It just wants a field value, but I want to give it an item?
You should add a new model which extends the existing (Sitecore template based) one.
For example you have the IArticle model, which has every field of the item, but not much else, as usual. You should create a new model, which inherits from the original, and you can add new fields, which will be mapped by Glass, if set properly. You can use the following attributes for example:
[SitecoreNode] (define an item by id or path)
[SitecoreParent] (by hierarchy)
[SitecoreQuery] (sitecore query)
[SitecoreChildren] (hierarchy)
Models
/// This model is based on the Sitecore template
[SitecoreType(TemplateId = "something")]
public interface IArticle : IBaseItem {
[SitecoreField]
string Title { get; set; }
[SitecoreField]
string Content { get; set; }
}
/// This model defines additional items.
public interface IArticleDetail : IArticle {
[SitecoreNode(Id = Constants.MainBannerId)]
IBanner PromoBanner { get; set; }
[SitecoreQuery("somequery")]
IEnumerable<ITag> Tags { get; set; }
}
In this case you GlassView inherits from the IArticleDetail, and the model binder propagates the additional fields as well.
If you want to render (editable images), you can just use the following syntax:
#RenderImage(Model.PromoBanner, m => BannerImage, isEditable: true)
or
#Html.Glass().RenderImage(Model.PromoBanner, m => BannerImage, isEditable: true)
#RenderImage will only accept a type of Glass.Mapper.Sc.Fields.Image but if you have a property on your view of Item, I would suggest just using the standard HTML helper field render:
#Html.Sitecore().Field("Field Name", Model.SomeSubItem)
We are developing a multisite sitecore solution where each sites can have have their own News as well as able to display the combined news from other Sites.
Problem:
Each site have their unique News requirements where 90% of the template fields matches but rest 10% are different.
For example, Site-A has news template with Authors drop down list where Author List are authored on Configuration Node. Where as Site-B has news template where Author is a FREE TEXT Field.
Therefore, when Glass Mapper automatically tries to Map Authors field it fails for Free Text one.
Solution:
This can be resolved either by creating a Author as drop down on all Sites but Product owners don't want this.
The other solution is manual mapping of news fields from both sources or use AUTOMAP etc.
Desired Solution:
Glassmapper automatically resolves and populates the Author Text Field or Drop Down Field on the fly.
Is above possible?
Thank you.
I would solve this by "fluent configuration", http://glass.lu/Mapper/Sc/Tutorials/Tutorial8.aspx.
Combined with the new Delegate feature added to the Glass Mapper recently.
The Delegate feature was originally introduced and described here: http://cardinalcore.co.uk/2014/07/02/controlling-glass-fields-from-your-own-code/
Nuget package for the Delegate feature: https://www.nuget.org/packages/Cardinal.Glass.Extensions.Mapping/
You can use Infer types as follows:
public interface IBaseNews
{
string Author {get; set;}
//List all other shared fields below
}
[SitecoreType(TemplateId="....", AutoMap = true)]
public class NewsSiteA : IBaseNews
{
[SitecoreField]
public string Author {get; set;}
//List all fields which are unique for SiteA
}
[SitecoreType(TemplateId="....", AutoMap = true)]
public class NewsSiteB : IBaseNews
{
[SitecoreField]
public string Author {get; set;}
//List all fields which are unique for SiteB
}
Now, Your code should be:
IBaseNews newsClass = NewsItem.GlassCast<IBaseNews>(true,true);
//You can use Author property now
Firstly, I would recommend updating to the latest version of Glass for many other reasons including the delegate feature.
From the infer type example in the comment - you shouldn't use GlassCast, use CreateType(Item item) from the sitecore service / context. If you adopt the version with Delegate in, there is now an official Cast(Item item) on the sitecore service instead.
Also the example there uses a would not solve the difference in type. Delegate would make this very easy. Remember with delegate that there is no lazy loading, this shouldn't matter in this case.
public interface INews
{
// All my other fields
string Author { get; set; }
}
The fluent configuration would be something like (to be done in GlassScCustom)
SitecoreType<INews> = new SitecoreType<INews>();
sitecoreType.Delegate(y => y.Author).GetValue(GetAuthor);
fluentConfig.Add(sitecoreType);
private string GetAuthor(SitecoreDataMappingContext arg)
{
Item item = arg.Item;
if(item.TemplateID == <templateid>)
{
// return the value from the drop link
}
return item["Authors"];
}
I've been trying to achieve the following with glass mapper but can't get it to work.
I have a Home Page template which doesn't have any fields itself but inherits the following two templates:
Navigation Template
Fields: Navigation Title
Meta Information Template
Fields: Page Title, Meta Description
I've created the corresponding interfaces / classes as follows:
[SitecoreType(TemplateId = "{5BAB563C-12AD-4398-8C4A-BF623F7DBCDC}", AutoMap = true)]
public interface INavigation
{
[SitecoreField(FieldName = "Navigation Title")]
string NavigationTitle { get; set; }
}
[SitecoreType(TemplateId = "{95539498-31A5-4CB5-8DD6-C422D505C482}", AutoMap = true)]
public interface IMetaInformation
{
[SitecoreField]
string PageTitle { get; set; }
[SitecoreField]
string MetaDescription { get; set; }
}
[SitecoreType(TemplateId = "{F08693E5-8660-4B13-BBD6-7B9DC6091750}", AutoMap = true)]
public class HomePage : INavigation, IMetaInformation
{
public virtual string NavigationTitle { get; set; }
public virtual string PageTitle { get; set; }
public virtual string MetaDescription { get; set; }
}
When I then try accessing my page all attributes are always null:
var context = new SitecoreContext();
var page = context.GetCurrentItem<HomePage>();
I've tried several different approaches to this but nothing works. Also what was described in different tutorials didn't work. The only thing that works is when I add the fields directly on the Home Page template, but I don't want that since I have more than one page type and I therefore want to inherit the fields.
Does anyone have any idea what I'm missing here?! I'm using Sitecore 7 with .NET 4.5 by the way if that makes a difference.
Your fields are not mapped because you use a space in the Fieldname in the Sitecore Template.
Either remove the space or add the attribute [SitecoreField(FieldName ="Page Title")] to the Model.
I think that the Homepage class is trying to map the NavigationTitle on the Homepage template with the fieldName NavigationTitle and ignores the FieldName attribute on the base model.
By the way: I am using only interfaces for the current project I'm working on and it works as expected with inheritance. No need to add a property more then once ;)
Try to set infer type to true. I cannot get it to work at all without having that set.
ex.
item.GlassCast<HomePage>(false, true);
or
context.GetCurrentItem<HomePage>(false, true);
I find it does not work without this set.
You should render the common fields in a separate sublayout as a GlassUserControl.
public partial class NavigationTemplate : GlassUserControl<NavigationTemplate>
{
protected void Page_Load(object sender, EventArgs e)
Here you will have direct access to the NavigationTemplate fields no matter what item you are loading, it will always be cast to NavigationTemplate and will read the field values of the item you are loading.
It seems that you expect to get the properties on the HomePage instance, but you need to ask for the exact interface that contains the property as seen here
I.e.
Instead of doing:
var page = context.GetCurrentItem<HomePage>();
You should explicitly get current item as INavigation and get the field from the interface:
var navigationTitle = context.GetCurrentItem<INavigation>().NavigationTitle;
Explanation:
Let's say I have an object graph that's nested several levels deep and each entity has a bi-directional relationship with each other.
A -> B -> C -> D -> E
Or in other words, A has a collection of B and B has a reference back to A, and B has a collection of C and C has a reference back to B, etc...
Now let's say I want to edit some data for an instance ofC. In Winforms, I would use something like this:
var instanceOfC;
using (var session = SessionFactory.OpenSession())
{
// get the instance of C with Id = 3
instanceOfC = session.Linq<C>().Where(x => x.Id == 3);
}
SendToUIAndLetUserUpdateData(instanceOfC);
using (var session = SessionFactory.OpenSession())
{
// re-attach the detached entity and update it
session.Update(instanceOfC);
}
In plain English, we grab a persistent instance out of the database, detach it, give it to the UI layer for editing, then re-attach it and save it back to the database.
Problem:
This works fine for Winform applications because we're using the same entity all throughout, the only difference being that it goes from persistent to detached to persistent again.
The problem is that now I'm using a web service and a browser, sending over JSON data. The entity gets serialized into a string, and de-serialized into a new entity. It's no longer a detached entity, but rather a transient one that just happens to have the same ID as the persistent one (and updated fields). If I use this entity to update, it will wipe out the relationship to B and D because they don't exist in this new transient entity.
Question:
My question is, how do I serialize detached entities over the web to a client, receive them back, and save them, while preserving any relationships that I didn't explicitly change? I know about ISession.SaveOrUpdateCopy and ISession.Merge() (they seem to do the same thing?), but this will still wipe out the relationships if I don't explicitly set them. I could copy the fields from the transient entity to the persistent entity one by one, but this doesn't work too well when it comes to relationships and I'd have to handle version comparisons manually.
I solved this problem by using an intermediate class to hold data coming in from the web service, then copying its properties to the database entity. For example, let's say I have two entities like so:
Entity Classes
public class Album
{
public virtual int Id { get; set; }
public virtual ICollection Photos { get; set; }
}
public class Photo
{
public virtual int Id { get; set; }
public virtual Album Album { get; set; }
public virtual string Name { get; set; }
public virtual string PathToFile { get; set; }
}
Album contains a collection of Photo objects, and Photo has a reference back to the Album it's in, so it's a bidirectional relationship. I then create a PhotoDTO class:
DTO Class
public class PhotoDTO
{
public virtual int Id { get; set; }
public virtual int AlbumId { get; set; }
public virtual string Name { get; set; }
// note that the DTO does not have a PathToFile property
}
Now let's say I have the following Photo stored in the database:
Server Data
new Photo
{
Id = 15,
Name = "Fluffy Kittens",
Album = Session.Load<Album>(3)
};
The client now wants to update the photo's name. They send over the following JSON to the server:
Client Data
PUT http://server/photos/15
{
"id": 15,
"albumid": 3,
"name": "Angry Kittens"
}
The server then deserializes the JSON into a PhotoDTO object. On the server side, we update the Photo like this:
Server Code
var photoDTO = DeserializeJson();
var photoDB = Session.Load(photoDTO.Id); // or use the ID in the URL
// copy the properties from photoDTO to photoDB
photoDB.Name = photoDTO.Name;
photoDB.Album = Session.Load<Album>(photoDTO.AlbumId);
Session.Flush(); // save the changes to the DB
Explanation
This was the best solution I've found because:
You can choose which properties the client is allowed to modify. For example, PhotoDTO doesn't have a PathToFile property, so the client can never modify it.
You can also choose whether to update a property or not. For example, if the client didn't send over an AlbumId, it will be 0. You can check for that and not change the Album if the ID is 0. Likewise, if the user doesn't send over a Name, you can choose not to update that property.
You don't have to worry about the lifecycle of an entity because it will always be retrieved and updated within the scope of a single session.
AutoMapper
I recommend using AutoMapper to automatically copy the properties from the DTO to the entity, especially if your entites have a lot of properties. It saves you the trouble of having to write every property by hand, and has a lot of configurability.