Wednesday, March 7, 2012

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

Thursday, February 16, 2012

Javascript/jquery count li elements inside ul

Below is the script with which you can get the count of li present in ul..


Html::

<ul id="myulid">
  <li>List Item1</li>
  <li>List Item2</li>
  <li>List Item3</li>
  <li>List Item4</li>
  <li>List Item5</li>
</ul>

Java Script:: 


<script type="text/javascript">
var myul = document.getElementById('myulid');
var liCount= myul.getElementsByTagName('li').length;
alert(liCount);
</script>


Hope this helps...

Popular Posts