0

I am trying to create a Windows Form for populating the information from SharePoint List into Windows Form. However, when I am trying to fetch the item from the list, it throws me an error message

An unhandled exception of type 'Microsoft.SharePoint.Client.PropertyOrFieldNotInitializedException' occurred in Microsoft.SharePoint.Client.dll Additional information: 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."

Here is the code for the same.

private void btn_populatecontainer_Click(object sender, EventArgs e)
        {
            var authManager = new AuthenticationManager();
            ClientContext overviewlistClientcontext = authManager.GetWebLoginClientContext("Tenant URL");
            var tenant = new Tenant(overviewlistClientcontext);
            var siteProperties = tenant.GetSiteByUrl("SiteCollectionURL");
            Web web = siteProperties.RootWeb;
            List olist = web.Lists.GetByTitle("SiteOverviewList");
            overviewlistClientcontext.Load(siteProperties);
            overviewlistClientcontext.ExecuteQuery();

            ListItemCollectionPosition position = null;
            var page = 1;
            do
            {
                CamlQuery camlQuery = new CamlQuery();
                camlQuery.ViewXml = @"<View Scope='Recursive'><RowLimit>4000</RowLimit><Query><Where><Eq><FieldRef Name='Site URL'/><Value Type='Text'>" + txtSite.Text + "</Value></Eq></Where></Query></View>";
                camlQuery.ListItemCollectionPosition = position;
                ListItemCollection listItems = olist.GetItems(camlQuery);
                overviewlistClientcontext.Load(listItems);
                overviewlistClientcontext.ExecuteQuery();
                position = listItems.ListItemCollectionPosition;
                DataTable dataTable = new DataTable();
                DataRow dr;
                DataColumn dc = new DataColumn("Site Owner");
                dc.DataType = Type.GetType("System.String");
                dataTable.Columns.Add(dc);
                foreach (var listItem in listItems)
                {
                    dr = dataTable.NewRow();
                    dr["Site Owner"] = listItem["Site Owner"];
                    dataTable.Rows.Add(dr);
                }
                OverViewListGrid.DataSource = dataTable;
                OverViewListGrid.DataBindings
                page++;*/
            }
            while (position != null);
}
    }
 }

I have debugged the code and it fails when it tries to execute query after fetching from Caml Query.

  1. When CamlQuery gets executed, it doesn't work at all.
  2. However, for the testing purpose, I have tried to fetch other columns and the code works if I try to fetch Title/Id Column directly using inbuilt functions (i.e. GetItembyID).
  3. But even the second steps won't work if i use another column from the same list.

Can anyone help me with this?

David Buck
  • 3,752
  • 35
  • 31
  • 35

2 Answers2

0

To filter list items from list which over threshold, need use indexed column.

The Site URL need to be indexed column.

<Query><Where><Eq><FieldRef Name='Site URL'/><Value Type='Text'>" + txtSite.Text + "</Value></Eq></Where></Query>

Otherwise, you need get all items from SharePoint by pagination.

CamlQuery camlQuery = new CamlQuery();
camlQuery.ViewXml = @"<View Scope='Recursive'><RowLimit>4000</RowLimit><Query></Query></View>";

Then, filter by Linq.

How I can filter a dataTable with Linq to datatable?

Lee
  • 5,305
  • 1
  • 6
  • 12
0

the exception says that You are trying to get value of a column that has not been initialized in the context. In order to get it You need to load it to the client context together with the list item collection. The simplest way to get all fields is to use 'FieldValuesAsText'. For Your code example the updated code would look like


.....
    overviewlistClientcontext.Load(listItems, includes => includes.Include(i => i.FieldValuesAsText));
    overviewlistClientcontext.ExecuteQuery();
    position = listItems.ListItemCollectionPosition;
    DataTable dataTable = new DataTable();
    DataRow dr;
    DataColumn dc = new DataColumn("Site Owner");
    dc.DataType = Type.GetType("System.String");
    dataTable.Columns.Add(dc);
    foreach (var listItem in listItems)
    {
          dr = dataTable.NewRow();
          dr["Site Owner"] = listItem.FieldValuesAsText["Site Owner"];
          dataTable.Rows.Add(dr);
    }
.....

Also please be aware that You should use internal name (not display name) of column to get the field value. So I suspect that the internal name for You column 'Site Owner' would be 'Site_x0020_Owner' so You should take it like:



      dr["Site Owner"] = listItem.FieldValuesAsText["Site_x0020_Owner"];


In order to check what is the internal name of Your column, the easiest way is to enter edit page of Your column and the internal name will be part of the URL as one of the GET parameters enter image description here

Adam
  • 810
  • 7
  • 10