1

Can anyone show me how can I get the lookup field value?

I used this line of code but it is throwing an error:

#$Lookup = new-object Microsoft.SharePoint.SPFieldLookupValue($listItem["Business_x0020_Unit"])

new-object : Cannot find type [Microsoft.SharePoint.SPFieldLookupValue]: make sure the assembly containing this type is loaded.

Below is my code.

 $loc = "C:\Scripts\ReadListUsingCSOM\dlls" # Location of DLL's
 $siteUrl = "https://collaborate.com/"

$listName = "Project"

Set-Location $loc

Add-Type -Path (Resolve-Path "Microsoft.SharePoint.Client.dll")
Add-Type -Path (Resolve-Path "Microsoft.SharePoint.Client.Runtime.dll")

#Write-Host "Please enter password for $($siteUrl):"
#$pwd = Read-Host -AsSecureString
$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteUrl)
# Remove or replace line below to change authentication method
#$ctx.Credentials = New-Object System.Net.NetworkCredential($loginname, $pwd)

$web = $ctx.Web 
$ctx.Load($web) 
$ctx.ExecuteQuery()


$list = $ctx.Web.Lists.GetByTitle($listName)
$caml = new-object Microsoft.SharePoint.Client.CamlQuery
$caml.ViewXML = "<View />"

$listItems = $list.GetItems($caml)
$ctx.Load($listItems);
$ctx.ExecuteQuery();

Write-Host " Current web title is '$($web.Title)', $($web.Url)"
Write-Host $listItems.Count
foreach ($listItem in $listItems) 
{ 
    #We have all the list item data. For example, Title. 
$Lookup = new-object Microsoft.SharePoint.SPFieldLookupValue($listItem["Business_x0020_Unit"])
[Microsoft.SharePoint.SPFieldLookupValue]$listItem["Business_x0020_Unit"]; 
    Write-Host $listItem.ID $listItem["Project_x0020_Full_x0020_Number"] 
} 

2 Answers 2

1

You are using CSOM in your example but the SSOM Microsoft.SharePoint.SPFieldLookupValue type is specified for a Lookup Field.

The line:

$Lookup = new-object Microsoft.SharePoint.SPFieldLookupValue($listItem["Business_x0020_Unit"])

have to be replaced with this one:

$businessUnit = [Microsoft.SharePoint.Client.FieldLookupValue]$listItem["Business_x0020_Unit"]

P.S.

How to get the type of a Client Object:

$type = $item["Business_x0020_Unit"].GetType()
2
  • I am getting the following error: Cannot index into a null array. At C:\Scripts\ReadListUsingCSOM\ReadList2.ps1:48 char:1 + $businessUnit = [Microsoft.SharePoint.Client.FieldLookupValue]$item["Business_x0 ...
    – Imir Hoxha
    Commented May 13, 2014 at 9:15
  • There is a typo, this error occurs since $item variable is not initialized. Please replace $item with $listItem Commented May 13, 2014 at 9:23
0

The class you are trying to use is not a client class (not originating from Microsoft.SharePoint.Client).

What you are looking for is Microsoft.SharePoint.Client.FieldLookupValue

1
  • Yes, I changed it but it is not working. Can you give a full example?
    – Imir Hoxha
    Commented May 13, 2014 at 9:25

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.