1

On Sharepoint Online, using powershell & CSOM, I am trying to move files and their metadata from an existing doclib into folders, either in the same doclib, or another doclib.

The doclib and folder names are based on values contained in custom fields within the library items' metadata.

I am having trouble extracting the values from the two custom fields that specify the doclib and folder name.

The destination doclib name is based on a substring within a text field named "ccProgram", the folder name is based on the Year contained within a DateTime field named "ReleaseDate".

EDIT: There is a site Content Type that is used for all doclib items called 'SEDA Document'. I loaded this, as well, but no change in behavior...

My (Updated) code looks like this:

# Get Client Context
$context = Get-SPOContext
$context.ExecuteQuery()

#Get Web
$web = $context.Web
$context.Load($web)
$context.ExecuteQuery()

#Get List
$list = $web.Lists.GetByTitle('SEDA Library')
$context.Load($list)
$context.ExecuteQuery();

#Get Content Type
$contentType = $web.ContentTypes.GetById('0x01010090D18E42E6E85A4BB529BD76BA94334F')
$context.Load($contentType)
$context.ExecuteQuery();

#Get List Items (# of items)
$query = [Microsoft.SharePoint.Client.CamlQuery]::CreateAllItemsQuery(10)
$items = $list.GetItems($query)
$context.Load($items)
$context.ExecuteQuery();

Foreach ($item in $items)
{
    Write-Host $([string]::Format("{0}: {1}, {2}, {3}", $($item["ID"]), $($item["FileLeafRef"]), $($item["ccProgram"]), $($item["ReleaseDate"])))
}

I am able to extract the common fields "ID" and "FileLeafRef" without issue, but the :"ccProgram" and ReleaseDate" fields return no values at all.

My output looks like this:

1: ad3402230000.pdf, , 
2: ad3402230100.pdf, , 
3: ad3402230200.pdf, , 
4: ad3402230300.pdf, , 
5: ad3402240100.pdf, , 
6: ad3402250000.pdf, , 
7: ad3402250100.pdf, , 
8: ad3402260000.pdf, , 
9: ad3402260100.pdf, , 
10: ad3402270200.pdf, ,

There are no errors displaying...

How do I access these custom fields, and how do I extract the Year from the DateTime field?

Thx!

4
  • 1
    You're sure ccProgram and ReleaseDate are the internal names of your fields?
    – Erin L
    Aug 18, 2016 at 23:10
  • Also, are they single line of text fields? You might need a .tostring(), e.g., $item["ccProgram"].tostring()
    – Erin L
    Aug 18, 2016 at 23:13
  • Please make sure internal name of column are correct. Aug 19, 2016 at 10:06
  • Thx @Erin, Rahul, Yes, 'ccProgram' and 'ReleaseDate' are the internal names of these fields, and they are both single-line, though ReleaseDate is a DateTime field. Adding .ToString() throws this error: You cannot call a method on a null-valued expression. I tried $context.Load($file) and ($file.ListItemAllFields), to see if these attributes were contained there, but this did not change the return values... Aug 19, 2016 at 14:33

2 Answers 2

0

I suspect the problem is with your CAML query. Instead of using "CreateAllItemsQuery()", I think you should make your own CAML query specifying all of the fields that you need to retrieve.

I suggest you check out CAML Query in Powershell Issues, and use CAML Builder or a similar tool to build the CAML query you want, making sure you include all desired fields. Once the query works correctly in CAML Builder, use this query in your call to GetItems()

0

Turns out I was using an old document library that didn't have values in all of the required fields.

Specifically, queries for $item["ccProgram"] and $item["ReleaseDate"].Year were successful, as long as those values were included in the target library items.

Lesson learned: Inspect your test data before developing code that depends on it...

1
  • Dear Barry, is this considered as an answer ? I am confused ! if it's an answer please add more details about it , thanks in advance. Aug 22, 2016 at 17:12

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.