0

Being a fan of DRY to write maintainable code I'm having some problems getting to terms with SharePoint development. Some information (e.g. GUIDs) is often repeated and spread out over several files, making it hard to know how things relate to each ohter, and I'm always looking for ways to do things programmatically (where I can use e.g. constants and get compile-time consistency checks), rather than via XML files spread out all over the project. Here's an example:

I'm using the Publishing capabilities of SharePoint and want to provide a certain PageLayout associated with a certain Content Type for a certain site. In one place I'm defining the Content Type, MyContentType. In a PageLayouts module I'm provisioning the PageLayout file (MyPageLayout.aspx) using something like:

<File Path="PageLayouts\MyPageLayout.aspx" Url="MyPageLayout.aspx" Type="GhostableInLibrary" >
  <Property Name="ContentType" Value="$Resources:cmscore,contenttype_pagelayout_name;" />
  <Property Name="PublishingAssociatedContentType" Value=";#MyContentType;#LongGUID;#" />
</File>

And in the onet.xml file I have a ContentTypeBinding element that associates MyContentType with the Pages list on e.g. MySite. Again I have to repeat the ContentType GUID.

Ideally I'd like to just have the onet.xml and have some callback being run when a new site is created based on the MySite template. In the callback I would programmatically ensure the required PageLayouts have been provisioned and made available in the gallery, and at the same time create Content Types as necessary.

Is this, or something like it, possible?

1 Answer 1

1

From the scenario you describe, it sounds like all you need is a EventReciever which triggers whenever a web is Provisioned.

In your project, add new item, select EventReciver. A dialog will appear in which you select Web Events in the first dropdown and then check A site was provisioned.

Your EventReciever will be created with the specified Provisioned method in which you write everything that should happen whenever a new web is created.

It is possible to check what site template was used etc. So it should be straight forward from here.

Hope this helps :)

2
  • This part I actually know how to do, but I don't know how to add a new PageLayout programmatically - given that I have an aspx file in my project, how do I make it available as a PageLayout?
    – claesv
    Commented Oct 10, 2012 at 8:07
  • To my knowledge, only through XML. That's how SharePoint is designed.
    – user2536
    Commented Oct 10, 2012 at 8:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.