How do you programmatically add an item to the Pages list from a feature receiver in a sandbox solution?

NB: The Microsoft.Publishing.dll assembly is not available within the sandbox, so you can't use the PublishingWeb class.

In addition, I also want to be able to specify a custom page layout, and make the page the site's default / home page.


In Sandbox solutions you could always rely on MODULE-s to deploy physical files. You should:

  1. Create a new Module, which deploy in the PAGES library. The trick here is to properly reference the handler for your page, because as oposed to WebPart pages, Publishing pages rely on Page Layouts (you also need to ensure that Page Layout and associated content type is already deployed at Site collection level, prior to attempting page creation). I'd recomand you read Waldek's post http://blog.mastykarz.nl/provisioning-publishing-pages-features-declarative-markup/ but also look in the 14-hive for the Publishing Web feature which has examples in it.
  2. Deploy the module via a Feature (could be Web scoped).
  • Hi C. Marius. I already have modules in my solution which deploy publishing pages to all my new sites, however in this particular case I am creating a feature which needs to delete and recreate the pages of an existing site each time it's deactivated and reactivated. Declarative modules do not support this behaviour (which is confirmed at the bottom of Waldek's post that you linked) -- when a feature containing a module is reactivated, the page is never overwritten, instead multiple copies of the web parts are added to the zones / page. So I'm stuck with using a feature receiver...
    – user4545
    Jan 14 '13 at 16:51
  • If it's all about deleting (and you are sure of it), why don't you simply connect to the PAGES library and delete the item as it would be a Document? Jan 15 '13 at 15:13
  • I've already got the deletions working fine. However in order to provision the pages again AFTER the existing files have been deleted, it needs to be done programmatically in the feature receiver too. I realise I could use two seperate features (one to delete the files programmatically, and a second feature to declaratively reprovision the pages) -- however that seems a little unecessary!
    – user4545
    Jan 15 '13 at 15:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy