Search This Blog

Showing posts with label Javascript. Show all posts
Showing posts with label Javascript. Show all posts

Wednesday, January 18, 2017

Create a Handler Page to Call Methods from Class file to JavaScript

Today i had a task which i need to call a code behind method from .ascx page. So i have created a handler page to achieve this task. Here i will tell you the steps to create a handler page:

1. Create a SharePoint 2013 empty project like below image:


2. Create a Mapped Layouts folder


3. In Visual Studio 2013 we don't have Dynamic Handler. So here we need to create Handler page by our selves. From add new item select an application page and rename it like below:


4. Now remove the designer.cs from the .ashx file. Here we don't need the designer file.

5. Open MyHandler.ashx.cs. It will look like below image:


6. Next we have to create GUID for this handler page. From tools select Create GUID


7. From that pop up select Create GUID and copy that ID

8. Now paste that GUID in top of our class file like below, make sure that all the alphabetical characters are in smaller case in that GUID:



   For this GUID we need to include the using System.Runtime.InteropServices; name space.

9. In class file remove the Page load method and add the default methods like below:


    and make sure that your handler page inherits from IHttpHandler not from layouts page. 
    Change that like the image:

10. Now we have to modify the MyHandler.ashx page. Delete everything from that page and replave         with the below code:

<%@ Assembly Name="$SharePoint.Project.AssemblyFullName$" %>
<%@ WebHandler Class="$SharePoint.Type.4dcefb1c-d4d7-4dd4-9870-4d0f66c2e55d.FullName$" %>

Here paste our GUID after SharePoint.Type with all the alphabets are in smaller case.

11. Now Change the Solution settings:

First unload the project:


Then edit .csproject like settings:


Add the the below line after the <SandboxSolutions> tag

<TokenReplacementFileExtensions>ashx;</TokenReplacementFileExtensions>


Now reload the project:


Now deploy your solution and browse the following url:

http://siteurl/_layouts/15/TestHandlerPage/MyHandler.ashx

and we can get the Out put like below:


If you are facing any error while accessing the page then change the some settings like below:

Right click the MyHandler.ashx and select properties. From that properties change the build action to Content


Now we will get the out put.

12. Now we can see how to call that method from .ascx page

function OpenTreeViewPopUp() {
                try {                    
                    $.ajax({
                        url: '/cgmt/_layouts/15/TestHandlerPage/MyHandler.ashx',
                        contentType: "application/json; charset=utf-8",
                        success: function (result) {
                            alert(result);
                        },
                        error: function (data) {
                            //alert(result);
                        }
                    });
                } catch (e) {
                    alert(e.message);
                }
                return false;
            }

using ajax we can call the handler method like the above code. The result will return the values from code behind.

Reference : Click Here

Hope this will help,
Thank you


Tuesday, September 1, 2015

Get Values From Query String Using jQuery

 $(document).ready(function () {
            try {              
                var mode = GetParameterValues('Mode');
                alert(mode);
            } catch (e) {
                alert(e);
            }
        });

function GetParameterValues(param) {
            var url = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
            for (var i = 0; i < url.length; i++) {
                var urlparam = url[i].split('=');
                if (urlparam[0] == param) {
                    return urlparam[1];
                }
            }
        }

Thursday, July 9, 2015

link button property to open in new tab?

 By Adding following onClientClick even in your link button we can open the url in new tab.

OnClientClick="aspnetForm.target ='_blank';"

so on click it will call Javascript function an will open respective link in News tab.

<asp:LinkButton id="lbnkVidTtile1" OnClientClick="aspnetForm.target ='_blank';" runat="Server" Text="New Tab"  />

and another option is you can go for Hyper link control to achieve this. By default it has the Target property and you can set that to _blank.

thank you,

Monday, June 29, 2015

How to hide a Yes/No column in NewForm.aspx and EditForm.aspx in sharepoint 2013

We can't hide the Yes/No field using OOB in sharepoint. Better we go for script editor to hide the particular column. Apply the following steps.

Add in the Content Editor Web Part located under the Media and Content category.

Paste the following code:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>

<script type="text/javascript">
$(document).ready(function() {
    $('nobr:contains("TestColumn")').closest('tr').hide(); 
});

</script>

you can use this for multiple columns also.


thank you.



Friday, June 19, 2015

How to add Custom validation for Newform.aspx and Editform.aspx in Sharepoint 2013

I am using PreSaveAction() JavaScript function to check whether start day is lower than End day or not. If it is lower than greater than end day then it will show an alert message;

Add in the Content Editor Web Part located under the Media and Content category.

Paste the following code:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script type="text/javascript">
 var j = jQuery.noConflict(); 
 function PreSaveAction() { 
 var startDay= j(":input[title='Start_Day']").val(); 
var endDay= j(":input[title='End_Day']").val(); 
if(startDay=='')
{
alert("Please enter start day");
return false;
}
else if(endDay=='')
{
alert("Please enter end day");
return false;
}
else if(endDay < startDay)
{       
   alert("End day should be greater than start day");          
   return false;
}
else{
   return true;
    }
}

</script>

thank you..

Saturday, May 30, 2015

Get the checkbox value is checked or not using Jquery

 $(document).ready(function () {
    $('#<%=chkValidateBabyName.ClientID%>').change(function () {
          if ($(this).is(":checked")) {
            alert("Check box checked");
          }
          else{
           alert("Check box un checked");
         }
    });
});

Include this code in document ready function. then you will get checkbox value when the check box value is changed..

Monday, May 25, 2015

How to create a static variable at client side

Actually we cant create a static variable at client side. But we can keep the value even after page postback using Hidden field control.

First we need to create a hidden control and need to set ClientIDMode as "Static"

Ex: <asp:HiddenField ID="hndControl" ClientIDMode="Static" runat="server" />

After that we need to assign the value to that hidden field.

document.getElementById('<%= hndControl.ClientID %>').value = "Static";

Here after we can use that value until we change hidden field value.



Sunday, May 24, 2015

How to avoid alphabets and special character from text box using javascript

Use the following code OnKeyPress Event in your text box:

<asp:TextBox ID="txtNumber" runat="server" OnKeyPress="return IsNumeric(event);"></asp:TextBox>


function IsNumeric(e) {
    var keyCode = e.which ? e.which : e.keyCode
    var ret = ((keyCode >= 48 && keyCode <= 57) || specialKeys.indexOf(keyCode) != -1);
    document.getElementById("error").style.display = ret ? "none" : "inline";
    //TestOnTextChange();
    return ret;
}

Restricting Custom People Picker to only one Sharepoint group programatically

Refer the following script files in your page,     <!-- For People Picker -->     <script type="text/javascript" src...