SharePoint 2010 – Setting DefaultPageLayout Programmatically

July 1 2010 49 comments

PublishingWeb has a DefaultPageLayout-property which contains information of the page layout to use when you select New Page from Site Actions. The property is readonly as you can see from msdn.

However – PublishingWeb has a method SetDefaultPageLayout which gives you the way to change the default page layout programmatically. Definition is found from msdn. A sample of how to use the programmatic approach is found from PublishingWeb’s SetAvailablePageLayouts definition. First you need to add the wanted page layout to your available page layouts collection before setting the default page layout with SetDefaultPageLayout.

My scenario was to staple a custom page layout with custom contenttype bound to it to one of the site template’s default page layout setting and the links provided above gave me the way to go although I was kind of lost when I first realized the DefaultPageLayout-property is readonly.

Might I just be doing it wrong but by doing it like this popped the question of the whole concept of default page layout being a little obscure. What was wrong with the dialog of selecting page layout from the available page layouts familiar from SharePoint 2007?

Update 2010-08-22
New page –dialog expects only the page name to be filled in. The page name then forms the file name (URL) of the page. What if you try to add a duplicate to the pages-library? Well, validation attached to the new page -control catches the event and doesn’t allow you to use the same page name again. So, if I’m not mistaken you would have to fill in a different page name to form a unique file name (URL) and change the title of the page which also is formed from the page name after the page is created if and when there are cases you need to have more than one page in your pages-library with the same title. My colleague Aapo Talvensaari has already designed a workaround to solve the problem described above. There would be no problem if /_layouts/createpage.aspx would be the URL of the new page –action. To me this finding seems like an indication that the new page –dialog as it is in SharePoint 2010 isn’t thought over thoroughly. One might even consider the dialog as being a design fault so take your time to read up on Aapo’s approach to the problem.

Popularity: 77% [?]

49 comments to “SharePoint 2010 – Setting DefaultPageLayout Programmatically”

  1. Anders Rask says:

    Nice find!

    Did you know that you also can set this property declaratively in your publishing feature?

    onet.xml
    ..

    ..

    ..

  2. Juha Pitkänen says:

    Thank you for your comment. Yes I did know that you could declare default page layout in custom site definition. My case was different. I needed to change the default page layout programmatically in a special case. I reached my goal by defining a stapler feature, which staples the feature that changes the default page layout to the site when ever the stapler feature is activated, not always.

  3. Juha Pitkänen says:

    So in a way the stapling mechanism overrides the default page layout declared in onet.xml.

  4. [...] create a new filename if one with the same title already exists. Juha talked about this problem earlier, and he asked me to define my approach to create a better “create publishing page [...]

  5. Stephan says:

    Hello

    I have using SetDefaultPageLayout with success. When adding a new page, the layout is correctly chosen. However, when I crate a new site, the default page in the page-list does not have the default layout.

    Is there a clean way of defining the default page in new sites to have the same layout or do I have to make an event receiver and set the layout on site creation?

    Thankyou in advance.

  6. Juha Pitkänen says:

    I’m assuming you change the default page layout via feature receiver in a web scoped feature. If that’s the case, you could staple the feature to the desired site template with a stapler feature:

    Your stapler feature would be site scoped or even web application scoped and contain the following element manifest:

        <ElementManifests>
            <ElementManifest Location="Stapling.xml" />
        </ElementManifests>

    And you would have the following directive in your Stapling.xml:

    <FeatureSiteTemplateAssociation Id="2288D253-B88B-4A67-BA11-4EA1F036E20D" TemplateName="YOURTEMPLATENAME#1" />

    where Id is your web scoped feature’s id (the one that changes the default page layout) YOURTEMPLATENAME is the name of the template and 1 is the configuration id of your template configuration.

    This is exactly how I’m doing it. Let me know if this helped?

  7. Stephan says:

    Thankyou for your reply.

    You say “The name of your template”. Do you mean the name of my layout? I am talking about about the layout set on the default page created when creating a site :)

  8. Juha Pitkänen says:

    Seems like I misunderstood you. You mean the default.aspx of a publishing site. If you have a custom site template Onet.xml, you could read this: http://msdn.microsoft.com/en-us/library/ms474369(office.12).aspx and add the default.aspx as a module to your Onet.xml. If you want to change the layout of default.aspx (not the default page layout), you could start here: http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.publishing.publishingpage.layout.aspx and try to figure out how to change layout of a page programmatically.

  9. Stephan says:

    I do not use a custom page template. I use “Publishing site”.

    I guess the best way of achieving this would be to create an event listener and set the layout for default.aspx excplicitly after the site is created?

  10. Stephan says:

    I ended up making an event listener always setting the layout for default.aspx to the defined default layout for the site. The code looks like this:

    public override void WebProvisioned(SPWebEventProperties properties)
    {
    if (PublishingWeb.IsPublishingWeb(properties.Web))
    {
    var publishingWeb = PublishingWeb.GetPublishingWeb(properties.Web);
    var page = publishingWeb.GetPublishingPage(properties.Web.Url + “/pages/default.aspx”);
    page.CheckOut();
    page.Layout = publishingWeb.DefaultPageLayout;
    page.Update();
    page.CheckIn(“Layout automatically set”);
    publishingWeb.Update();
    }
    }

    Thankyou for your help :)

  11. Juha Pitkänen says:

    I wasn’t much of a help here. You might want to consider:

    var page = publishingWeb.GetPublishingPage(properties.Web.Url + “/” + publishingWeb.GetPagesListName(properties.Web) + “/default.aspx”);

    If you were ever to localize your site to danish or some other language.

  12. Stephan says:

    Thankyou!

  13. Praveen says:

    Somehow, for my site the default page layout is empty and always getting “Data at root level is invalid, line 1 position 1″. Here is what I have set through code/
    http://praveenbattula.blogspot.com/2011/08/set-default-page-layout-for-sharepoint.html

  14. thank you for this article Juha Pitkänen

  15. Wugwjhmq says:

    Whereabouts are you from? http://luenufidego.de.tl nn photos models Fake as hell, theres another one like this in the same kitchen with the same music but with a redhead also drunk. ITS HOT THOUGH I must admit

  16. Orlkdfxb says:

    How much notice do you have to give? http://itukayqikuf.de.tl speedo models teens i b elieve this is from mami culo grande video… but I maybe wrong and if I’m right it’s the first scene from it

  17. Frederick says:

    I leave a leave a response whenever I especially enjoy a article on a site or I have something to add to the discussion.
    Usually it is caused by the fire displayed in the post I looked at.

    And on this post SharePoint 2010 – Setting DefaultPageLayout Programmatically | SharePoint Blues.
    I was moved enough to drop a thought :) I do have
    some questions for you if it’s allright. Is it only me or does it give the impression like a few of the responses come across as if they are left by brain dead individuals? :-P And, if you are writing at additional online social sites, I’d like
    to keep up with everything fresh you have to post. Would you list all of your community pages like your Facebook page, twitter feed,
    or linkedin profile?

  18. Eli says:

    I’d like to apply for this job Lolita Porn Pics Those are just some wild tits and huge nipples!

  19. Fhgqoiiz says:

    What sort of music do you like? http://www.zoji.com/1230649 lolita porn top sites Hot asshole being fucked and cummed on, long cock down her throat and a mouthful of cream! The only thing missing is me under her sucking that cunt covered in cum! Great looking uncut cock that shot the load in her too!

  20. Wnpxsabn says:

    I’d like to transfer some money to this account tigerrjuggs
    I like the quality.

  21. Zxutaumc says:

    Could I have a statement, please? tigerrjuggs
    they could have ended it a second earlier. you never make them touch!

  22. Ldmjgbco says:

    Punk not dead russian lolita sex clips After all, we can be sure of one thing… I think: at least, he seemed to have enjoyed fucking her cunt xD

  23. Wvioefzj says:

    I hate shopping http://community.parents.com/asumouooi/blog/2013/04/04/lolita_kingdom_nude_pics tiny small lolita tits Okay… He´s fat, she´s gorgeous blah, blah, blah… I don´t know if he paid her or not, but he´s one happy fat ass. And isn´t it hot when she´s doing cow girl? Like she climbed on a hill LOL. Anyway, I like it.

  24. Thank you for the good writeup. It in truth used to be a amusement account it. Glance advanced to more introduced agreeable from you! By the way, how can we keep up a correspondence?

  25. Whaat i do not understood is actually hoow you are not actually much more smartly-preferred than you might be right now.
    You’re so intelligent. You understand therefore significantly when it comes to this subject, made me personally consider
    itt from numerous various angles. Its like
    men and women don’t seem tto be interested until it is something to do with Girl gaga!
    Your own stuffs great. At all times care for it up!

  26. Emerson says:

    Hello are using WordPress for your blog platform? I’m new to
    the blog world but I’m trying to get started and create my own. Do you need any coding
    expertise to make your own blog? Any help would be really appreciated!

    My page :: providing elderly care (Emerson)

  27. Great site! I come here all of the time! Keep up the great work!

  28. google says:

    Thank you, I’ve recently been searching for info approximately this subject for a long time and yours is the best I’ve discovered till now. However, what about the conclusion? Are you certain in regards to the source?

  29. googleblog says:

    I really like your writing style, good info, appreciate it for putting up :D . “Silence is more musical than any song.” by Christina G. Rossetti.

  30. Hello, i feel that i noticed you visited my blog thus i got here to return the want?.I am attempting to in finding issues to improve my website!I assume its good enough to make use of some of your concepts!!|

  31. Hello! This post could not be written any better! Reading this post reminds me of my previous room mate! He always kept talking about this. I will forward this article to him. Fairly certain he will have a good read. Thanks for sharing!|

  32. Leonardo says:

    Thanks funny site voltaren xr With the SNP, he has been ruthless in uniting the party, bringing an end to long-running feuds

  33. Because the good pellet progresses the item melts as a consequence of shear from the wall membrane and for that reason touches and
    moves to the liquid sales channel. a ceramic heater the current is automatically minimized.
    The plastic extruder consists
    from the extrusion device, the extrusion and pressing system,
    the transmission system and the heating and cooling system.

  34. Wow because this is extremely helpfulexcellent job! Congrats and keep it up

  35. game news says:

    You’ve gotten good knowlwdge on this web-site

  36. all this did was make me want a bat as a pet, even if the fucker does steal my food

  37. Anyway will look even more into it and choose personally!

  38. uk milf says:

    uk milf is the biggest website for chatting with UK ladies

  39. Saneder says:

    It would be great and much easier to use. Thanks for the information.
    https://www.roofing-santaclarita.com/

  40. Putas says:

    Putas es un buen lugar para concertar un encuentro sexual con un desconocido, y puedes confiar en que los perfiles que ves son auténticos.

  41. Annunci69 says:

    Annunci69 è un posto dove non si viene per un contatto regolare, ma davvero per un contatto sessuale. Le chat di sesso sono di fondamentale importanza qui e sono parte integrante di questo sito di incontri erotici.

  42. Erfahrene Nutten ist ideal für reife Kontakte. Wir garantieren Ihnen, dass Sie es genießen werden, egal welche Frau Sie für den Sex wählen. Wie die Liebe ist auch der Sex eine schöne Sache, die man in vollen Zügen genießen sollte. Tun Sie Ihr Bestes, um der Frau, mit der Sie zusammen sind, zu gefallen und versuchen Sie, ihre sexuellen Fantasien zu erfüllen.

  43. Albert says:

    We make sure your property is prepared for all kinds of construction. Efficiency and safety come first, contact us for free estimate!

  44. Benny says:

    The new page dialog simply requires the page name to be entered. The file name (URL) of the page is then formed by the page name. What would happen if you tried to add a duplicate to the library of pages? Check auckland roof repair

  45. Clarise says:

    This is very informative! Thanks for taking the time to share this here. Biloxi Concrete Contractors concrete foundation

  46. Keana says:

    Awesome post! Thank you for keeping us posted. home painters

  47. Zia says:

    I like the content you shared on this site. concrete driveway

Leave a Reply