Track Sitecore item history - sitecore

Sitecore tracks the item changes with last updated by, Created by information.
Is it possible to track changes made to "fields" in an item against the person who changed them? And retrive the list of changes made to fields of an item.

You can create a custom handler and add it to item:saving event in Sitecore events/event configuration:
<sitecore>
<events>
<event name="item:saving">
<handler type="My.Assembly.Namespace.CreateHistoryEntryHandler, My.Assembly" method="OnItemSaving" />
</event>
</events>
</sitecore>
The class below saves the information to the Workflow History Store so you can see it using History menu from ribbon (see screenshot), but you can save it to any other place
namespace My.Assembly.Namespace
{
public class CreateHistoryEntryHandler
{
protected void OnItemSaving(object sender, EventArgs args)
{
Item newItem = Event.ExtractParameter(args, 0) as Item;
if (newItem == null || newItem.Database.DataManager.GetWorkflowInfo(newItem) == null)
{
return;
}
Item originalItem = newItem.Database.GetItem(newItem.ID, newItem.Language, newItem.Version);
newItem.Fields.ReadAll();
IEnumerable<string> fieldNames = newItem.Fields.Select(f => f.Name);
IEnumerable<string> differences = fieldNames.Where(fieldName => newItem[fieldName] != originalItem[fieldName]).ToList();
if (differences.Any())
{
string message = String.Format("Item content changed [{0}]", differences.Aggregate((s1, s2) => s1 + ", " + s2));
AddHistoryEntry(newItem, message);
}
}
public static void AddHistoryEntry(Item item, string text)
{
WorkflowProvider workflowProvider = (item.Database.WorkflowProvider as WorkflowProvider);
if (workflowProvider != null && workflowProvider.HistoryStore != null)
{
string workflowState = GetWorkflowState(item);
workflowProvider.HistoryStore.AddHistory(item, workflowState, workflowState, text);
}
}
private static string GetWorkflowState(Item item)
{
WorkflowInfo info = item.Database.DataManager.GetWorkflowInfo(item);
return (info != null) ? info.StateID : String.Empty;
}
}
}

Related

Changing workflow state in Sitecore

I'm having an issue changing the workflow state for an item programmatically. The state isn't being changed no matter what I do to the field. I've tried using (new SecurityDisabler()){} and putting the item in editing mode then changing the field manually. I've noticed that the item itself has the Lock set to <r />, could this be causing an issue?
Here is some sample code of what I've tried to do:
[HttpPost]
[MultipleButton(Name = "action", Argument = "Submit")]
public ActionResult Submit(LoI model)
{
if (model.Submitted || !model.Signed)
{
return Redirect("/Profile/LoI");
}
ModifyCandidateInfo(model, true);
Session["message"] = Translate.Text("loi-submitted-message");
Session["messageClass"] = "success";
return Redirect("/Profile/LoI");
}
private static void ModifyCandidateInfo(LoI model, bool isSubmission)
{
using (new SecurityDisabler())
{
var candidateFolder = CBUtility.GetCandidateFolder();
var loi= candidateFolder.GetChildren().SingleOrDefault(loi => loi.TemplateID == LoITemplateId);
if (loi == null) return;
loi.Editing.BeginEdit();
EditFields(loi, model);
EditChildren(loi, model);
//Send emails upon submission
if (isSubmission)
{
loi.ExecuteCommand("Submit",
loi.Name + " submitted for " + model.CandidateName);
using (new SecurityDisabler())
{
loi.Editing.BeginEdit();
loi.Fields["__Workflow state"].Value = "{F352B651-341B-4CCF-89FE-BD77F5E4D540}";
loi.Editing.EndEdit();
}
}
loi.Editing.EndEdit();
}
}
I initalized the item's workflow with the following function:
public static void InitializeWorkflow(Item item, ID workflowId)
{
item.Editing.BeginEdit();
var workflow =
item.Database.WorkflowProvider.GetWorkflow(workflowId.ToString());
workflow.Start(item);
item.Editing.EndEdit();
}
The item starts at the default drafting state and executed a "Submit" command that fires off emails. Through the Sitecore UI if I hit submit it'll go to the next workflow state but not programmatically when I fire off the ExecuteCommand function. Below you'll find the ExecuteCommand function.
public static WorkflowResult ExecuteCommand(this Item item, string commandName, string comment)
{
using (new SecurityDisabler())
{
var workflow = item.Database.WorkflowProvider.GetWorkflow(item);
if (workflow == null)
{
return new WorkflowResult(false, "No workflow assigned to item");
}
var command = workflow.GetCommands(item[FieldIDs.WorkflowState])
.FirstOrDefault(c => c.DisplayName == commandName);
return command == null
? new WorkflowResult(false, "Workflow command not found")
: workflow.Execute(command.CommandID, item, comment, false);
}
}
The command fires off fine and the emails are sent but I can't figure out why the state won't change. Could someone provide me with other suggestions or a solution?
Am I reading the workflow state id correctly? I'm using the item ID for the workflow state.
I think your code is really similar to my implementation. This is my code's background.
All items have the same workflow named "WF" and it has three workflow states (Working, Awaiting Approval, and Approved). One page-item having "WF" has some rendering items and those datasource items. Suppose a content editor is ready to submit and approve the item with its related items. By hitting the "Submit" and "Approval" button in the page, all page-item's related items have the same workflow state as the page-item's one.
Most code are from Marek Musielak and this code is perfectly working in my side.
public class UpdateWorkflowState
{
// List all controls in page item
public RenderingReference[] GetListOfSublayouts(string itemId, Item targetItem)
{
RenderingReference[] renderings = null;
if (Sitecore.Data.ID.IsID(itemId))
{
renderings = targetItem.Visualization.GetRenderings(Sitecore.Context.Device, true);
}
return renderings;
}
// Return all datasource defined on one item
public IEnumerable<string> GetDatasourceValue(WorkflowPipelineArgs args, Item targetItem)
{
List<string> uniqueDatasourceValues = new List<string>();
Sitecore.Layouts.RenderingReference[] renderings = GetListOfSublayouts(targetItem.ID.ToString(), targetItem);
LayoutField layoutField = new LayoutField(targetItem.Fields[Sitecore.FieldIDs.FinalLayoutField]);
LayoutDefinition layoutDefinition = LayoutDefinition.Parse(layoutField.Value);
DeviceDefinition deviceDefinition = layoutDefinition.GetDevice(Sitecore.Context.Device.ID.ToString());
foreach (var rendering in renderings)
{
if (!uniqueDatasourceValues.Contains(rendering.Settings.DataSource))
uniqueDatasourceValues.Add(rendering.Settings.DataSource);
}
return uniqueDatasourceValues;
}
// Check workflow state and update state
public WorkflowResult ChangeWorkflowState(Item item, ID workflowStateId)
{
using (new EditContext(item))
{
item[FieldIDs.WorkflowState] = workflowStateId.ToString();
}
Sitecore.Layouts.RenderingReference[] renderings = GetListOfSublayouts(item.ID.ToString(), item);
return new WorkflowResult(true, "OK", workflowStateId);
}
// Verify workflow state and update workflow state
public WorkflowResult ChangeWorkflowState(Item item, string workflowStateName)
{
IWorkflow workflow = item.Database.WorkflowProvider.GetWorkflow(item);
if (workflow == null)
{
return new WorkflowResult(false, "No workflow assigned to item");
}
WorkflowState newState = workflow.GetStates().FirstOrDefault(state => state.DisplayName == workflowStateName);
if (newState == null)
{
return new WorkflowResult(false, "Cannot find workflow state " + workflowStateName);
}
unlockItem(newState, item);
return ChangeWorkflowState(item, ID.Parse(newState.StateID));
}
// Unlock the item when it is on FinalState
public void unlockItem(WorkflowState newState, Item item)
{
if (newState.FinalState && item.Locking.IsLocked())
{
using (new EditContext(item, false, false))
{
item["__lock"] = "<r />";
}
}
}
}

Run code when Publishing Restriction is saved in Sitecore

I need to run code when an author saves a publishing restriction for an item.
How would I go about doing that?
The time restrictions are stored in the "__Valid to" and "__Valid from" fields. Attach a new pipe like this:
<event name="item:saved">
<handler type="Test.ValidTest, Test" method="OnItemSaved" />
</event>
And then test if those fields changed and do your thing:
public class ValidTest
{
private static readonly ID __Validfrom = new ID("{C8F93AFE-BFD4-4E8F-9C61-152559854661}");
private static readonly ID __Validto = new ID("{4C346442-E859-4EFD-89B2-44AEDF467D21}");
public void OnItemSaved(object sender, EventArgs args)
{
Item obj = Event.ExtractParameter(args, 0) as Item;
if (obj == null)
return;
//if (!(obj.TemplateID == YourTemplateId)) //restrict this to a limited set of templates if possible
// return;
try
{
ItemChanges itemChanges = Event.ExtractParameter(args, 1) as ItemChanges;
if (itemChanges != null &&
(itemChanges.FieldChanges.Contains(__Validfrom) || itemChanges.FieldChanges.Contains(__Validto)))
{
//YOUR THING here
Log.Info("Changed!", (object)this);
}
}
catch (Exception ex)
{
Log.Error("failed", ex, (object)this);
}
}
}

Are Sitecore events handled synchronously?

I want to hook the item:renamed event to do some processing. It may take a few minutes though. Are event handlers executed asynchronously or synchronously with normal pipeline execution? Is there a standard Sitecore way to kick this off asynchronously if I need to do that myself?
The only time this handler needs to execute is when an item is renamed in Content Editor.
Sitecore events are executed synchronously. There is a Sitecore Development Toolkit module on Sitecore Marketplace which contains a code for firing events asynchronously which you can easily reuse in your solution Sitecore Development Toolkit.
Here is a part of their code which fires methods asynchronously when the event is fired:
public void OnItemRenamed(object sender, EventArgs args)
{
if (args != null)
{
var item = Event.ExtractParameter(args, 0) as Item;
Assert.IsNotNull(item, "No item in parameters");
var name = Event.ExtractParameter(args, 1) as string;
Assert.IsNotNullOrEmpty(name, "No name in parameters");
DoAsync(() => OnItemRenameAsync(item, name));
}
}
private void OnItemRenameAsync(Item item, string name)
{
var itemRef = new ItemReference(item.Parent);
var itemRefText = itemRef.ToString();
// do some work here
}
Sitecore events are synchronous. You can kick off your long running task as a job. First create a class to handle the event:
namespace MyNamespace
{
public class MyClass
{
public void ItemRenamed (object sender, EventArgs args)
{
Run("LongRenameTask");
}
protected void Run(string methodName, EventArgs args)
{
var item = Event.ExtractParameter(args, 0) as Item;
var name = Event.ExtractParameter(args, 1) as string;
RunJob(methodName, item, name);
}
protected Handle RunJob(string methodName, Item item, string name)
{
var options = new JobOptions(
"Preparing rename job '{0}' for '{1}'".FormatWith(
methodName,
item.ID.ToString()),
"item:renamed",
"shell",
new ItemRenamedManager(item, name),
methodName)
{
WriteToLog = true,
AtomicExecution = true,
};
var job = new Job(options);
JobManager.Start(job);
return job.Handle;
}
}
}
Then create a class to do your work (this will be called on a background thread by Sitecore):
namespace MyNamespace
{
public class ItemRenamedManager
{
protected Item RenamedItem { get; set; }
protected string Name { get; set; }
public ItemRenamedManager(Item item, string name)
{
RenamedItem = item;
Name = name;
}
public void LongRenameTask()
{
// Do your long running task here.
// The property 'RenamedItem' will give you the item
}
}
}
Then patch your event handler in:
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
<sitecore>
<events>
<event name="item:renamed">
<handler type="MyNamespace.MyClass" method="ItemRenamed" />
</event>
</events>
</sitecore>
</configuration>
The above code is cribbed a bit from memory and needs some error handling, but should be pretty close, but this way, your long running task won't block the Content Editor UI.

How to sort the selected items in a Sitecore Treelist?

Is there a way to always have the selected items in a Sitecore Treelist sorted alphabetically?
No, but you could look in to creating your own 'sorted treelist'. Someone asked a different question earlier today but it has basically the same answer:
Sitecore Tree list datasource - VersionExist
Sitecore lets you create custom field types. They can be based on existing ones, but with some added tweaks.
As mentioned in the answers to the other question, here are 2 articles which are good places to start:
Creating a Composite Custom Field
Apply Dynamic TreeList Source Parameters with the Sitecore ASP.NET CMS
Here's my implementation, which although long, is mostly copy-and-pasted from the decompiled Treelist code. I've highlighted which bits which are new in the Value property and the Add method:
namespace CustomFieldTypes
{
public class Treelist : Sitecore.Shell.Applications.ContentEditor.TreeList
{
public override string Value
{
get
{
// ---------- New code here -----------
var ids = base.Value.Split('|');
var db = Sitecore.Configuration.Factory.GetDatabase("master");
var items = ids.Select(id => db.GetItem(id)).Where(item => item != null);
var orderedItems = items.OrderBy(item => item.Name);
var orderedIds = orderedItems.Select(item => item.ID.ToString());
return String.Join("|", orderedIds);
// ---------------------------------------
}
set
{
base.Value = value;
}
}
protected void Add()
{
if (this.Disabled)
return;
string viewStateString = this.GetViewStateString("ID");
TreeviewEx treeviewEx = this.FindControl(viewStateString + "_all") as TreeviewEx;
Assert.IsNotNull((object) treeviewEx, typeof (DataTreeview));
Listbox listbox = this.FindControl(viewStateString + "_selected") as Listbox;
Assert.IsNotNull((object) listbox, typeof (Listbox));
Item selectionItem = treeviewEx.GetSelectionItem();
if (selectionItem == null)
{
SheerResponse.Alert("Select an item in the Content Tree.", new string[0]);
}
else
{
if (this.HasExcludeTemplateForSelection(selectionItem))
return;
if (this.IsDeniedMultipleSelection(selectionItem, listbox))
{
SheerResponse.Alert("You cannot select the same item twice.", new string[0]);
}
else
{
if (!this.HasIncludeTemplateForSelection(selectionItem))
return;
SheerResponse.Eval("scForm.browser.getControl('" + viewStateString + "_selected').selectedIndex=-1");
ListItem listItem = new ListItem();
listItem.ID = Sitecore.Web.UI.HtmlControls.Control.GetUniqueID("L");
// ----- New Code Here -----------------------
bool listItemAdded = false;
for (int i = 0; i < listbox.Controls.Count; i++ )
{
ListItem control = (ListItem)listbox.Controls[i];
if (control == null)
return;
if (String.Compare(GetHeaderValue(selectionItem), control.Header) < 0)
{
listbox.Controls.AddAt(i, listItem);
listItemAdded = true;
break;
}
}
if (!listItemAdded)
{
Sitecore.Context.ClientPage.AddControl((System.Web.UI.Control)listbox, (System.Web.UI.Control)listItem);
}
// ------------------------------------------
listItem.Header = this.GetHeaderValue(selectionItem);
listItem.Value = listItem.ID + (object) "|" + (string) (object) selectionItem.ID.ToString();
SheerResponse.Refresh((Sitecore.Web.UI.HtmlControls.Control) listbox);
SetModified();
}
}
}
protected static void SetModified()
{
Sitecore.Context.ClientPage.Modified = true;
}
private bool HasIncludeTemplateForSelection(Item item)
{
Assert.ArgumentNotNull((object)item, "item");
if (this.IncludeTemplatesForSelection.Length == 0)
return true;
else
return HasItemTemplate(item, this.IncludeTemplatesForSelection);
}
private bool HasExcludeTemplateForSelection(Item item)
{
if (item == null)
return true;
else
return HasItemTemplate(item, this.ExcludeTemplatesForSelection);
}
private bool IsDeniedMultipleSelection(Item item, Listbox listbox)
{
Assert.ArgumentNotNull((object)listbox, "listbox");
if (item == null)
return true;
if (this.AllowMultipleSelection)
return false;
foreach (Sitecore.Web.UI.HtmlControls.Control control in listbox.Controls)
{
string[] strArray = control.Value.Split(new char[1]
{
'|'
});
if (strArray.Length >= 2 && strArray[1] == item.ID.ToString())
return true;
}
return false;
}
private static bool HasItemTemplate(Item item, string templateList)
{
Assert.ArgumentNotNull((object)templateList, "templateList");
if (item == null || templateList.Length == 0)
return false;
string[] strArray = templateList.Split(new char[1]
{
','
});
ArrayList arrayList = new ArrayList(strArray.Length);
for (int index = 0; index < strArray.Length; ++index)
arrayList.Add((object)strArray[index].Trim().ToLowerInvariant());
return arrayList.Contains((object)item.TemplateName.Trim().ToLowerInvariant());
}
}
}
When this is compiled and in your application you need to go to /sitecore/system/Field types/List Types/Treelist in the core database. In there, you need to fill in the Assembly and Class fields, and clear out the Control fields.
You could create a custom field and sort the items in the selected list using generics, then save the guids back to the field. I covered this in a recent blog post. There isn't that much coding involved.

Event handler item:saved - failing?

I have the following .cs file;
using System;
using System.Collections.Generic;
using Sitecore.Data;
using Sitecore.Data.Items;
using Sitecore.Diagnostics;
using Sitecore.Events;
using Sitecore.SecurityModel;
namespace LocationItemEventHandler
{
public class ItemEventHandler
{
private static readonly SynchronizedCollection<ID> MProcess = new SynchronizedCollection<ID>();
/// <summary>
/// This custom event auto-populates latitude/longitude co-ordinate when a location item is saved
/// </summary>
/// <param name="sender"></param>
/// <param name="args"></param>
protected void OnItemSaved(object sender, EventArgs args)
{
var item = Event.ExtractParameter(args, 0) as Item;
if (item != null && !MProcess.Contains(item.ID))
{
if (item.TemplateID.Equals("{E490971E-758E-4A75-9C8D-67EC2C6321CA}"))
{
string errMessage = "";
string responseCode = "";
string address = item.Fields["Address"].Value;
if (1=1)
{
string latitude = "100";
string longitude = "200";
MProcess.Add(item.ID);
try
{
var latlngField = item.Fields["Google LatLng"];
using (new SecurityDisabler())
{
item.Editing.BeginEdit();
latlngField.SetValue(latitude + " - " + longitude, true);
Sitecore.Context.ClientPage.ClientResponse.Alert(
string.Format(
"Fields updated automatically\r\nLatitude: {0}\r\nLongitude: {1}",
latitude, longitude));
item.Editing.EndEdit();
}
}
catch (Exception exception)
{
Log.Error(exception.Message, this);
}
finally
{
MProcess.Remove(item.ID);
}
}
}
}
}
}
}
This is in a code file LocationItemEventHandler.cs in App_Code.
this is in the web.config
<event name="item:saved">
<handler type="LocationItemEventHandler.ItemEventHandler, LocationItemEventHandler" method="OnItemSaved"/>
</event>
When i try to save an item i get a "Could not resolve type name".
What am i missing?
The class name you have configured does not match the actual class name.
Edit:
If the handler is in App_Code, i believe you should leave off the assembly name in the type reference. Better yet, don't use App_Code.