If you migrated from OfficeDevPnP (SharePointPnPCoreOnline nuget package) to PnP.Framework you will need to rewrite code which adds SPFx web parts on modern pages. Old code which used OfficeDevPnP looked like that:
var page = ctx.Web.LoadClientSidePage(pageName); var groupInfoComponent = new ClientSideComponent(); groupInfoComponent.Id = webPartId; groupInfoComponent.Manifest = webPartManifest; page.AddSection(CanvasSectionTemplate.OneColumn, 1); var groupInfiWP = new ClientSideWebPart(groupInfoComponent); page.AddControl(groupInfiWP, page.Sections[page.Sections.Count - 1].Columns[0]); page.Save(); page.Publish();
But it won't compile with PnP.Framework because ClientSideWebPart became IPageWebPart. Also ClientSideComponent means something different and doesn't have the same properties. In order to add SPFx web part to the modern page with PnP.Framework the following code can be used:
var page = ctx.Web.LoadClientSidePage(pageName); var groupInfoComponent = page.AvailablePageComponents().FirstOrDefault(c => string.Compare(c.Id, webPartId, true) == 0); var groupInfoWP = page.NewWebPart(groupInfoComponent); page.AddSection(CanvasSectionTemplate.OneColumn, 1); page.AddControl(groupInfoWP, page.Sections[page.Sections.Count - 1].Columns[0]); page.Save(); page.Publish();
Here we first get web part reference from page.AvailablePageComponents() and then create new web part using page.NewWebPart() method call. After that we add web part on a page using page.AddControl() as before. Hope it will help someone.
No comments:
Post a Comment