While learning about Remove-ItemVersion, I came across below piece of code -

New-UsingBlock(New-Object Sitecore.Data.Events.EventDisabler) {
    Remove-ItemVersion -Path master:\content\home -Language "en" -MaxRecentVersions 10

Can anybody please tell me why is new-usingblock used here with remove-itemversion? Is it necessary to use it everytime we are working with Remove-ItemVersion?

  • The command is run in the context of an event disabler. Typically any event subscription due to removing a version will not be fired. – Michael West Oct 22 '19 at 12:43

New-UsingBlock is analogous to the using block we use in C#.

As per the definition in Sitecore PowerShell Doc, The New-UsingBlock command disposes of the provided input object after the specified scriptblock completes execution. You may find the data returned by the scriptblock is contained within the following object System.Collections.ObjectModel.Collection1[[System.Management.Automation.PSObject, System.Management.Automation, Version=, Culture=neutral, PublicKeyToken=31bf3856ad364e35]]`. This helps when multiple objects are returned and is known behavior.

Like usingblock in C#, it is not mandatory to use New-UsingBlock but a best practice as it takes care of disposing objects that are no longer needed after the script is executed. Generally, this is very useful when dealing with bulk operations where lot of objects are created at runtime.

| improve this answer | |

The New-UsingBlock command disposes of the provided input object after the specified scriptblock completes execution. So if the type implements IDisposable, it automatically disposes it. It is similar to the using, let's understand it in simple c# way -

public class ADisposableClass : IDisposable
   //Some class implementation

In the above class, we want to call the dispose() every time, even if the code throws an exception -

ADisposableClass aDisposableClass = new ADisposableClass ();
  aDisposableClass .Action();
  if (aDisposableClass != null)
    aDisposableClass .Dispose();

and another method is to use the using block -

using (ADisposableClass aDisposableClass = new ADisposableClass ()) {

In your piece of code- New-UsingBlock(New-Object Sitecore.Data.Events.EventDisabler), EventDisabler is being called during Remove-ItemVersion. A script execution triggers some events in the background and if you don't want to trigger those events then you can simply use the EventDisabler in using statement. once you wrap something with New-UsingBlock statement, everything that is inside should work in 'switched context', with all events disabled.

Most of your questions are related to Sitecore Powershell Extension, I will suggest you, walk through the Sitecore PowerShell Extensions (SPE) module documents- https://doc.sitecorepowershell.com

| improve this answer | |
  • Hi, I have read SPE documents. But some of my doubts that doesn't get clear, those I post here. – NewbieHere Oct 22 '19 at 12:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.