Exception of Glassmapper after upgrading to Sitecore 8.2 - sitecore

I am getting the below exception after we upgraded to Sitecore 8.2 and glass mapper version 4.3.4.197.
Exception : Message = "Failed to find configuration for parent item
type Sitecore.Data.Items.Item"
My code below :
Saving item into master database
Parent Item type = Sitecore.Data.Items
var newItem = sitecoreContext.Create(parentItem , fixed);

I was having the same issue and found that if you cast your parent item to a specific template it seems to work. For example, instead of
Item parent = sitecoreService.GetItem<Item>(GUID);
ChildTemplateType childItem = sitecoreService.Create(parent, childData);
use:
Item parent = sitecoreService.GetItem<Item>(GUID);
ParentTemplateType parentCasted = sitecoreService.Cast<ParentTemplateType>(parent);
ChildTemplateType childItem = sitecoreService.Create(parentCasted, childData);

Related

How to remove newly created item version if there is no change with previous version in sitecore programmatically

I am creating version of item on lock & edit button. I want to remove that version if author does not change any value of that version while check in version. I would like to compare versions and delete newly created version if there is no change while check in item version.
Note: No workflow is needed
On ItemSaving event you can get the list of changes in the item.
Here is some sample code to get the idea:
protected void OnItemSaving(object sender, EventArgs args)
{
var newItem = Event.ExtractParameter(args, 0) as Item;
Item originalItem = newItem.Database.GetItem(newItem.ID, newItem.Language, newItem.Version);
var differences = FindDifferences(newItem, originalItem);
}
private List<string> FindDifferences(Item newItem, Item originalItem)
{
newItem.Fields.ReadAll();
IEnumerable<string> fieldNames = newItem.Fields.Select(f => f.Name);
return fieldNames
.Where(fieldName => newItem[fieldName] != originalItem[fieldName])
.ToList();
}
Open item in Content Editor
Click on Versions tab
Click on Compare button on Versions chunk
As result you will see dialog with ability to compare 2 versions. If you don't detect any changes then you can remove newly created version.
P.S. Screenshot is from 7.2 version, but 8+ version have pretty the same way to compare versions.

Sitecore's treelist field not getting updated immediately

I am updating sitecore treelist field type through code in background (when RTE is open for the same item) and it is not reflecting with the new values immediately. I had checked both in database and CMS.
After sometime it reflects in the field and the database as well.
Here are few things I checked:
Cleared sitecore and browser cache.
Added Cachemanager. Enabled=false and Cachemanager.Enabled=true when updating the item.
Checked the eventqueue table if the item save event is called.
Here is the code:
var article = master.GetItem(new ID(data.CurrentItemId));
if (article != null)
{
var referencedValues = article.Fields["Referenced articles"];
using (new SecurityDisabler())
{
//CacheManager.Enabled = false;
article.Editing.BeginEdit();
if (referencedValues != null)
referencedValues.Value = "{5A3D67DF-3917-4AC2-BDAF-69CDA1204C2A}";
article.Editing.EndEdit(true);
//CacheManager.Enabled = true;
}
}

get the recently added item from sitecore

If I create an item in sitecore from code behind using following code as shown in example under the heading Creating items:
//Now we can add the new item as a child to the parent
parentItem.Add("NewItemName", template);
And then in order to get the this newly added item from sitecore database what I should do? Because I don't know the ID.
The new item will be returned by the add method.
Eg.
Item newItem = parentItem.Add("NewItemName", template);

Sitecore 7 automatically opens an item created from custom field on save

I've created a custom field for sitecore Item, this custom field just creates a new Item with name specified under some specific folder. I do it on save (appropriate condition in onLoad method for custom field control):
if (!Sitecore.Context.ClientPage.IsEvent)
{
//blah-blah-blah
}
else
{
TemplateItem template = db.GetTemplate(TemplatePath);
Item parentItem = db.Items[ItemsRootPath];
var newItem = parentItem.Add("item name", template);
}
Item is creating successfully, but content editor automatically opens this item when I save a parent item (item that contains this custom field).
The question is: how I can avoid it and create this item in background, without displaying it to user?
Thanks,
A
You can disable Notifications which will prevent the new item being loaded like so:
Client.Site.Notifications.Disabled = true;
TemplateItem template = db.GetTemplate(TemplatePath);
Item parentItem = db.Items[ItemsRootPath];
var newItem = parentItem.Add("item name", template);
Client.Site.Notifications.Disabled = false;
I tested this by adding a child to the item that I was saving and resulted in the tree not being updated as well. I fixed that by adding this line after the notification enable.
Sitecore.Context.ClientPage.SendMessage(this, string.Format("item:refreshchildren(id={0})", ItemID));

How to flag new items as unpublished items?

In sitecore, if I add a new item to the master database(Unpublished), it does not show any indication regarding the published state.
For an example, if a user has added 10 items, he might get confused to figureout the items added by him which are pending for publishing.
Is there a way to identify newly added items as unpublished or in new and display a validation in the "Quick action bar"?
Never thought about this, but it's actually pretty easy to fix.
I created a GutterRenderer that indicates wether an item has been published to at least one, to all, or to none of the publishing targets.
EDIT: Added Click behaviour. When you click the gutter icon, the Publish dialog will be shown for that item.
First I will show you the code that I wrote for this and then I'll show you screenshots of the setup and the result.
Here is the code:
using System.Collections.Generic;
using System.Linq;
using Sitecore;
using Sitecore.Data;
using Sitecore.Data.Items;
using Sitecore.Globalization;
using Sitecore.Shell.Applications.ContentEditor.Gutters;
namespace ParTech.Library.Gutters
{
public class PublicationStatus : GutterRenderer
{
private readonly ID publishingTargetsFolderId = new ID("{D9E44555-02A6-407A-B4FC-96B9026CAADD}");
private readonly ID targetDatabaseFieldId = new ID("{39ECFD90-55D2-49D8-B513-99D15573DE41}");
protected override GutterIconDescriptor GetIconDescriptor(Item item)
{
bool existsInAll = true;
bool existsInOne = false;
// Find the publishing targets item folder
Item publishingTargetsFolder = Context.ContentDatabase.GetItem(publishingTargetsFolderId);
if (publishingTargetsFolder == null)
{
return null;
}
// Retrieve the publishing targets database names
List<string> publishingTargetsDatabases = publishingTargetsFolder.GetChildren()
.Select(x => x[targetDatabaseFieldId])
.ToList();
// Check for item existance in publishing targets
publishingTargetsDatabases.ForEach(delegate(string databaseName)
{
if (Database.GetDatabase(databaseName).GetItem(item.ID) != null)
{
existsInOne = true;
}
else
{
existsInAll = false;
}
});
// Return descriptor with tooltip and icon
string tooltip = Translate.Text("This item has not yet been published");
string icon = "People/16x16/flag_red.png";
if (existsInAll)
{
tooltip = Translate.Text("This item has been published to all targets");
icon = "People/16x16/flag_green.png";
}
else if (existsInOne)
{
tooltip = Translate.Text("This item has been published to at least one target");
icon = "People/16x16/flag_yellow.png";
}
return new GutterIconDescriptor()
{
Icon = icon,
Tooltip = tooltip,
Click = string.Format("item:publish(id={0})", item.ID)
};
}
}
}
And this is how so set it up and how it will look once it's running:
Figure 1: Create a new Gutter item in the Core database:
Figure 2: Switch back to your Master database and activate the Gutter by right-clicking in the gutter area.
Figure 3: The Gutter now indicates the publication status of your items
From the top of my head it's not available out of the box. In the core database however, there's the definitions of the gutter, etc. You could create your own.
There's the 'published' field on items though, but I'm not sure if that takes different versions into account.
Maybe you can check the differences between the item in the master and web (i.e. Item doesn't exist or is different version in web, then it's waiting to be published).
Alternatively, have a read through this: http://webcmd.wordpress.com/2011/08/31/sitecore-ribbon-that-displays-published-state-of-an-item/
It'll explain how to check if an item is published as a ribbon.