Friday, 10 January 2014

Handling event of SharePoint form dropdown value change

Problem Description:

Many times we want to perform event handling on SharePoint list’s form.
This blog will give us idea about “Choice” column value change event handling.

As usual add Content Editor WebPart into SP list form and copy paste below code into HTML form.


<script type="text/javascript">

_spBodyOnLoadFunctionNames.push("ourFunctionName");

function ourFunctionName()
{

$("Select[Title='ColumnName']").change(function () {
alert("Choice column value changed");
});
}

</script>

Reference:



Remark:

For Date and Time column is quite different, for that follow below link,


Nice article written by Ruud Stijger.


Check Box control on SharePoint form

How to check value of checkbox control on SP form:

var EligibleValue=$("#ctl00_m_g_baacd937_c4ae_4d2d_9436_57c6bbb65904_ff191_ctl00_ctl00_BooleanField").is(":checked");

alert(EligibleValue);






How to set uncheck value of checkbox control on SP form:
   
$("#ctl00_m_g_baacd937_c4ae_4d2d_9436_57c6bbb65904_ff191_ctl00_ctl00_BooleanField").attr('checked', false);





How to set check value of checkbox control on SP form:
   
$("#ctl00_m_g_baacd937_c4ae_4d2d_9436_57c6bbb65904_ff191_ctl00_ctl00_BooleanField").attr('checked', true);





Get current list name of new-edit form using javascript

Problem Description:

While playing with JavaScript, today I require List name of current form (new, edit).
After little bit googlling i am getting some scripts but there are some bugs.
after working with that all scripts, final scripts is ready to use, as below....

var clientContext = SP.ClientContext.get_current();
oWeb = clientContext.get_web();
oListColl = oWeb.get_lists();
var currentListGUID = SP.ListOperation.Selection.getSelectedList();
var ListName="";
if (currentListGUID === null)
{
        ListName = $(".s4-titletext h2 a:first").html(); //requires JQuery to get list name from ribbon
}
else
{               
        ListName=currentListGUID;
}

That's all...!!!




Wednesday, 8 January 2014


Problem Description:

How to set default values to SharePoint form control using javascript.

Resolution:


Add below script in SharePoint form content editor web part :

<script type="text/javascript">

$(document ).ready(function() {
$("#Control_Name_From_Page_View_Source").val("Default_Value");
});


</script>


Problem Description:

Set default value to column via Powershell.

Resolution:


$w = Get-SPWeb http://localhost
$l = $w.GetListFromUrl("/Lists/RequestList/DefaultStatus.aspx")
$f = $l.Fields["Status"]
$f.DefaultValue = "Delivered"
$f.Update()


Feel free to revert in-case of any query...


Product Applies To:

·         SharePoint Server 2010
·         SharePoint Foundation 2010
·         SharePoint Server 2013



Limitation of javascript in sharepoint column name length

Problem Description:

My one of the SharePoint list column length is more than 30 characters (commented part in below image), but at the time of item updation it gives me error.
So i check with Debugger then i found column length mistake.
Because of JavaScript column length limitation if actual column name having more than 30 characters,it will take character maximum as 30 (see red circle in image).




Add items and removing all option of dropdown box in javascript

Add items and removing all option of dropdown box in javascript


How to add items in dropdown using JavaScript
<script type="text/javascript">
    function AddItem(Text,Value)
    {
        // Create an Option object       
        var opt = document.createElement("option");
      // Add an Option object to Drop Down/List Box
        document.getElementById("DropDownList").options.add(opt);
        // Assign text and value to Option object
        opt.text = Text;
        opt.value = Value;
    }
<script />


Removing all option of dropdown box in JavaScript

document.getElementById('id').options.length = 0;