Kirk Liemohn is a Principal Software Engineer at ThreeWill. He has over 20 years of software development experience with most of that time spent in software consulting.
I was having a problem where I was trying to migrate list content from SharePoint 2010 to SharePoint 2013 but a couple of fields were not making it over. I was using a migration tool that I like to use, but had to resort to connecting to both SharePoint 2010 and SharePoint 2013 using CSOM due to environment restrictions. I know there are limitations with CSOM and likely more with SharePoint 2010 than there are with SharePoint 2013, but I was still surprised the tool couldn’t get values for these two fields.
I thought I could get around this if I wrote the code myself so I gave it a shot. I wrote some PowerShell to first query for the list item IDs from the source list. The code uses a CAML query and only has the ID field in the ViewFields for the query. I then iterate across the results of the query. For each result I get the entire source item by ID as follows:
$sourceItem = $sourceList.GetItemById($sourceItemFromCAMLQuery.ID) $sourceCtx.Load($sourceItem) $sourceCtx.ExecuteQuery()
GetItemById is the key method above as I thought it would do a good job of getting all fields for that list item. However, for the two fields that gave me problems with the migration tool, it failed with my code. The line of code that gave me an error was:
$sourceValue = $sourceItem[$fieldName]
The exception thrown was of type PropertyOrFieldNotInitializedException. The exception message was:
The property or field has not been initialized. It has not been requested or the request has not been executed. It may need to be explicitly requested.
I immediately thought this might be a row ordinal issue based on problems it has caused me in the past. In retrospect I discovered that according to the schema XML for the fields, they are still in row ordinal 0 (which is good) so that didn’t explain my issue, but my fix was the same regardless.
To fix this problem I simply added these two problem fields to the ViewFields for my initial CAML query and subsequently get the values from the CAML query instead of the list item obtained using GetItemById. It added a little complexity since I had to pay attention to which field values I obtained from the CAML query and which I obtained from GetItemById, but it solved my problem! I hope it helps others as well.