Wednesday, March 7, 2012

Programatically set the welcome page of a sharepoint site MOSS2010

In this post we will see how to change the welcome page of a sharepoint site using object modelling.

Normally for any sharepoint site, the welcome page will be default.aspx. In this post we will set the Home.aspx page(present in pages library) as the welcome page, so that home.aspx will open as soon as user opens the site.

Below is the code snippet::

        /// <summary>
        /// method to add welcome page
        /// </summary>
        /// <param name="publishingWeb"></param>
        /// <param name="oWeb"></param>
        /// <param name="pageName"></param>
        private void CreateWelcomePage(PublishingWeb publishingWeb, string pageName)
        {
            try
            {
                string fullPageUrl = publishingWeb.Url + "/Pages/" + pageName + ".aspx";
                SPFile fileNew = publishingWeb.Web.GetFile(fullPageUrl);
                publishingWeb.DefaultPage = fileNew;
                fileNew.Publish(pageName + ".aspx set as Welcome Page");
                fileNew.Approve(pageName + ".aspx set as Welcome Page");
                publishingWeb.Update();
            }
            catch (Exception ex)
            {
               
            }

        }

Call the above method as below
CreateWelcomePage(myPublishingWebObj,"Home");


Hope this helps...
Next post: Programatically applying master page in sharepoint

Programatically changing the page layout of default.aspx page MOSS2010

In this post we will change the page layout of default.aspx page MOSS2010 using object modelling.
Below is the code snippet::

        /// <summary>
        /// Method to Change PageLayout For DefalutPage
        /// </summary>
        /// <param name="publishingWeb"></param>
        /// <param name="currentWeb"></param>
        public void ChangePageLayoutForDefalutPage(PublishingWeb publishingWeb, string customPageLayout)
        {

            PublishingPage publishingPage = publishingWeb.GetPublishingPage(publishingWeb.Url + "/Pages/default.aspx");

            if (publishingPage != null)
            {
                List<PageLayout> layouts = new List<PageLayout>(publishingWeb.GetAvailablePageLayouts());
                PageLayout newPageLayout = layouts.Find(
                                delegate(PageLayout l)
                                {
                                    return l.Name.Equals(customPageLayout, StringComparison.CurrentCultureIgnoreCase);
                                });
                if (newPageLayout != null)
                {
                    if (publishingPage.ListItem.File.CheckOutType == SPFile.SPCheckOutType.None)
                    {
                        publishingPage.CheckOut();
                    }
                    publishingPage.Layout = newPageLayout;
                    publishingPage.Update();
                    publishingPage.CheckIn("Checked In");
                    publishingPage.ListItem.File.Publish("page published");
                    publishingPage.ListItem.File.Approve("page approved");
                    publishingWeb.Update();

                }

            }


        }

Call the method as below
ChangePageLayoutForDefalutPage(mypublishingwebObj,"myPagelayout.aspx");


Hope this helps..

Programatically applying master page of a sharepoint site MOSS2010

In this post we will see how to Programatically set the master page of a sharepoint site.
And also make sure its subsites inherits the root site master page.
Below is the code snippet::

/// <summary>
        /// method to add master page
        /// </summary>
        /// <param name="CurrentWeb"></param>
        public void AddMasterPage(SPWeb CurrentWeb,string customMasterPage)
        {
            try
            {

                CurrentWeb.MasterUrl = CurrentWeb.Site.RootWeb.ServerRelativeUrl + "_catalogs/masterpage/v4.master";

                CurrentWeb.CustomMasterUrl = CurrentWeb.Site.RootWeb.ServerRelativeUrl + "_catalogs/masterpage/" + customMasterPage;

                CurrentWeb.Update();

//To change master page of all subsites
                foreach (SPWeb subweb in CurrentWeb.GetSubwebsForCurrentUser())
                {

                    ChangeMasterPage(subweb, CurrentWeb.MasterUrl, CurrentWeb.CustomMasterUrl);

                }

                CurrentWeb.Update();

            }
            catch (Exception ex)
            {

            }
        }


        /// <summary>
        /// method to change master page
        /// </summary>
        /// <param name="Web"></param>
        /// <param name="pstrMasterURL"></param>
        /// <param name="pstrCustomURL"></param>
        private void ChangeMasterPage(SPWeb subWeb, string pstrMasterURL, string pstrCustomURL)
        {
            try
            {
                subWeb.AllowUnsafeUpdates = true;
                subWeb.MasterUrl = pstrMasterURL;

                subWeb.CustomMasterUrl = pstrCustomURL;

                subWeb.Update();
                subWeb.AllowUnsafeUpdates = false;
                subWeb.Dispose();

            }
            catch (Exception ex)
            {

            }


        }

Call the method as below
AddMasterPage(myweb,"mymasterpage.master");


Hope this helps...
Next Post- Programatically changing pagelayout of default.aspx page in Sharepoint

Popular Posts