Solr startup issue as window service on Windows 10

If you are configuring Sitecore 9 then you have to setup Solr as window service for supporting xConnect. While starting Solr as window service you may have following issue

Program “Solr Path” exited with return code 3221225794.

To fix this issue , kindly make sure that you are using windows 10 supported version of NSSM. Here are the comments available on https://nssm.cc/download for windows 10

Windows 10 Creators Update

2017-04-26: Users of Windows 10 Creators Update should use prelease build 2.2.4-101 to avoid an issue with services failing to start. If for some reason you cannot use that build you can also set AppNoConsole=1 in the registry (\HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\<servicename>\Parameters)

, noting that applications which expect a console window may behave unexpectedly.

 

Sitecore – Failed to find the root item

Recently content managers were getting following issue while accessing the file upload option to import the translations. After analysis we found that its  

<securityEnabled>true</securityEnabled>

configuration under the filesystem database node (

<database id="filesystem" singleInstance="true" type="Sitecore.Data.Database, Sitecore.Kernel">

). To fix this issue you may disable the security for file system.

Error : An instance of Sitecore.Data.Items.Item was null. Additional information: Failed to find the root item “/”.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidOperationException: An instance of Sitecore.Data.Items.Item was null. Additional information: Failed to find the root item “/”.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:

[InvalidOperationException: An instance of Sitecore.Data.Items.Item was null. Additional information: Failed to find the root item “/”.]
Sitecore.Web.UI.HtmlControls.DataContext.GetState(Item& root, Item& folder, Item[]& selected) +1904
Sitecore.Web.UI.HtmlControls.DataContext.get_AtRoot() +60
Sitecore.Web.UI.HtmlControls.UpAction.OnDataContextChanged(DataContext dataContext) +24
Sitecore.Web.UI.HtmlControls.Control.HandleMessage(Message message) +391
Sitecore.Shell.Framework.Commands.MessageCommand.DispatchMessage(Control control) +67
Sitecore.Shell.Framework.Commands.MessageCommand.DispatchMessage(Control control) +157
Sitecore.Shell.Framework.Commands.MessageCommand.DispatchMessage(Control control) +157
Sitecore.Shell.Framework.Commands.MessageCommand.DispatchMessage(Control control) +157
Sitecore.Shell.Framework.Commands.MessageCommand.DispatchMessage(Control control) +157
Sitecore.Shell.Framework.Commands.MessageCommand.DispatchMessage(Control control) +157
Sitecore.Shell.Framework.Commands.MessageCommand.DispatchMessage(Control control) +157
Sitecore.Shell.Framework.Commands.MessageCommand.DispatchMessage(Control control) +157
Sitecore.Shell.Framework.Commands.MessageCommand.DispatchMessage(Control control) +157
Sitecore.Shell.Framework.Commands.MessageCommand.DispatchMessage(Control control) +157
Sitecore.Shell.Framework.Commands.MessageCommand.Execute(CommandContext context) +636
Sitecore.Web.UI.Sheer.ClientPage.SendMessage(Message message) +90
Sitecore.Web.UI.HtmlControls.DataContext.OnChanged() +104
Sitecore.Web.UI.HtmlControls.DataContext.set_DataViewName(String value) +186
Sitecore.Shell.Applications.Files.FileExplorer.FileExplorerForm.OnLoad(EventArgs e) +97

[TargetInvocationException: Exception has been thrown by the target of an invocation.]
System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor) +0
System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments) +128
System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) +146
Sitecore.Reflection.ReflectionUtil.CallMethod(Type type, Object obj, String methodName, Boolean includeNonPublic, Boolean includeInherited, Boolean includeStatic, Object[] parameters) +223
Sitecore.Web.UI.Sheer.ClientPage.OnLoad(EventArgs e) +592
System.Web.UI.Control.LoadRecursive() +68
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +4498

How to get Sitecore Agent details

If you have scenario to get the details of Sitecore Agents, its last run time and interval to run, below code will help

	Sitecore.Tasks.Scheduler scheduler = new Sitecore.Tasks.Scheduler();
            var agentData  = scheduler.GetType().GetField("m_agents", BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance).GetValue(scheduler);

            Type schedulerType = typeof(Sitecore.Tasks.Scheduler);
            Type agentType = schedulerType.GetNestedType("Agent", BindingFlags.NonPublic);
            FieldInfo lastrunpro = agentType.GetField("m_lastRun", BindingFlags.NonPublic | BindingFlags.Instance);
            FieldInfo Name = agentType.GetField("m_name", BindingFlags.NonPublic | BindingFlags.Instance);
            FieldInfo interval = agentType.GetField("m_interval", BindingFlags.NonPublic | BindingFlags.Instance);

            if (agentData != null)
            {
                var agentCollection = agentData as IEnumerable;

                foreach (object agent in agentCollection)
                {
                    AgentsList.Add(new AgentsInfo() {
                        Name = Convert.ToString(Name.GetValue((agent))).Replace("Sitecore.Tasks.", ""),
                        LastRunTime = Convert.ToString(lastrunpro.GetValue((agent))),
                        Interval = Convert.ToString(interval.GetValue((agent)))
                    });
                }
            }
        }

 

 

Sitecore Customize Media Provider

There are scenarios when you have to customize/override Sitecore default media provider. Like in my case we enabled Akamai (CDN) for media items and  requirement was to generate the media items url using Akamai domain rather than site domain.  There are other scenarios also where you may have to customize media provider.

So to customize media provider

  • Create a new public class and inherit it from resource.Media.MediaProvider class and Sitecore.Events.Hooks.IHook Interface, like
public class CustomMediaProvider : MediaProvider, Sitecore.Events.Hooks.IHook
  • Create initialize method
public void Initialize()
        {
            MediaManager.Provider = this;
        }

  • Create constructor of class and create mediacreator object
public CustomMediaProvider ()
        {
            OverrideMediaCreator();
        }

        protected void OverrideMediaCreator()
        {
            Sitecore.Resources.Media.MediaCreator mediaCreator = GetMediaCreator();
            if (mediaCreator == null)
            {
                return;
            }

            Creator = mediaCreator;
        }

        protected virtual Sitecore.Resources.Media.MediaCreator GetMediaCreator()
        {
            return Factory.CreateObject("mediaLibrary/mediaCreator", false) as MediaCreator;
}
  • Override the GetMediaUrl and customize the code as per your need
public override string GetMediaUrl(MediaItem item)
        {
            return GetCDNURL(base.GetMediaUrl(item));
        }

        public override string GetMediaUrl(MediaItem item, MediaUrlOptions options)
        {
            return GetCDNUrl(base.GetMediaUrl(item, options));
        }

  • Do a entry in custom patch file
<sitecore>
<hooks>
      <hook  type="Assembly.Namespace.CustomMediaProvider, Assembly Details">
      </hook>
    </hooks>
</sitecore>
  •  Complete code is as follows
public class CustomMediaProvider : MediaProvider,IHook
    {
        public void Initialize()
        {
            MediaManager.Provider = this;
        }

        public override string GetMediaUrl(MediaItem item)
        {
            return GetCDNUrl(base.GetMediaUrl(item));
        }

        public override string GetMediaUrl(MediaItem item, MediaUrlOptions options)
        {
            return GetCDNUrl(base.GetMediaUrl(item, options));
        }

        string GetCDNUrl (string url)
        {
            ….
        }

        public CustomMediaProvider ()
        {
            OverrideMediaCreator();
        }

        protected void OverrideMediaCreator()
        {
            Sitecore.Resources.Media.MediaCreator mediaCreator = GetMediaCreator();
            if (mediaCreator == null)
            {
                return;
            }

            Creator = mediaCreator;
        }

        protected virtual Sitecore.Resources.Media.MediaCreator GetMediaCreator()
        {
            return Factory.CreateObject("mediaLibrary/mediaCreator", false) as MediaCreator;
        }
    }