Tuesday, February 12, 2013

Radio checked by default in two SharePoint connected lists.

Suppose we have a list and a doc library. Both have a common column by which they are connected. So in the list, to make the radio checked by default, we need to do the following steps.

  1. Click on the radio of the item that you want to make it auto checked when the page loads.
  2. When you click the radio, the page reloads. Get the view GUID and SelectedID query string values and pass them in the code below.


<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js" type="text/javascript">
</script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.js" type="text/javascript"></script>

<script type="text/javascript">
function initjLoadMe() {
var arrayList = $("img[src*='rbsel.gif']").parent();
if(arrayList.length == 0)
                //Pass the view ID and SelectedID
SelectField('{ED49DA87-0864-41A5-A153-8DB45BC43309}','1');
return false;
}

$(document).ready(function() {
initjLoadMe();
});
</script>

Happy coding.

Monday, February 11, 2013

The SharePoint native Dropdown column does not contain a "Please select" or empty value if it is set as "Required"

The SharePoint native Dropdown column does not contain a "Please select" or empty value if it is set as "Required". To add that value and force the user to select, we need to put some javascript in an Content Editor Web Part on the page. Here is the javascript code. Please do not forget to write the fieldTitle in function MyCustomExecuteFunction.


<script type="text/javascript">
function GetDropdownByTitle(title) {
    var dropdowns = document.getElementsByTagName('select');
    for (var i = 0; i < dropdowns.length; i++) {
        if (dropdowns[i].title === title) {
            return dropdowns[i];
        }
    }
    return null;
}

function GetOKButtons() {

    var inputs = document.getElementsByTagName('input');
    var len = inputs.length;
    var okButtons = [];
    for (var i = 0; i < len; i++) {
        if (inputs[i].type && inputs[i].type.toLowerCase() === 'button' && 
             inputs[i].id && inputs[i].id.indexOf('diidIOSaveItem') >= 0) {
             okButtons.push(inputs[i]);
        }
    }
    return okButtons;
}

function AddValueToDropdown(oDropdown, text, value, optionnumber){

    var options = oDropdown.options;
    var option = document.createElement('OPTION');
    option.appendChild(document.createTextNode(text));
    option.setAttribute('value',value);
    if (typeof(optionnumber) == 'number' && options[optionnumber]) {
        oDropdown.insertBefore(option,options[optionnumber]);
    }
    else {
        oDropdown.appendChild(option);
    }
    oDropdown.options.selectedIndex = 0;
}

function WrapClickEvent(element, newFunction) {

    var clickFunc = element.onclick;
    element.onclick = function(event){
        if (newFunction()) {
            clickFunc();
        }
    };
}

function MyCustomExecuteFunction() {

    // find the dropdown
    var fieldTitle = 'Large Lookup Field';
    var dropdown = GetDropdownByTitle(fieldTitle);
    if (null === dropdown) {
        alert('Unable to get dropdown');
        return;
    }

    AddValueToDropdown(dropdown, '', '', 0);


    // add a custom validate function to the page

    var funcValidate = function() {
        if (0 === dropdown.selectedIndex) {
            alert("Please choose a value for " + fieldTitle + ".");
            // require a selection other than the first item (our blank value)
            return false;
        }
        return true;
    };

    var okButtons = GetOKButtons();

    for (var b = 0; b < okButtons.length; b++) {
        WrapClickEvent(okButtons[b], funcValidate);
    }
}

_spBodyOnLoadFunctionNames.push("MyCustomExecuteFunction");


</script>