Author: vindaav

Sitecore: How to import content with minimum effort

Very often (in testing perspective or vital necessity) there is a need to import data into the Sitecore. There are many tools that can be used for such purpose. But I want to propose you something very simple, something readily available, and yet something powerful and flexible enough to build a reusable component. In such a case I would use Sitecore Powershell Extensions.

Continue reading “Sitecore: How to import content with minimum effort”

Advertisements

Sitecore: Search for Items That Don’t Have Any Language Versions Using Powershell

Recently, I have faced with an issue using traditional Powershell commands. I was trying to identify items that did not have any associated language version. There are various ways of how an item can get to this state. I will not get into these cases here, because they are typically specific to the implementation. But, what is certain across all cases is that these items cause problems because they (and their descendants) don’t get published. This causes a lot of confusion between what you see in Experience Editor and on the front-end site – items are not the same.

The problem lies in the format of traditional Powershell commands which are require a -Language param to retrieve specific versions. I specifically needed to identify unversioned items, but it was not so simply:

  • If you leave the language param off, it will use “en” as the default choice
  • If you pass an empty string for this param, it will return all versions of items that have language versions.

That’s great, but I still had not udentifier the items without a language version. If you want to reproduce it, just create a new item, then go to the “Versions” tab and delete the current version.

Here comes the Powershell

Since I could not use Get-ChildItems to get my unversioned items, I had the idea to look for items that have a collective count of all language versions equal to 0. There is a wonderful method  that helps me to achive my goal – GetVersions. It receives at the input bool includeAllLanguages and returns an array of items. This is the perfect method to leverage since I can test for an empty count to determine if it’s one of the unversioned items.

# point this to the root of your website
$folder = "/sitecore/content"

# Where-Object clause could be combined with other filters
# like Name or Template to narrow results for something specific
$items = Get-ChildItem -Path $folder -recurse | `
Where-Object { $_.Versions.GetVersions($true).Count -eq 0 } | `
%{
[PSCustomObject]@{
"ID" = $_.ID
"ItemName" = $_.Name
"Item" = $_
"ItemPath" = $_.Paths.Path
}
}

# print out any results that were found
$items

I hope this work around will save you some time and frustration. As I mentioned, being able to identify these items is critical to resolving the conflict between what you are seeing in the Experience Editor and what is showing on the front-end site.

Enjoy!

Sitecore Content Editor: Performance Tips

Sitecore Content Editor: Performance Tips

While Sitecore continues to extend Content Editor functionality to the Experience Editor, Content Editor remains the interface of choice for developers and power users. Its performance can significantly impact the productivity of any development team. With the fairly simple adjustments I outline here, you can optimize Sitecore Content Editor performance and usability.

Continue reading “Sitecore Content Editor: Performance Tips”

Sitecore: Security best practices

From time to time developer faces with the need to perform some action with the item in the scope of administrative user. In such case there are two possibilities:
SecurityDisabler and UserSwitcher.

Both of them allows developer to solve the task.  So, let’s what is the difference between them and which of them is most preferable to use.

Continue reading “Sitecore: Security best practices”

Log4net: Including stack trace into the logs

The log4net version included in Sitecore doesn’t contains the stack trace. That means that you cannot use %stacktrace or %stacktracedetail in the conversionsPattern.

I have found the way of how to do it. As a workaround, you should override the appender!

First create the following class:

public class SitecoreLogFileWithStackTraceAppender : SitecoreLogFileAppender
{
 protected override void Append(LoggingEvent loggingEvent)
 {
 var msg = loggingEvent.MessageObject as string;
 if (msg != null)
 {
 var prop = loggingEvent.GetType().GetField("m_data", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
 var mData = (LoggingEventData)prop.GetValue(loggingEvent);
 mData.Message = $"{msg}{Environment.NewLine}{Environment.StackTrace}";
 prop.SetValue(loggingEvent, mData);
 }
base.Append(loggingEvent);
 }
}

Second step, register newly created log4net appender:

<appender name="StackFileAppender" type="AndreyVinda.Loggers.SitecoreLogFileWithStackTraceAppender, AndreyVinda.Loggers">
 <file value="$(dataFolder)/logs/log.Stacktraces.{date}.txt" />
<appendToFile value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%5t %d{yyyy-MM-dd HH:mm:ss.fff} %-5p %c - %m%n%n" />
</layout>
</appender>

For one of my projects I have chosen to log all the errors and all the messages that contains [SD] within the stacktrace. The config file is the following:

<appender name="StackFileAppender" type="AndreyVinda.Loggers.SitecoreLogFileWithStackTraceAppender, AndreyVinda.Loggers">
 <file value="$(dataFolder)/logs/log.Stacktraces.{date}.txt" />
<filter type="log4net.Filter.StringMatchFilter">
<stringToMatch value="[SD]"></stringToMatch>
</filter>
<filter type="log4net.Filter.DenyAllFilter" />
<appendToFile value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%5t %d{yyyy-MM-dd HH:mm:ss.fff} %-5p %c - %m%n%n" />
</layout>
</appender>

Enjoy!

Sitecore: Force of Query String Parameters

Sitecore is a web based system and just like to others systems it uses query string parameters to pass viable data. They are used to explicitly configure such important things as display modes, sites, languages etc. Remembering of those tricky things could have a huge speed improvement in cases when dealing with multi-language, multi-device or page editor enabled solutions. Below is the list of the Sitecore query string parameters that I have been using in my day-to-day job.

Continue reading “Sitecore: Force of Query String Parameters”