1

I have created 2 column in sharepoint list. One field called owner is person or group type column and the other one is website template using choice type column.

The question how to read the value owner and website template column above.

Here is the code so far:

listName ="CustomerList" 

Add-Type -Path "D:\libs\Powershell Office365\Libraries\Microsoft.SharePoint.Client.dll" 
Add-Type -Path "D:\libs\Powershell Office365\Libraries\Microsoft.SharePoint.Client.Runtime.dll" 
Add-Type -Path "D:\libs\Powershell Office365\Libraries\Microsoft.SharePoint.Client.Taxonomy.dll" 

# connect/authenticate to SharePoint Online and get ClientContext object.. 
$clientContext = New-Object Microsoft.SharePoint.Client.ClientContext($url) 
$credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $securePassword) 
$clientContext.Credentials = $credentials 



if (!$clientContext.ServerObjectIsNull.Value) 
{ 
    Write-Host "Connected to SharePoint Online site: '$Url'" -ForegroundColor Red 
} 

$web = $clientContext.Web
$clientContext.load($web)
$clientContext.ExecuteQuery()

$list = $clientContext.Web.Lists.GetByTitle($listName)

$camlQuery = new-object Microsoft.SharePoint.Client.CamlQuery;
$camlQuery.ViewXml = "<View>
<Query>
 <Where> 
      <Eq>   
       <FieldRef Name='Status' /> 
            <Value Type='Text'>Approve</Value>
         </Eq>
       </Where>
 </Query>
</View>"

$items = $list.GetItems( $camlQuery )
$clientContext.Load($list)
$clientContext.Load($items) 
#execute query 
$clientContext.ExecuteQuery()

Write-Host "Connecting to SharePoint Online, URL = $rootUrl"
try
{
   Connect-SPOService -Url $rootUrl -Credential $credential
   Write-Host "Successfully connected.." -ForegroundColor Green
}
catch
{
   Write-Error "Failed to connect to $url - check the credentials and URL!" $_
}

foreach($item in $items)
{
    $siteUrl  = "https://contoso.sharepoint.com/sites/"
    $title = $item["Title"]
    $owner = [Microsoft.SharePoint.Client.FieldUserValue]
    $owner = $item["Owner"]
    $siteTemplate =  $item.GetFormattedValue("Website Template")


    Write-host $title, $status, $owner, $siteTemplate


}
Disconnect-SPOService

Based on the code above, i unable to display Website Template and Owner column. Any feedbacks or help greatly appreciate.

Thank you

1

Try this to get person type field value:

$owner = new-object Microsoft.SharePoint.Client.FieldUserValue($item["Owner"]).LookupValue

The choice value should work using $item.GetFormattedValue("Website Template"). Make sure the field name is correct.

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.