1

Using Sitecore PowerShell Extension (SPE).

New to SPE and I am trying to do this:

If the template id matches && the item is expired && the content type of "video" is missing = add the "video" value to content type to all matches.

Template Id to match = {9FB83FAB-8911-4A90-A9FD-1CD1C32AF433}

Expiration date is a sitecore content editor field called End Date and Time. It's used for our events to expire them.

Content Type is a sitecore content editor field under Taxonomy list called Content Type.

What I have so far from piecing stuff together... I am getting a lot of "Sitecore.Data.Fields.DateField : The term 'Sitecore.Data.Fields.DateField' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.":

$path = "/sitecore/A/B/Home/Topics/Video";
$presentTime = [datetime]::Now;
$video = "video";

write-host $presentTime; 

$items = Get-ChildItem -Path "master:" $path -Language 'en' -recurse

write-host $items; 

foreach ($item in $items) {
    $selectedItem = Get-Item -Path "master:" -ID $item.ID
  
    #Check the items has template Id {9FB83FAB-8911-4A90-A9FD-1CD1C32AF433} 
    if ($selectedItem.TemplateId -eq "{9FB83FAB-8911-4A90-A9FD-1CD1C32AF433}") {
        #Get the Date time value from Item
        Sitecore.Data.Fields.DateField dateTimeField = $selectedItem.Fields["End Date and Time"];

        write-host $dateTimeField.Value; 
     
        #Validating the datetime value is expired or not
        if (dateTimeField.Value -lt $presentTime) {
            if ($null -eq $selectedItem.Fields["Content Type"]) {
                $selectedItem.Editing.BeginEdit(); 
       
                #Assigning Video value to Content Type
                $selectedItem.Fields["Content Type"].Value = $video;  
    
                $selectedItem.Editing.EndEdit();    
            }
        }
    }
}

Hopefully that makes sense - could someone help me with this while I try to get up to speed with PShell and sitecore.

2
0

I have updated your Sitecore PowerShell Script as shown below

$path = "/sitecore/A/B/Home/Topics/Video";
$presentTime = [datetime]::Now;
$video = "video";

Write-Host $presentTime; 

$items = Get-ChildItem -Path "master:" $path -Language 'en' -recurse

Write-Host $items; 

foreach ($item in $items) {
    
    #$selectedItem = Get-Item -Path "master:" -ID $item.ID
  
    #Check the items has template Id {9FB83FAB-8911-4A90-A9FD-1CD1C32AF433} 
    if ($item.TemplateId -eq "{9FB83FAB-8911-4A90-A9FD-1CD1C32AF433}") {
        #Get the Date time value from Item
        [Sitecore.Data.Fields.DateField] $dateTimeField = $item.Fields["End Date and Time"];

        Write-Host $dateTimeField.Value; 
     
        #Validating the datetime value is expired or not
        if ($dateTimeField.Value -lt $presentTime) {
            if ($item.Fields["Content Type"].Value -eq $null) {
                $item.Editing.BeginEdit(); 
       
                #Assigning Video value to Content Type
                $item.Fields["Content Type"].Value = $video;  
    
                $item.Editing.EndEdit();    
            }
        }
    }
}

The following statement has been commented $selectedItem = Get-Item -Path "master:" -ID $item.ID because there is no need to fetch the same item again. The $item is already the item you are trying to fetch. So, no need to fetch it twice. Use the $item within the foreach.

Secondly, for the datetime, you should use [Sitecore.Data.Fields.DateField] with the square brackets so that PowerShell can interpret it. Additionally, you need to declare the variable dateTimeField as $dateTimeField.

Finally, you may have an issue in terms of updating the item due to security. That is, the user running the script may require access to update the item. You may use the following:

New-UsingBlock (New-Object Sitecore.SecurityModel.SecurityDisabler) {
    $item.Editing.BeginEdit(); 

    #Assigning Video value to Content Type
    $item.Fields["Content Type"].Value = $video;  

    $item.Editing.EndEdit(); 
}

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.