2
votes

I have a Sitecore 8.1 CD instance. I also have some code that needs to create a content item in the Master database. (I am aware that is a no-no but I just need to figure this out at the moment) When my code attempts to use Glass Mapper to create a content item I get an error. Here is the code snippet and the error message. I am just trying to understand what the error means. I have a sense that this is simply a configuration problem. This code works fine on our Sitecore CM server. So I am hoping that by simply adjusting the config on our CD server I can get this to work. So far I have re-enabled the Master entry in ConnectionStrings.config and in Sitecore.config. But that hasn't fixed this.

SitecoreService service = new SitecoreService("master");
SimpleAes aes = new SimpleAes();

using (new SecurityDisabler())
{
    Item parentItem =  Factory.GetDatabase("master").GetItem("/sitecore/content/Non Page Content/Account Information/Shipping Addresses");
    newAddress = service.Create(parentItem, newAddress);     //THIS IS WHERE THE CODE FAILS
    user.Addresses.Add(newAddress);
    Utility.PublishItem(service.ResolveItem(newAddress));
    id = aes.EncryptToString(newAddress.Id.ToString());
    user.Addresses = user.Addresses;
    user.Save();
}

Error Message:

Glass.Mapper.MapperException: Failed to find configuration for parent item type Sitecore.Data.Items.Item ---> System.NullReferenceException: Object reference not set to an instance of an object. at System.Object.GetType() at Glass.Mapper.Context.GetTypeConfiguration[T](Object obj, Boolean doNotLoad, Boolean checkBase) at Glass.Mapper.Sc.SitecoreService.Create[T,TK](TK parent, T newItem, Boolean updateStatistics, Boolean silent) --- End of inner exception stack trace --- at Glass.Mapper.Sc.SitecoreService.Create[T,TK](TK parent, T newItem, Boolean updateStatistics, Boolean silent)

2

2 Answers

0
votes

It is failing on this line

Item parentItem =  Factory.GetDatabase("master").GetItem("/sitecore/content/Non Page Content/Account Information/Shipping Addresses");

if you put a check around it saying

if (parentItem != null) { // your code }

Then the code will work through and you will not get exception, but nothing will happen as well if parentItem is null.

Quick fix solution will be to give a 'master' DB connection string on your CD server (which is a no-no as you said). Better solution will be to expose master database through Sitecore Item API or your custom API, securing it through authentication and then calling this code from CD server via API.

0
votes

I am not sure if you still are looking how to solve this issue, but when I faced today with it I found your question.

Problem is that your parentItem has type Item. It causes issue inside in Glass. You can use any type as parent, but limitation is that it should not be inherited from Sitecore Item class. Try this:

var parentItem =  Factory.GetDatabase("master").GetItem("/sitecore/content/Non Page Content/Account Information/Shipping Addresses").GlassCast<BaseSitecoreItem>();
newAddress = service.Create(parentItem, newAddress); 

where BaseSitecoreItem is some your Glass Model.

It helped me and hope will help you.