Obtener/establecer valores de campos de búsqueda en la lista de SharePoint usando PowerShell
Aquí están mis fragmentos de código de PowerShell para leer/escribir valores de campos de búsqueda en listas de SharePoint.
Escenario: Considere que tiene una lista principal llamada "Proyectos principales" y una lista secundaria "Hitos del proyecto". El campo "Nombre del proyecto principal" de la lista secundaria se busca desde el campo "Nombre del proyecto" de la lista principal.
Obtener el valor del campo de búsqueda de la lista de SharePoint usando PowerShell:
Add-PSSnapin Microsoft.SharePoint.Powershell -ErrorAction SilentlyContinue
#Variables
$WebURL="https://projects.crescent.com"
$ListName="Project Milestones"
$LookupFieldName="Parent Project"
#Get Web and List objects
$Web = Get-SPWeb $WebURL
$List = $Web.Lists.tryGetList($ListName)
if($List -ne $null)
{
#Get a Row from list
$Item = $List.Items[0]
#Get Lookup Field Value
$Lookup = New-Object Microsoft.SharePoint.SPFieldLookupValue($Item[$LookupFieldName])
#Send the output to screen
write-host $Lookup.LookupValue
}
Leer desde el valor del campo de búsqueda de valores múltiples:
Add-PSSnapin Microsoft.SharePoint.Powershell -ErrorAction SilentlyContinue
#Variables
$WebURL="https://projects.crescent.com"
$ListName="Project Milestones"
$LookupFieldName="Parent Project"
#Get Web and List objects
$Web = Get-SPWeb $WebURL
$List = $Web.Lists.tryGetList($ListName)
if($List -ne $null)
{
#Get a Row from list
$Item = $List.Items[0]
#Get Lookup Field Value
$LookupValueCollection = New-Object Microsoft.SharePoint.SPFieldLookupValueCollection($Item[$LookupFieldName].ToString())
Foreach ($LookupValue in $LookupValueCollection)
{
write-host $LookupValue.LookupValue
}
}
¿Cómo actualizar la columna de búsqueda en SharePoint usando PowerShell?
Para actualizar una columna de búsqueda, simplemente puede usar: $ListItem[LookupFieldName]=”Parent-Item-ID;#LookupValue”
Por ejemplo. Supongamos que tiene una columna de búsqueda llamada "Nombre del proyecto principal" en la lista "Métricas del proyecto", que hace referencia al campo "Nombre del proyecto" de la lista "Proyectos principales" como en la imagen de arriba. Ahora, para configurar la columna de búsqueda, puede usar:
$ProjectMetrics["ParentProjectName"]="2;#Cloud Devlopment"
Donde: 2 es el ID de la fila en la lista de Proyectos principales donde el nombre del proyecto es: Desarrollo en la nube.
PowerShell para configurar el campo de búsqueda en SharePoint:
Aquí se explica cómo actualizar el campo de búsqueda en SharePoint usando PowerShell
Add-PSSnapin Microsoft.SharePoint.Powershell -ErrorAction SilentlyContinue
#Variables
$WebURL="https://projects.crescent.com"
$ParentListName="Parent Projects" #Lookup Parent List
$ChildListName="Project Milestones" #List to add new lookup value
$ParentListLookupField="Project Name"
$ChildListLookupField ="Parent Project"
$ParentListLookupValue="Cloud Development" #Parent Project value
#Get Web and List objects
$Web = Get-SPWeb $WebURL
$ParentList = $Web.Lists.tryGetList($ParentListName)
$ChildList = $Web.Lists.tryGetList($ChildListName)
#Add new item in Child List - Project Milestones
$NewItem=$ChildList.Items.Add()
#Set Title field for the new list item
$NewItem["Title"]="Dec 2014 Milestone"
#Get Lookup Item from Parent List
$ParentListLookupItem = $ParentList.Items | where {$_[$ParentListLookupField] -eq $ParentListLookupValue}
#Set Lookup Field Value in Child List
$NewItem[$ChildListLookupField] = $ParentListLookupItem.ID
$NewItem.Update()
#Send the output to screen
write-host "Lookup Field value has been updated!"
Actualizar la columna de búsqueda de valores múltiples usando PowerShell:
¿Qué tal actualizar el campo de búsqueda con “Permitir valores múltiples” habilitado?
Add-PSSnapin Microsoft.SharePoint.Powershell -ErrorAction SilentlyContinue
#Variables
$WebURL="https://projects.crescent.com"
#Get Web and List objects
$Web = Get-SPWeb $WebURL
$ParentProjects = $Web.Lists.tryGetList("Parent Projects")
$ProjectMileStones = $Web.Lists.tryGetList("Project Milestones")
#Add new item in Child List - Project Milestones
$NewMilestone=$ProjectMileStones.Items.Add()
#Set Title field for the new list item
$NewMilestone["Title"]="Dec 2014 Milestone"
#Object for MultiValue Lookup Field Values
$LookupValues = New-Object Microsoft.SharePoint.SPFieldLookupValueCollection
#Get a Lookup Item from Parent List
$ParentProjectItem = $ParentProjects.Items | where {$_["Project Name"] -eq "Cloud Development"}
$LookupValue = New-Object Microsoft.SharePoint.SPFieldLookupValue
$LookupValue.LookupId = $ParentProjectItem.ID
$LookupValues.Add($LookupValue) #Add to Collection
#Get an another Lookup Item from Parent List
$ParentProjectItem = $ParentProjects.Items | where {$_["Project Name"] -eq "Smart Home"}
$LookupValue = New-Object Microsoft.SharePoint.SPFieldLookupValue
$LookupValue.LookupId = $ParentProjectItem.ID
$LookupValues.Add($LookupValue) #Add to Collection
#Set multi-value lookup field value
$NewMilestone["Parent Project"] = $LookupValues
$NewMilestone.Update()
Para obtener o configurar valores de la columna de búsqueda en SharePoint Online, use mi otra publicación: PowerShell para recuperar/actualizar el valor de la columna de búsqueda en SharePoint Online

