Microsoft.SharePoint.Client.PropertyOrFieldNotInitializedException: '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.'

Lakshmanan B 81 Reputation points
2024-08-15T08:49:02.92+00:00

Hi I am working with retrieving data from a SP list. I am able to connect to the list, get the list name and count the number of records (items) from the given list. Next, I wanted to get the data from a particular columns.

Say I have the following list:

Column A | Column B | Column C |
Val A | 55 | 70 |
Val A | 20 | 70 |
Val B | 55 | 70 |
Val A | 20 | 70 |

I want the retrieve the Column A values and I tried the following code and getting the error:

List sp_list = M_clientContext.Web.Lists.GetByTitle("testProgramCodes");
CamlQuery query = new CamlQuery();
query.ViewXml = "<View><Query><GroupBy Collapse='TRUE'><FieldRef Name='Column_x0020_A'/></GroupBy><OrderBy><FieldRef Name='Column_x0020_A' Ascending='TRUE'/></OrderBy></Query></View>"; ListItemCollection collListItem = sp_list.GetItems(query);

M_clientContext.Load(collListItem);
M_clientContext.ExecuteQuery();

if (collListItem.Count == 0)
{
    Console.WriteLine("No items found.");
    return null;
}
else
{
    string[] testProgramName = null;
    foreach(ListItem val in collListItem)
    {
        testProgramName.Append(val["Column A"]);
    }
    return testProgramName.Distinct().ToArray();
}

I am getting the error at "testProgramName.Append(val["Column A"]);"

stating:

Microsoft.SharePoint.Client.PropertyOrFieldNotInitializedException: '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.'

Where I went wrong? Can somebody help. TIA

.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,808 questions
Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,762 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,858 questions
SharePoint Development
SharePoint Development
SharePoint: A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.Development: The process of researching, productizing, and refining new or existing technologies.
2,948 questions
0 comments No comments
{count} votes

Accepted answer
  1. RaytheonXie_MSFT 35,066 Reputation points Microsoft Vendor
    2024-08-16T02:14:55.2566667+00:00

    Hi @Lakshmanan B,

    You need to load list item before loop collListItem. You could refer to following code

    M_clientContext.Load(collListItem, its => its.Include(val => val["Column A"]));
    M_clientContext.ExecuteQuery();
    

    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Vinay Shokeen 0 Reputation points
    2024-08-20T09:13:54.7233333+00:00

    To retrieve data from specific columns in a SharePoint list after successfully connecting to the list and counting records, you can use either REST API or Microsoft Graph API depending on your setup. Below is an example using JavaScript with the Fetch API for retrieving column values.

    Example Using REST API

    Assuming you’re working within a context that allows access (like SPFx), here’s how you might fetch data:

    const
    

    Explanation:

    • Replace yourtenant and yoursite with your actual tenant name and site URL.
    • Adjust $select= parameter based on which columns you’d like to retrieve (Val A, etc.).

    This code will log out values of “Column A” and "Column B."

    If you’re looking specifically for unique entries or filtering by certain criteria (e.g., only where “Value” equals Val A), consider adding query options such as $filter.

    For instance:

    .../items?$select=Title,Field1&$filter=Field1 eq 'Val A'
    

    Make sure you’ve set up proper permissions in Azure AD app registration settings when accessing via APIs.

    Feel free to adjust this template according to your programming environment!

    Certainly! In order to retrieve data from specific columns in a SharePoint list, you can use the REST API or Microsoft Graph API. The example provided uses JavaScript with the Fetch API and demonstrates how to fetch column values using the REST API.

    Here’s an explanation of each step:

    1. Define variables:
      • siteUrl: Replace this with your SharePoint site URL.
        • listName: Specify the name of your SharePoint list.
        1. Create an asynchronous function called getColumnData that will handle retrieving column data from the specified list.
        2. Use the Fetch API to make a GET request to retrieve items (records) from your SharePoint list:
          • Constructing the endpoint URL by combining ${siteUrl}/_api/web/lists/getbytitle('${listName}')/items?$select=ColumnA, ColumnB. This specifies which columns (ColumnA, ColumnB) should be included in response results.
          1. Set necessary headers for authentication and specify 'Accept': 'application/json; odata=verbose'.
          2. Check if there was any error during fetching items using .ok property on response object.

    6a.If no errors occurred, parse JSON response into variable named jsonResponse using .json() method on Response object returned by fetch() call.

    7a.Process retrieved items stored within jsonResponse.d.results array as per requirement: Iterate through each item inside it via forEach loop, Access desired column values like item.ColumnA and item.ColumnB Perform further processing/logic based on these values

    8.Call getColumnData() function at end or wherever appropriate point in code execution flow

    Remember that when working with APIs such as REST or Microsoft Graph, proper permissions need to be set up either through Azure AD app registration settings or other means depending upon requirements/authentication mechanism used for accessing those APIs.

    Feel free modify this template according to programming environment/language being utilized while ensuring compatibility between versions/APIs supported by respective platforms

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.