Showing posts with label Master Page. Show all posts
Showing posts with label Master Page. Show all posts

Wednesday, February 22, 2012

SharePoint 2010 : Using ECMA script recall javascript function on control click in UpdatePanel.

I dont know if this is important or not but I found this thing interesting to blog.

Goal

There is a in UpdatePanel on the page. We have to call a javascript everytime the page is postback. Even if it is a postback from a control under UpdatePanel. I discovered a way using ECMA using which we can easily call the javascript function.

Solution

<script>!window.jQuery && document.write('<script src="http://code.jquery.com/jquery-1.4.2.min.js"><\/script>');</script>
<script type="text/javascript">
    //Call the function or assign the event on ready
    $(document).ready(function(){
        //This will wait until script is loaded and than call our function
        ExecuteOrDelayUntilScriptLoaded(PageLoadEvent, "sp.js");
    });
    
    //This function is the most important function that will add the end request
    Sys.WebForms.PageRequestManager.getInstance().add_endRequest(FunctionHandler);
    
    //Event handler that fires on the UpdatePanel control events too
    function FunctionHandler(sender, args) {
        ExecuteOrDelayUntilScriptLoaded(ButtonClickEvent, "sp.js");
    }
    
    //Write the time on page load
    function PageLoadEvent(){
        alert('Page Load : ' + (new Date()).toString() );
    }

    //Will call on button click event
    function ButtonClickEvent(){
        alert('Button Click : ' + (new Date()).toString() );
    }                
</script>
<asp:UpdatePanel ID="upPanel" runat="server">
    <ContentTemplate>
        <asp:Button ID="btn" runat="server" Text="Click here!!!" />
    </ContentTemplate>
</asp:UpdatePanel>


Important Note

The most important thing in this is, if you remove "Sys.WebForms.PageRequestManager.getInstance().add_endRequest(FunctionHandler);" and try clicking the button the event wont be raised to call "function ButtonClickEvent()". So that is the magic trick.

Let me know if this helped you!

Monday, September 26, 2011

Sharepoint 2010 Master page issue with re-deployment with new changes from Visual Studio solution

PROBLEM :- There is a problem with the current process. For the first time this will work but if you make any changes to the master page and re-deploy the same solution will not deploy this new master page with the changes on the server.


In my last post "SharePoint 2010 Deploy Master Page using Visual Studio Solution Project and Set it as Default Master Page.", i explained how to add a master page and set it as a default. I suggest to read the last blog first.


What causes the issue?

Answer: When a file is deployed to the server using a Visual Studio Solution, the file gets deleted first and than added back to server. But when we assign a file as a Default Master Page it "CANNOT" be deleted. So just follow the below given steps to make it a fix.

Solution

Just write a deactivating event in the feature receiver setting V4.master as a master page. Please checkout the code below.


/// <summary>
/// Set the v4.master as default master for site.
/// </summary>
/// <param name="properties"></param>
public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
{
    SPSecurity.RunWithElevatedPrivileges(delegate
    {
        using (SPWeb _web = ((SPSite)properties.Feature.Parent).RootWeb)
        {
            Uri _siteMaster = new Uri(string.Format("{0}/_catalogs/masterpage/v4.master", _web.Url));
            _web.MasterUrl = _siteMaster.AbsolutePath;
            _web.CustomMasterUrl = _siteMaster.AbsolutePath;
            _web.Update();
        }
    });
}
Hope this was helpful to you. Thanks for reading the blog.

SharePoint 2010 Deploy Master Page using Visual Studio Solution Project and Set it as Default Master Page.

This blog will give an understanding of how to add a master page using a Visual Studio solution project. Also setting the same master as a default for site.


STEP 1 : Adding a Master Page using Module

  1. Open a visual studio 2010
  2. Add new project
  3. Empty Sharepoint Project > Deploy as Farm Solution > Finish.
  4. Add New Item > Select "Module" > I named it as "MasterPage".
  5. Delete the "Sample.txt" which gets added in the module by default.
  6. Add new MainMasterPage.master. Do whatever change you want to it.
  7. Open the elements.xml file of "MasterPage" module. You will have to make some modification shown below.


    <?xml version="1.0" encoding="utf-8"?>
    <Elements xmlns="http://schemas.microsoft.com/sharepoint/">
        <Module Name="MasterPage" List="116" Url="_catalogs/masterpage">
            <File Path="MasterPage\MainMasterPage.master" Url="MainMasterPage.master" Type="GhostableInLibrary">
                <Property Type="string" Name="Title" Value="My Main Master Page"/>
            </File>
      </Module>
    </Elements>
  8. Please verify the below given text i made change in the original one. This is MOST IMPORTANT!!!.
    List="116" Url="_catalogs/masterpage"
    Url="MainMasterPage.master" Type="GhostableInLibrary"
  9. Once this is set just deploy the project.

STEP 1.1 : Verify the file got uploaded

  1. Open the site in SharePoint Designer 2010.
  2. All Files > _catalogs > MasterPage
  3. The file should be at this location because if you see the element.xml above in the url we have given the path of "_catalogs/masterpage". That's it we now achieved the first step of adding a master file to the site.


STEP 2 : Apply the page as a default master page

  1. In the visual studio solution project select "Features > Feature1 > Feature1.feature".
  2. Right click "Add event receiver".
  3. Put this code.

    /// <summary>
    /// Set the MainMasterPage.master as default master for site.
    /// </summary>
    /// <param name="properties"></param>
    public override void FeatureActivated(SPFeatureReceiverProperties properties)
    {
        SPSecurity.RunWithElevatedPrivileges(delegate
        {
            using (SPWeb _web = ((SPSite)properties.Feature.Parent).RootWeb)
            {
                Uri _siteMaster = new Uri(string.Format("{0}/_catalogs/masterpage/MainMasterPage.master", _web.Url));
                _web.MasterUrl = _siteMaster.AbsolutePath;
                _web.CustomMasterUrl = _siteMaster.AbsolutePath;
                _web.Update();                   
            }
        });
    }
  4. Right click the project and deploy the solution.

I hope this helps you with the deployment of master page and setting it as a default.

PROBLEM :- There is a problem with the current process. For the first time this will work but if you make any changes to the master page and re-deploy the same solution will not deploy this new master page with the changes on the server. Please read my next blog "Sharepoint 2010 Master page issue with re-deployment with new changes from Visual Studio solution" for the solution.