[Solved] 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 error in SharePoint 2013/2016/Online

SharePoint 2013 The property or field Url has not been initialized SharePoint online
SharePoint deveopment training course

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 which 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:

The property or field Url has not been initialized SharePoint online
SharePoint 2013 The property or field Url has not been initialized SharePoint online

In the code I was writting below to retrieve the web site 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 following SharePoint csom tutorials:

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 explicitly requested error in SharePoint.

Check out Best Alternative to InfoPath -> Try Now

free sharepoint training

SharePoint Online FREE Training

JOIN a FREE SharePoint Video Course (3 Part Video Series)

envelope
envelope

About Bhawana Rathore

Hello Everyone!! I am Bhawana a SharePoint MVP and having about 8+ years of SharePoint experience as well as in .Net technologies . I have worked in all the versions of SharePoint from wss to Office 365. I have good exposure in Customization and Migration using Nintex, Metalogix tools . Now exploring more in SharePoint 2016 :) Hope here I can contribute and share my knowledge to the fullest. As I believe "There is no wealth like knowledge, and no poverty like ignorance"

View all posts by Bhawana Rathore →