SubPointSolutions / spmeta2

SharePoint artifact provision for .NET platform. Supports SharePoint Online, SharePoint 2019, 2016 and 2013 via CSOM/SSOM.
http://subpointsolutions.com/spmeta2
133 stars 56 forks source link

How to deploy WikiPageDefinition with code (maybe ScriptEditor?) via SPMeta #1111

Closed andreasblueher closed 6 years ago

andreasblueher commented 6 years ago

Hey,

I've tried a couple of different things to deploy a WikiPage to my SitePages library with JavaScript code on it.

public static WikiPageDefinition ServiceHomePageInProgress = new WikiPageDefinition
{
    Title = "In Progress",
    Content = "MyCode",
    FileName = "In Progress.aspx"
};

or empty WikiPageDefinition with ScriptEditorWebpar

.AddWikiPage(Definitions.WebPartDefinitions.TicketToDosRefresh, page => page.AddScriptEditorWebPart(new ScriptEditorWebPartDefinition
{
    Content = MyCode,
    Id = "OpenAgainWebpartsOpenAgainWebparts"
}))

Neither of those options results in a working script. When trying the 2nd option I even got an exception when accessing the application page:

Application error when access /SitePages/In Progress.aspx, Error=StartIndex cannot be less than zero. Parameter name: startIndex at System.String.Substring(Int32 startIndex, Int32 length) at Microsoft.SharePoint.WebPartPages.ScriptEditorWebPart.GetHiddenFieldName() at Microsoft.SharePoint.WebPartPages.ScriptEditorWebPart.OnLoad(EventArgs e) at System.Web.UI.Control.LoadRecursive() at System.Web.UI.Control.LoadRecursive() at System.Web.UI.Control.LoadRecursive() at System.Web.UI.Control.LoadRecursive() at System.Web.UI.Control.LoadRecursive() at System.Web.UI.Control.LoadRecursive() at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)

I couldn't find any references regarding this exception, so I have no clue what the real issue is.

Currently I deploy the application using SPMeta2 and add the JavaScript code later on by using "Embed code" via UI.

SubPointSupport commented 6 years ago

A few things to test out here:

Could you please provider SharePoint version (13/16) and API used (CSOM/SSOM) - will test to see if this scenario even works.

As for supported scenarios and adding web part to a wiki page, we have the following one covered: CanDeploy_ScriptEditorTo_WikiPageContent_As_AddToPageContent

https://github.com/SubPointSolutions/spmeta2/blob/master/SPMeta2/SPMeta2.Regression.Tests/Impl/Scenarios/WebpartScenariosTest.cs#L790

Web part provision to a wiki page is rather a tricky one. SharePoint has got a special zone ID "wpz", and then adds everything to the wiki page content (similar to publishing pages) with super-special web part ID mapping. We have tons of magic underneath to get it more or less stable and repeatable but it won't be a wonder that it fails on some cases.

If other means to deliver this JavaScript on the page can be considered - that might be a more stable solution at the end. Meanwhile, more than happy to troubleshoot this case and make it working as much as we can.

andreasblueher commented 6 years ago

Hey guys,

WebpartScenariosTest.cs helped me a lot. I managed to deployed the WikiPage, the ScriptEditor and add the JavaScript automatically. It works like a charm.

I've thought about what piece of my code I could share to make this issue more useful to other people, but your scenario should help everyone.