SP 2016. I have a web-scoped feature that
- deploys an HTML page to the Site Assets library via a module
- has feature activation code that:
- creates a new Wiki page in the Site Pages library
- inserts a Content Editor Web Part into the new Wiki page
- sets the "Content Link" property of the CEWP to the URL of the HTML file in the Site Assets library
This works like a charm if you go to the site features page and click the Activate button in a browser, but it is failing if I try to activate the feature via Powershell Enable-SPFeature
.
Specifically, the part that is failing is the insertion of the CEWP:
SPFile page = web.GetFile(urlToNewWikiPage);
if (page.Exists)
{
SPList siteAssets = web.Lists["Site Assets"];
ContentEditorWebPart cewp = new ContentEditorWebPart();
cewp.ChromeType = PartChromeType.None;
cewp.ContentLink = $"{siteAssets.RootFolder.ServerRelativeUrl}/my-subfolder/my-page.html";
WikiEditPage.InsertWebPartIntoWikiPage(page, cewp, 0);
}
The exception I catch is as follows
Microsoft.SharePoint.WebPartPages.WebPartPageUserException
This page has encountered a critical error. Contact your system administrator if this problem persists.
at Microsoft.SharePoint.ApplicationRuntime.SafeControls.IsSafeControl(Boolean isAppWeb, Type type, String& unsafeErrorMessage, Boolean blockPropertyTraversal, Boolean containsTraversalChars)
at Microsoft.SharePoint.WebPartPages.TypeCache.CheckIfSafeControl(Type type, Boolean isAppWeb)
at Microsoft.SharePoint.WebPartPages.TypeCache.get_Item(Type type, Boolean isAppWeb, Boolean disableSafeControlsCheck)
at Microsoft.SharePoint.WebPartPages.WebPart.WriteXmlGlobal(Boolean disableSafeControlsCheck)
at Microsoft.SharePoint.WebPartPages.BinaryWebPartSerializer.Serialize(SerializationMode mode, BinaryWebPartSerializerFlag binaryWebPartSerializerFlags, SPSerializationBinderBase serializationBinder, BinaryWebPartSerializerWriter writer)
at Microsoft.SharePoint.WebPartPages.SPWebPartManager.AddWebPartToStore(WebPart webPart, Int32 viewId, String viewGuid)
at Microsoft.SharePoint.WebPartPages.SPWebPartManager.AddWebPartInternal(SPSupersetWebPart superset, Boolean throwIfLocked)
at Microsoft.SharePoint.WebPartPages.WikiEditPage.InsertWebPartIntoWikiPage(SPFile wikiFile, WebPart webpart, Int32 position)
Nearby my entries in the ULS logs where I catch the exception (same correlation ID) there are
Unknown SPRequest error occurred. More information: 0x800707dc
<nativehr>0x800707dc</nativehr><nativestack></nativestack>
System.Runtime.InteropServices.COMException: <nativehr>0x800707dc</nativehr><nativestack></nativestack>, StackTrace:
at Microsoft.SharePoint.WebPartPages.SPWebPartManager.ThrowIfCantModifyZone(String zoneID, Boolean throwIfLocked)
[my caught exception here]
Cannot complete this action. Please try again.<nativehr>0x80004005</nativehr><nativestack></nativestack>
The Powershell script is being run by a service account that has Site Collection Admin rights on the site collection / subsite where I'm trying to activate the feature.
Even so, just in case it might help, I wrapped all that code inside an SPSecurity.RunWithElevatedPrivileges
delegate, and it still throws the same errors when the feature is activated by Powershell (but still works when activated from a browser).
Any ideas as to what's going on here, and why it would work if I do it manually from a browser, but doesn't work from Powershell?