This SharePoint tutorial explains, how to solve the error: The property or field Url has not been initialized. It has not been requested or the request has not been executed. It may need to be explicitly requested. The error comes while working with SharePoint CSOM (Client Side Object Model).
Recently I was working with csom (.net managed object model) code for SharePoint Online sites. There is a requirement to retrieve web Id of the SharePoint site. So we have developed a console application that was communicating to SharePoint online sites.
We got an error message which says:
An unhandled exception of type ‘Microsoft.SharePoint.Client.PropertyOrFieldNotInitializedException’ occurred in Microsoft.SharePoint.Client.Runtime.dll
Additional information: The property or field ‘Url’ has not been initialized. It has not been requested or the request has not been executed. It may need to be explicitly requested.
It looks like below:
In the code, I was writing below to retrieve the website details like ServerRelativeUrl and RootWeb details.
using (ClientContext context = new ClientContext(parentURL))
{
context.AuthenticationMode = ClientAuthenticationMode.Default;
context.Credentials = new SharePointOnlineCredentials(GetSPOAccountName(), GetSPOSecureStringPassword());
context.Load(context.Web, w => w.ServerRelativeUrl);
context.Load(context.Site.RootWeb, w => w.Id, w => w.Url);
context.Load(context.Web, w => w.Id);
string rootWebURL = context.Site.RootWeb.Url;
}
When I execute the above code the above exception comes in the below line:
string rootWebURL = context.Site.RootWeb.Url;
The property or field ‘Url’ has not been initialized. It has not been requested or the request has not been executed. It may need to be explicitly requested
The mistake I was doing was not to calling the ExecuteQuery() method after loading the properties.
context.ExecuteQuery();
Below is the full code:
using (ClientContext context = new ClientContext(parentURL))
{
context.AuthenticationMode = ClientAuthenticationMode.Default;
context.Credentials = new SharePointOnlineCredentials(GetSPOAccountName(), GetSPOSecureStringPassword());
context.Load(context.Web, w => w.ServerRelativeUrl);
context.Load(context.Site.RootWeb, w => w.Id, w => w.Url);
context.Load(context.Web, w => w.Id);
context.ExecuteQuery();
string rootWebURL = context.Site.RootWeb.Url;
}
You may like the following SharePoint csom tutorials:
- Copy list items from one site collection to another programmatically using CSOM in SharePoint Online
- SharePoint online Read CSV file from document library using .Net managed client object model csom
- Getting Started with PnP PowerShell – SharePoint 2013/2016/SharePoint Online
- SharePoint create folder programmatically using CSOM
- How to get access token in SharePoint Online using CSOM and use in Postman or Google Rest client
- How to activate SharePoint Publishing Feature Programmatically using CSOM and PowerShell
- Get all lists and document libraries programmatically using CSOM in SharePoint Online
- Get SharePoint List Name and GUID using PowerShell and CSOM
I hope this will be helpful to resolve The property or field Url has not been initialized. It has not been requested or the request has not been executed. It may need to be an explicitly requested error in SharePoint.
Bhawana Rathore is a Microsoft MVP (3 times in Office Apps & Services) and a passionate SharePoint Consultant, having around 10 years of IT experience in the industry, as well as in .Net technologies. She likes to share her technical expertise in EnjoySharePoint.com and SPGuides.com