Wednesday, March 7, 2012

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...

Monday, February 6, 2012

How to call code behind server method from a client side javascript

In this post we will see how to call a server side function from JavaScript.
 

HTML::
<div style="visibility: hidden">
<asp:Button ID="mybtn" runat="Server" onclick="mybtn_Click" />
</div>

Java Script::
var hiddenMyBtn = document.getElementById('<%= mybtn.ClientID %>');
hiddenMyBtn.click();

Server side code::
protected void mybtn_Click(object sender, EventArgs e)
{
//Do your stuff
}


Hope this helps...


Popular Posts