Code Monkey home page Code Monkey logo

Comments (11)

koltyakov avatar koltyakov commented on June 11, 2024 2

Hi @jimmywim,

Not an issue to add this enhancement. Actually, I already did it in a local branch and in a process of testing before PR.

Thanks for enhancement suggestion offer!

from pnpjs.

patrick-rodgers avatar patrick-rodgers commented on June 11, 2024 1

Sorry, @jimmywim - did you mean "now" working or "not" working?

If there is no special functionality on that endpoint I am not sure we need to explicitly extend the Web object to include it. However if there is something we can support that will help, please let us know.

What about something like getParentWeb(): Promise<Web>?

from pnpjs.

jimmywim avatar jimmywim commented on June 11, 2024 1

Hi Patrick, I do mean "now" working - the same I pasted above works.

getParentWeb(): Promise<Web> is something that I feel would be useful, and the implementation would appear to simply be the two lines:

const response = await web.select('ParentWeb').expand('ParentWeb').get();
return sp.site.openWebById(response.ParentWeb.Id);

from pnpjs.

koltyakov avatar koltyakov commented on June 11, 2024

After some tests:

ParentWeb endpoint is not the same as having an instance of the web.

And my initial idea of adding:

    /**
     * Gets this web's parent web
     *
     */
    public get parentWeb(): Web {
        return this.clone(Web, `parentWeb`);
    }

to Web class failed on deep testing.

Let me elaborate a bit on this.

My idea was to add sp.web.parentWeb returning an instance of Web. But parentWeb is just a set of properties and can't be chained with other usual endpoints. I.e. sp.web.parentWeb.lists will fail, sp.web.parentWeb.select('Url') won't return the prop.

In existing code base we can actually deal with parentWeb in the following way:

pnp.sp.web.select('ParentWeb').expand('ParentWeb').get().then(console.log);

So... I'm not sure, do we need parentWeb as an additional get method? Any thoughts?

from pnpjs.

jimmywim avatar jimmywim commented on June 11, 2024

That method appears to get some data about the parent Web but it's not of type Web in the code base, so doesn't have all the methods & properties on it (which is useful, and what I currently need as I'm in a recursive method trying to pull out Publishing's available page layouts - agreed that's not something to deal within Modern sites but the ability to get the full Web object for the parent might still be useful).

The object that comes back does have Id though, so we can use Site.openWebById() on it.

from pnpjs.

koltyakov avatar koltyakov commented on June 11, 2024

A web object can be constructed using const web = new Web([web absolute url]).
In most situations, I would use existing web URL and pop last path part out and build parent web object with full API support.

Or, as you said, site.openWebById is the case too:

(async () => {

  const { ParentWeb } = await pnp.sp.web.select('ParentWeb/Id').expand('ParentWeb').get();
  const { web: parentWeb } = await pnp.sp.site.openWebById(ParentWeb.Id);
  
  const lists = await parentWeb.lists.get();
  console.log(lists);

})()
  .catch(console.error)

from pnpjs.

jimmywim avatar jimmywim commented on June 11, 2024

Almost.

This is a now-working example I have for recursing webs (looking for a property bag value that isn't "__inherits":

async recurseWeb(web: Web): Promise<Web> {
    const bagOValues = await web.allProperties.get();
    if (bagOValues[SitePropertyBagFields.AVAILABLE_PAGE_LAYOUTS]) {
        const layoutsXml = bagOValues[SitePropertyBagFields.AVAILABLE_PAGE_LAYOUTS];
        if (layoutsXml === "__inherit") {
            const response = await web.select('ParentWeb').expand('ParentWeb').get();
            const parentWeb = await sp.site.openWebById(response.ParentWeb.Id);
            return this.recurseWeb(parentWeb.web);
        } else {
            // ... parse the xml and pull out the necessary data
        }
    }
}

from pnpjs.

koltyakov avatar koltyakov commented on June 11, 2024

Maybe not for merging, but for a review of the helper. Please check the PR - the implementation of getParentWeb(): Promise<{ data: any; web: Web; }>. As openWebById returns Web object together with the data object, I keen on keeping both.

const { data, web: parentWeb } = await web.getParentWeb();

What do you think would it be helpful?

from pnpjs.

jimmywim avatar jimmywim commented on June 11, 2024

Yep, I think that'd be perfect.

from pnpjs.

patrick-rodgers avatar patrick-rodgers commented on June 11, 2024

Looks like this was merged, so closing issue. Thanks!

from pnpjs.

sun2silicon avatar sun2silicon commented on June 11, 2024

I finally got the lists from parent web by
const results = await sp.site.rootWeb.lists.getByTitle("mylist")...

Full example:

const results = await sp.site.rootWeb.lists.getByTitle("mylist").getItemsByCAMLQuery({
        ViewXml: `<View><Query>
                    <Where>
                        <And>
                            <And>
                                <Contains>
                                <FieldRef Name='${"Metadata_x0020_Navigation"}' />
                                <Value Type='TaxonomyFieldType'>Lease Agreements</Value>
                                </Contains>
                                <Contains>
                                <FieldRef Name='Metadata_x0020_Navigation' />
                                <Value Type='TaxonomyFieldType'>Graphics</Value>
                                </Contains>
                            </And>
                            <Eq>
                                <FieldRef Name='FSObjType' />
                                <Value Type='Integer'>0</Value>
                            </Eq>
                        </And>
                    </Where>
                </Query></View>`,
    });

from pnpjs.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.