0

I would like to update my document library but i do not know how to do with one of my field which is a lookup field. Here is my code:

#plug-in SP PS 
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#Variables
$WebURLp="http://myparentsite"
$WebURLc="http://myparentsite/mychildsite"

#Get Web and List objects
$Webp = Get-SPWeb $WebURLp
$Webc = Get-SPWeb $WebURLc
$ParentProducts = $Webp.Lists.tryGetList("PRODUCTS") 
$ChildRecipe = $Webc.Lists.tryGetList("RECIPE") 
$spContentType = $ChildRecipe .ContentTypes["New Recipe"]

#Search if Recip exist
$RecipName = "My FIRST RECIP"
$RecipDetail = "Cold Recip"
$RecipLetter = "R"
$DocSetName = "R320_" + $RecipName + $RecipLetter 

[Microsoft.SharePoint.SPFolder]$targetFolder = $Webc.GetFolder($ChildRecipe .RootFolder.ServerRelativeUrl + "/$DocSetName")
#Build properties hash table from $docSetInfos[]
[Hashtable]$docsetProperties = @{}

#CREATE A RECIP FOLDER
if (-not $targetFolder.Exists)
{

$RefRecip= "R320"
$RecipDetail = "Cold Recip"
##Build properties hash table from $docSetInfos[]
#[Hashtable]$docsetProperties = @{}
$docsetProperties.Add('v_ref',$RefRecip)
$docsetProperties.Add('v_name',$RecipName)
$docsetProperties.Add('v_detail',$RecipDetail )
$newDocumentSet =   [Microsoft.Office.DocumentManagement.DocumentSets.DocumentSet]::Create($targetFolder.ParentFolder,$DocSetName,$spContentType.Id,$docsetProperties)
}

#UPDATE RECIP FOLDER
$docsetProperties.Add('v_box',"BOXRECIP")
$docsetProperties.Add('v_cat',"208 - vegetable")
#LOOKUP
$LookupValues = New-Object Microsoft.SharePoint.SPFieldLookupValueCollection

#Get a Lookup Item from Parent List
$ParentProductsItem= $ParentProducts.Items | where {$_["Category"] -eq "208 - vegetable"}
$LookupValue = New-Object Microsoft.SharePoint.SPFieldLookupValue
$LookupValue.LookupId = $ParentProjectItem.ID
$LookupValues.Add($LookupValue) #Add to Collection

#MAJ Lookup
$docsetProperties.Add('v_cat_lookup',$LookupValues)
$targetFolder.Update()

Nothing happens, no code error but no update done :(

2 Answers 2

0

Use object type SPFieldLookupValue to update. A helpful docu might be this one here

1
  • Thx, I got it finaly :)
    – Dany
    Jun 29, 2018 at 12:28
0

I got it finally because i had 2 problems to solve :

  • my code was not appropriate in the Update part of my document-Set

  • I was not using the good function for the lookup value

Here is my correct code :

#plug-in SP PS 
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#Variables
$WebURLp="http://myparentsite"
$WebURLc="http://myparentsite/mychildsite"

#Get Web and List objects
$Webp = Get-SPWeb $WebURLp
$Webc = Get-SPWeb $WebURLc
$ParentProducts = $Webp.Lists.tryGetList("PRODUCTS") 
$ChildRecipe = $Webc.Lists.tryGetList("RECIPE") 
$spContentType = $ChildRecipe .ContentTypes["New Recipe"]

#Search if Recip exist
$RecipName = "My FIRST RECIP"
$RecipDetail = "Cold Recip"
$RecipLetter = "R"
$DocSetName = "R320_" + $RecipName + $RecipLetter 

[Microsoft.SharePoint.SPFolder]$targetFolder = $Webc.GetFolder($ChildRecipe .RootFolder.ServerRelativeUrl + "/$DocSetName")
#Build properties hash table from $docSetInfos[]
[Hashtable]$docsetProperties = @{}

#CREATE A RECIP FOLDER
if (-not $targetFolder.Exists)
{
$RefRecip= "R320"
$RecipDetail = "Cold Recip"
##Build properties hash table from $docSetInfos[]
#[Hashtable]$docsetProperties = @{}
$docsetProperties.Add('v_ref',$RefRecip)
$docsetProperties.Add('v_name',$RecipName)
$docsetProperties.Add('v_detail',$RecipDetail )
$newDocumentSet =   [Microsoft.Office.DocumentManagement.DocumentSets.DocumentSet]::Create($targetFolder.ParentFolder,$DocSetName,$spContentType.Id,$docsetProperties)
}
else
{
$newDocumentSet = [Microsoft.Office.DocumentManagement.DocumentSets.DocumentSet]::GetDocumentSet($targetFolder)
}

#UPDATE RECIP FOLDER
$item = $newDocumentSet.Item
$item['v_box'] = "BOXRECIP"
$item['v_cat'] ="208 - vegetable"

#LOOKUP
$LookupValue = New-Object Microsoft.SharePoint.SPFieldLookupValue

#Get a Lookup Item from Parent List
$ParentProductsItem= $ParentProducts.Items | where {$_["Category"] -eq "208 - vegetable"}
$LookupValue.LookupId = $ParentProjectItem.ID

#MAJ Lookup
$item['v_cat_lookup'] = $LookupValue
$item.Update()

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.