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

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()
  • 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. – kohila vani Jul 5 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 – kohila vani Jul 5 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 Jul 5 at 14:03

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.