0

I have set a value in a General link field in a Sitecore item using PowerShell. Here is the PowerShell script.

$item.Editing.BeginEdit()                        

[Sitecore.Data.Fields.LinkField]$fieldValueLink = $Item.Fields[$templateField.DisplayName]

$fieldValueLink.Text = "Link"
$fieldValueLink.Title = "click on the link"
$fieldValueLink.LinkType = "external"
$fieldValueLink.Url = $itemRow.$fieldTrimmedValue

$item[$templateField.DisplayName] = $fieldValueLink.Value;

$item.Editing.EndEdit() | Out-Null      

Now I want to set values into other field types like DropLink, Multilist and DropList. For setting these field values I think I need to create Sitecore items using PowerShell and then assign them to the fields' values, But I don't know where to start and how to proceed.

Can anyone help me out with this?

2 Answers 2

2

To create a new item with Sitecore Powershell Extensions, use New-Item:

$newItemParentPath = "/sitecore/content/Home"
$newItemTemplate = "{76036F5E-CBCE-46D1-AF0A-4143F9B557AA}"

$newItem = New-Item -Path $newItemParentPath -Name "New Item Name" -ItemType $newItemTemplate

Now to assign that new item in Droplink field you need to assign ID of that item to the field like:

$item.Editing.BeginEdit()
$item["Droplink field name"] = $newItem.ID
$item.Editing.EndEdit()

and to assign that new item in Droplist field you need to assign Name of that item to the field like:

$item.Editing.BeginEdit()
$item["Droplist field name"] = $newItem.Name
$item.Editing.EndEdit()
3
  • If i want to add a datasource which has more items then, should i need to add give value for sort. Or please tell me how to add list of items to a droplink field. Commented Jul 5, 2019 at 13:57
  • I'm trying something like this. $item.Editing.BeginEdit() $allItems = Get-ChildItem -Path '/sitecore/content/ListManagerTask' -Recurse foreach($droplinkItem in $allItems){ $item[$templateField.DisplayName] = $droplinkItem.ID; } $item.Editing.EndEdit() | Out-Null Commented Jul 5, 2019 at 13:59
  • So you take some item ($item) and then you iterate after ListManagerTask and all its descendants and in every loop you assign ID of that descendant to the same field. So after your loop is done, it will be the last descendant assigned to $item[$templateField.DisplayName] field, right? Btw, don't use DisplayName here, use Name or ID.
    – Marek Musielak
    Commented Jul 5, 2019 at 14:03
0

Here's an example script that sets values for a DropLink, Multilist, and DropList field on a Sitecore

# Define the path to the item to be edited
$itemPath = "master:/sitecore/content/path/to/your/item"
$item = Get-Item -Path $itemPath

# Ensure the item is retrieved
if ($item -eq $null) {
    Write-Host "Item not found at path: $itemPath" -ForegroundColor Red
    return
}

# Define the linked items for DropLink, Multilist, and DropList fields
$dropLinkItemPath = "master:/sitecore/content/path/to/linked/item"
$dropLinkItem = Get-Item -Path $dropLinkItemPath

$multilistItemPaths = @(
    "master:/sitecore/content/path/to/linked/item1",
    "master:/sitecore/content/path/to/linked/item2"
)
$multilistItems = $multilistItemPaths | ForEach-Object { Get-Item -Path $_ }

$dropListItemPath = "master:/sitecore/content/path/to/linked/item"
$dropListItem = Get-Item -Path $dropListItemPath

# Begin editing the item
$item.Editing.BeginEdit()
try {
    # Set DropLink field value
    if ($dropLinkItem -ne $null) {
        $item["YourDropLinkFieldName"] = $dropLinkItem.ID.ToString()
    }

    # Set Multilist field value
    if ($multilistItems -ne $null -and $multilistItems.Count -gt 0) {
        $item["YourMultilistFieldName"] = [string]::Join('|', $multilistItems | ForEach-Object { $_.ID.ToString() })
    }

    # Set DropList field value
    if ($dropListItem -ne $null) {
        $item["YourDropListFieldName"] = $dropListItem.Name
    }
}
finally {
    $item.Editing.EndEdit()
    Write-Host "Item fields updated successfully." -ForegroundColor Green
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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