Using custom sitecore data item clone notification - sitecore

I understand that you can add new Notification for usage when cloning data items.
Where do you specify where you should use the custom Notification class?

We display a warning on items that have been cloned. The trick is to use the "getContentEditorWarnings" pipeline:
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
<sitecore>
<pipelines>
<getContentEditorWarnings>
<processor type="Example.OriginalItem, Example" patch:after="processor[#type='Sitecore.Pipelines.GetContentEditorWarnings.Notifications, Sitecore.Kernel']" />
</getContentEditorWarnings>
</pipelines>
</sitecore>
</configuration>
Then the code for this pipeline is:
using Sitecore.Globalization;
using Sitecore.Pipelines.GetContentEditorWarnings;
namespace Example
{
public class OriginalItem
{
public void Process(GetContentEditorWarningsArgs args)
{
var item = args.Item;
if ((item == null) || item.GetClones().Count() == 0) return;
var warning = args.Add();
warning.Title = "This Item has clones";
warning.IsExclusive = false;
}
}
}
Not really germane to your question, but in this example, we use the links db to find if the item has clones:
public static IEnumerable<Item> GetClones(this Item original)
{
Assert.ArgumentNotNull(original, "source");
return (from link in Globals.LinkDatabase.GetReferrers(original)
select link.GetSourceItem() into clone
where ((clone != null) && (clone.Source != null)) && (clone.Source.ID == original.ID)
select clone);
}

Related

How to set HttpOnly and Secure "lang" Sitecore cookie?

Anyone, how to set HttpOnly and Secure flag of "lang" Sitecore cookie?
I've tried to set via processor pipeline as the following code
public class CookiesFlagResolver : HttpRequestProcessor
{
public override void Process(HttpRequestArgs args)
{
HttpContext current = HttpContext.Current;
if (current != null)
{
SiteContext context = Context.Site;
string cookieKey = context.GetCookieKey("lang");
HttpCookie cookie = current.Request.Cookies[cookieKey];
if (cookie != null)
{
if (cookie.HttpOnly == false || cookie.Secure == false)
{
cookie.HttpOnly = true;
cookie.Secure = true;
current.Response.AppendCookie(cookie);
}
}
}
}
}
Patch configuration
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/" xmlns:set="http://www.sitecore.net/xmlconfig/set/" xmlns:role="http://www.sitecore.net/xmlconfig/role/">
<sitecore role:require="Standalone or ContentDelivery or ContentManagement">
<pipelines>
<httpRequestBegin>
<processor type="Website.Custom.Pipelines.CookiesFlagResolver, Website" patch:before="processor[#type='Sitecore.Pipelines.HttpRequest.LanguageResolver, Sitecore.Kernel']"/>
</httpRequestBegin>
</pipelines>
</sitecore>
</configuration>
I have checked that the code is being hit correctly when i debug it.
Unfortunately the "lang" cookie in browser still disabled.

Sitecore Publishing Restriction Override

I have two publishing target - one is stage and one is production.The publishing on Production should adhere to the publishing restrictions, but publishing on Stage should not look at or discard the valid to and valid from dates and publish under any circumstance.
I have written a publishing pipeline (PipelinePublishProvider). I am not sure how could I manage to overwrite the field values temporarily so it publishes on to stage every-time.
public class StagePublishOverride : PipelinePublishProvider
{
public override PublishHelper CreatePublishHelper(PublishOptions options)
{
Assert.ArgumentNotNull(options, "options");
if (options.TargetDatabase.Name.ToLower() == "stage")
{
Item itemToBePublished = new Item(options.RootItem.ID, options.RootItem.InnerData, new Database("web"));
itemToBePublished.Editing.BeginEdit();
itemToBePublished.Publishing.ValidTo = DateTime.MaxValue;
itemToBePublished.Publishing.ValidFrom = DateTime.MinValue;
itemToBePublished.Editing.EndEdit();
options.RootItem = itemToBePublished;
}
if (options is ExtendedPublishOptions)
return new ExtendedPublishHelper(options as ExtendedPublishOptions);
return new PublishHelper(options);
}
}
public class ExtendedPublishHelper : PublishHelper
{
private readonly ExtendedPublishOptions _options;
public ExtendedPublishHelper(ExtendedPublishOptions options)
: base(options)
{
_options = options;
}
public override Item GetVersionToPublish(Item sourceItem)
{
Assert.ArgumentNotNull(sourceItem, "sourceItem");
if (Options is ExtendedPublishOptions)
{
return sourceItem.Publishing.GetValidVersion(Options.PublishDate, _options.RequireApproval);
}
return sourceItem.Publishing.GetValidVersion(Options.PublishDate, true);
}
}
public class ExtendedPublishOptions : PublishOptions
{
public ExtendedPublishOptions(Database sourceDatabase, Database targetDatabase, PublishMode mode, Language language, DateTime publishDate, bool requireApproval)
: base(sourceDatabase, targetDatabase, mode, language, publishDate)
{
RequireApproval = requireApproval;
}
public bool RequireApproval { get; set; }
}
}
I believe you'll be better off adding a processor to the publishItem pipeline. Here is some UNTESTED code, which I think will serve your purpose:
public class PublishOverride : PublishItemProcessor
{
public override void Process(PublishItemContext context)
{
Assert.ArgumentNotNull((object)context, "context");
if (context.Action != PublishAction.None)
return;
Item sourceItem = this.GetSourceItem(context);
if (sourceItem == null)
return;
var stagingDB = Factory.GetDatabase("Stage");
if (stagingDB != null && !sourceItem.Publishing.NeverPublish && context.PublishContext.PublishOptions.TargetDatabase == stagingDB)
{
context.Action = PublishAction.PublishVersion;
context.VersionToPublish = sourceItem;
}
}
private Item GetSourceItem(PublishItemContext context)
{
Assert.ArgumentNotNull((object)context, "context");
return context.PublishHelper.GetSourceItem(context.ItemId);
}
}
Make sure you patch it in before the DetermineAction processor in the default config. So your config patch would look like this:
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/" xmlns:x="http://www.sitecore.net/xmlconfig/">
<sitecore>
<pipelines>
<publishItem>
<processor patch:before="*[#type='Sitecore.Publishing.Pipelines.PublishItem.DetermineAction, Sitecore.Kernel']"
type="YourNamespace.PublishOverride, YourAssembly" />
</publishItem>
</pipelines>
</sitecore>
</configuration>

Hide Quick info in Sitecore through code

I want to hide quick info section through code instead of unchecking the check box in Application Options dialog box. Can someone help in this?
The following code does exactly what your looking for.
Add this code below:
namespace Custom.Framework.SC.Extensions.Pipelines
{
using Sitecore.Pipelines.LoggedIn;
using SC = Sitecore;
/// <summary>The default quick info.</summary>
public class DefaultQuickInfo : SC.Pipelines.LoggedIn.LoggedInProcessor
{
/// <summary>The process.</summary>
/// <param name="args">The args.</param>
public override void Process(LoggedInArgs args)
{
const string DefaultToVisible = "false";
SC.Diagnostics.Assert.ArgumentNotNull(args, "args");
SC.Diagnostics.Assert.IsTrue(
SC.Security.Accounts.User.Exists(args.Username),
args.Username);
var user = SC.Security.Accounts.User.FromName(
args.Username,
true);
SC.Diagnostics.Assert.IsNotNull(user, "user");
var sitecoreDomain = SC.SecurityModel.DomainManager.GetDomain(
"sitecore");
SC.Diagnostics.Assert.IsNotNull(sitecoreDomain, "sitecoreDomain");
if (user.Domain != sitecoreDomain
|| user.Name.ToLower().EndsWith("\\" + sitecoreDomain.AnonymousUserName))
{
SC.Diagnostics.Log.Warn(this + " : unexpected security domain or user : " + user.Name, this);
return;
}
var key = "/" + args.Username + "/UserOptions.ContentEditor.ShowQuickInfo";
if (string.IsNullOrEmpty(user.Profile[key]))
{
user.Profile[key] = DefaultToVisible;
user.Profile.Save();
}
}
}
}
Then patch in this configuration change to add the processor to the appropriate place:
<?xml version="1.0" encoding="utf-8" ?>
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
<sitecore>
<processors>
<loggedin>
<processor patch:after="processor[position()=last()]" type="Custom.Framework.SC.Extensions.Pipelines.DefaultQuickInfo, Custom.Framework.SC.Extensions" />
</loggedin>
</processors>
</sitecore>
</configuration>

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);
}
}
}

Track Sitecore item history

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;
}
}
}