Sp-dev-docs: Access dataVersion for component definitions from GetClientSideWebParts

Created on 22 Mar 2018  路  3Comments  路  Source: SharePoint/sp-dev-docs

Category

  • [X] Question
  • [ ] Typo
  • [ ] Bug
  • [ ] Additional article idea

Expected or Desired Behavior

When I get the list of available components from the GetClientSideWebParts call I can then add them to the page as well as set properties. When I get the definitions there is no dataVersion anywhere in the component definition (please let me know if I missed it) so we always default to 1.0.

Is there a way to know the correct/latest dataVersion so we can set it correctly when creating web parts

Observed Behavior

Some properties do not "work" until I also update the dataVersion. You can see full explanation in this issue.

Steps to Reproduce

This code uses latest version of pnpjs libraries

npm install @pnp/logging @pnp/common @pnp/odata @pnp/sp

import { sp, ClientSideWebpart } from "@pnp/sp";

const partDefs = await sp.web.getClientSideWebParts();

// this is person web part
const yy = partDefs.filter(d => d.Id.startsWith("7f7184"));

const part = ClientSideWebpart.fromComponentDef(yy[0]);

part.setProperties({
    layout: 2,
    persons: [{
        id: "i:0#.f|membership|[email protected]",
    }],
});

// FORCE dataVersion - can't do this in current pnp release, will change for upcoming
// the property is there it is just protected
(<any>part).dataVersion = "1.2";

const page = await ClientSidePage.fromFile(sp.web.getFileByServerRelativeUrl("/sites/dev/sitepages/Test_8q5L.aspx"));

page.addSection().addControl(part);

await page.save();
spfx-general tracked question

Most helpful comment

@waldekmastykarz since you are interested.

All 3 comments

@waldekmastykarz since you are interested.

Thank you for your feedback.
Data version is not in the manifest as it's a runtime value. It depends on the actual code that's executing.

The reason behind this is that a web part could have a manifest pointing at a URL in the CDN that may get updated with a JS fix. If that changed the serialized data, a change in the data version would be required, but the manifest would never be updated.

From the pnp issue, I checked the People web part, and the layout property was added in data version 1.1, so using 1.0 will enforce the layout to be Compact (the default at the time).

When enforcing the properties of a web part externally, you should be aware of which data version are you using, as the web part code may run code to modify the properties to map with its current data version. This is done to ensure backwards compatibility regardless of when the web part was added to the page.

In order to check the data version, you can check the serialized data of the web part. BaseClientSideWebPart.dataVersion is the data version, and it defaults to 1.0

@mpasarin - when I add a web part through code how would one even know what data version is correct? Is there not a way to publish the latest version somehow in the manifest? Yes, if I add the web part manually and then pull down the content I can see the version - for folks trying to provision through code - is the idea they would have to do this step for each web part to ensure they have the correct data version?

Was this page helpful?
0 / 5 - 0 ratings