Question

I'm trying to retrieve a relative url from a SP.Web I get my sp.web from a SP.list with

SPWeb test = listNL.ParentWeb;

When I try to get the url I got an exception:

test.ServerRelativeUrl' threw an exception of type 'Microsoft.SharePoint.Client.PropertyOrFieldNotInitializedException

test.ServerRelativeUrl;

Do you know a solution to retrieve the relative url from the list?

Was it helpful?

Solution

This error occurs since ServerRelativeUrl property of Web object has not been loaded.

You have to explicitly specify to load ServerRelativeUrl property:

var list = ctx.Web.Lists.GetByTitle(listTitle);
ctx.Load(list,l => l.ParentWeb.ServerRelativeUrl);
ctx.ExecuteQuery(); 
var webUrl = list.ParentWeb.ServerRelativeUrl;

or use List ParentWebUrl property:

var list = ctx.Web.Lists.GetByTitle(listTitle);
ctx.Load(list);
ctx.ExecuteQuery();  
var webUrl = list.ParentWebUrl;

to retrieve Web ServerRelativeUrl property.

How to retrieve List server-relative Url via CSOM

var list = ctx.Web.Lists.GetByTitle(listTitle);
ctx.Load(list,l => l.RootFolder.ServerRelativeUrl);
ctx.ExecuteQuery(); 
var listUrl = list.RootFolder.ServerRelativeUrl;
Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top