1

I'm accessing a SharePoint Online document library using the following:

## <redacted boilerplace> ##
# Get the Client Context and Bind the Site Collection
$context = New-Object Microsoft.SharePoint.Client.ClientContext($siteroot + $sitesubsite)

# Authenticate
$credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($AdminUsername, $AdminPassword)
$context.Credentials = $credentials

# Lookup Source Address 
$rootWeb = $Context.Web
$List = $rootWeb.lists.getByTitle($ListName)
$fields = $List.Fields;
$ListItems = $List.GetItems([Microsoft.SharePoint.Client.CamlQuery]::CreateAllItemsQuery())
# Load the List
$Context.Load($rootWeb)
$Context.Load($List)
$Context.Load($ListItems)
$Context.Load($fields)
$Context.ExecuteQuery()

# create a hastable of listdata for casting into JSON
$listArray = New-Object System.Collections.Generic.List[System.Object]
foreach ($item in $ListItems)
{   
    $listArray.Add([hashtable]@{
        Filename = $item["Name"];
        DisplayTitle=$item["Title"].ToString(); 
        Settings= $item["Setting"]; 
        Description= $item["File_x0020_Description"];
        HealthAreas= $item["Health_x0020_Area"]; 
        ResourceType= $item["Resource_x0020_Type"]; 
        Active= $item["Currently_x0020_active_x003f_"];     
        ExternalURL= $item["External_x0020_file_x0020_path"]; 
    })
}

When I use Write-Host to print the results to the screen the Filename returns as null.

I've also attempted to use $item.AttachmentFiles.Path; and $item.File.LinkingUrl however both return null results.

I thought potentially this could be stored in AttachmentFiles (even though document libraries are a 1:1 relationship with files) and did $item.AttachmentFiles.Count; which also returned a null value.

Searching online indicates that I should use item.Name for this however that isn't available within my object and the resources don't appear to address SharePoint Online (most are from on-premise 2010).

I'm sure it's something simple I'm missing, but what element should I be using to access the filename for the files within my SharePoint Online document library?

  • Try it as Filename = $item["FileLeafRef"]; – Gautam Sheth Feb 22 '17 at 6:26
1

You should be using Filename = $item["FileLeafRef"]; (documentation)

Try it as below:

$listArray.Add([hashtable]@{
        Filename = $item["FileLeafRef"];
        DisplayTitle=$item["Title"].ToString(); 
        Settings= $item["Setting"]; 
        Description= $item["File_x0020_Description"];
        HealthAreas= $item["Health_x0020_Area"]; 
        ResourceType= $item["Resource_x0020_Type"]; 
        Active= $item["Currently_x0020_active_x003f_"];     
        ExternalURL= $item["External_x0020_file_x0020_path"]; 
    })

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.