Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PnP.Core.ClientException: Specify the GraphGet/GraphGetLinq field of the ClassMapping property #631

Closed
1 task done
SPDEVGUY opened this issue Nov 16, 2021 · 4 comments
Closed
1 task done
Assignees
Labels
question Further information is requested

Comments

@SPDEVGUY
Copy link

SPDEVGUY commented Nov 16, 2021

Category

  • Bug / Question?

Describe the bug

Unhelpful exception when querying sub-webs:
PnP.Core.ClientException: Specify the GraphGet/GraphGetLinq field of the ClassMapping property

Attempting to follow documentation to enumerate sub-webs:
https://pnp.github.io/pnpcore/using-the-sdk/webs-intro.html#getting-sub-webs

Error thrown:
image

Source code:
image

Essential bits:
`
var url = "https://contoso.sharepoint.com";

var context = await Pnp.CreateAsync(new Uri(url));

log.Log($"Loaded context for {ctx.Site.Id}");

var site = ctx.Site;

await site.GetAsync(p => p.RootWeb);

var rw = site.RootWeb;

await rw.GetAsync(p => p.Webs);

foreach (var w in rw.Webs) << Exception accessing rw.Webs property.
`

Steps to reproduce

Code above.
Can I not use PnPContext.Site.RootWeb.Webs with the foreach?

Expected behavior

When accessing Context.Site.RootWeb.Webs I expect to be able to enumerate the webs within that site after doing the GetAsync calls as per the documentation.

Throw informative exception or improve documentation?

Environment details (development & target environment)

  • SDK version: 1.4.0
  • OS: Win10
  • SDK used in: Console app, .NET Framework 4.7.2
  • Framework: .NET Framework 4.7.2
  • Browser(s): Chorme?
  • Tooling: VS2019
  • Additional details: None.

Additional context

Thanks for your contribution! Sharing is caring.

@jansenbe
Copy link
Contributor

jansenbe commented Nov 17, 2021

@SPDEVGUY : Once you've loaded data you need to use .AsRequested() to query the loaded data...in your case you're trying to query twice. The documentation was still missing AsRequested() thanks for spotting that.

So your code would be like this:

var url = "https://contoso.sharepoint.com";
var context = await Pnp.CreateAsync(new Uri(url));
log.Log($"Loaded context for {ctx.Site.Id}");
var site = ctx.Site;
await site.GetAsync(p => p.RootWeb);
var rw = site.RootWeb;
await rw.GetAsync(p => p.Webs);

foreach (var w in rw.Webs.AsRequested())
{
}

You could also optimize above code a little bit as rootweb is always loaded by default + possibly you can save a roundtrip by loading additional RootWeb properties during PnPContext initialization (see https://pnp.github.io/pnpcore/using-the-sdk/basics-context.html). Taking that in account below code will do 2 server roundtrips to achieve the same result:

var options = new PnPContextOptions()
{
    AdditionalWebPropertiesOnCreate =
        new Expression<Func<IWeb, object>>[]
        {
            w => w.Webs
        }
};

var url = "https://contoso.sharepoint.com";
var context = await Pnp.CreateAsync(new Uri(url), options);

log.Log($"Loaded context for {ctx.Site.Id}");

foreach (var w in context.Web.Webs.AsRequested())
{
  // Use the web
}

@jansenbe jansenbe self-assigned this Nov 17, 2021
@jansenbe jansenbe added the question Further information is requested label Nov 17, 2021
@s-KaiNet
Copy link
Collaborator

I think still we have an issue here, because below code throws an exception:

var webs = context.Web.Webs.ToList();

To my understanding, it shouldn't throw and should query subwebs instead.

@jansenbe
Copy link
Contributor

@s-KaiNet : Yes, I know...something to still be fixed. For @SPDEVGUY 's case however the provided code is a good alternative, but I do agree that this should be fixed to ensure a consist experience across the various models

jansenbe added a commit that referenced this issue Nov 17, 2021
@jansenbe
Copy link
Contributor

jansenbe commented Nov 17, 2021

@s-KaiNet , @SPDEVGUY : I've also pushed a fix that enables using LINQ (context.Web.Webs.ToList() or foreach (var w in rw.Webs) { } ). Think this issue can be closed now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants