locked
Set home page programatically RRS feed

  • Question

  • Hi all,

    In Sharepoint 2010 we now have a button on the ribon to set the current page to the default landing page for the Web. My question is 1) how do I do that with code and

    2) (This should be easy if question 1 is answered) How do I programatically check whether the current page is the home page for the SPWeb   

    Thanks in advance

    Tuesday, January 4, 2011 12:34 PM

Answers

All replies

    • Marked as answer by Wayne Fan Monday, January 10, 2011 10:31 AM
    Tuesday, January 4, 2011 12:43 PM
  • The following code will work on a Publishing Web:

        public void SetWelcomePageOnWeb(string p_sWebUrl, string p_sWelcomePageUrl)
        {
          using ( var oSite = new SPSite(p_sWebUrl) )
          {
            using ( var oWeb = oSite.OpenWeb() )
            {
              if ( Microsoft.SharePoint.Publishing.PublishingWeb.IsPublishingWeb(oWeb) )
              {
                var oPublishingWeb = Microsoft.SharePoint.Publishing.PublishingWeb.GetPublishingWeb(oWeb);
    
                try
                {
                  var oWelcomePageFile = oWeb.GetFile(p_sWelcomePageUrl);
                  oPublishingWeb.DefaultPage = oWelcomePageFile;
                  oPublishingWeb.Update();
                }
                catch(Exception oException)
                {
                  //handle the exception
                }
                finally
                {
                  //prevent memory / SPRequest leaks by closing the Publishing web
                  oPublishingWeb.Close();
                }
              }
            }
          }
        }
    
    • Marked as answer by Wayne Fan Monday, January 10, 2011 10:31 AM
    Tuesday, January 4, 2011 2:58 PM
  • You can also do this via custom actions and the client object model.  I wrote a blog post on doing this exact thing not too long ago.  Note that it's for non-publishing sites.

    http://www.mukalian.com/blog/post/2010/10/11/Custom-Actions-in-SharePoint-2010-Knight-Rider-on-Steroids.aspx

    - M


    - Michael Mukalian - 2010 MS MVP SharePoint Services - MCTS: MOSS 2007 Configuration - http://www.mukalian.com/blog
    • Marked as answer by Wayne Fan Monday, January 10, 2011 10:31 AM
    Wednesday, January 5, 2011 1:59 AM
  • Hi,

    There is  web.RootFolder.WelcomePage property.

    http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spfolder.welcomepage.aspx

    Hope it helps.

    Regards,

     

     

     

     


    Vladimir
    MSTS, SharePoint tips blog: http://buyevich.blogspot.com
    • Marked as answer by Wayne Fan Monday, January 10, 2011 10:31 AM
    Wednesday, January 5, 2011 2:57 PM
  • hi  Ahammad,

      thanks  , this  is  a  great  post . this link  is  very  useful  for me


    chakravarthy k

    Thursday, April 12, 2012 5:57 AM