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;

Add item into SharePoint list using javascript

In this blog, we learn how to add item into SharePoint list using JavaScript.

Below function add to the content editor webpart or into your core .js file,and call on link click or button click.

<script type="text/javascript">
// Make sure the SharePoint script file 'sp.js' is loaded before your
// code runs.
SP.SOD.executeFunc('sp.js', 'SP.ClientContext', sharePointReady);
function createListItem()
{
    var clientContext = SP.ClientContext.get_current();
    var oList = clientContext.get_web().get_lists().getByTitle('InwardOutward');
    var itemCreateInfo = new SP.ListItemCreationInformation();
    this.oListItem = oList.addItem(itemCreateInfo);
   oListItem.set_item('Title', 'My New Item!');
   oListItem.update();
    clientContext.load(oListItem);
    clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
}

function onQuerySucceeded()
{
    alert('Item created: ' + oListItem.get_id());
 }
function onQueryFailed(sender, args)
{
    alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}
</script>

<a href="javascript:createListItem();"><img alt="Add" src="/Image_/add.png" border="0"/></a>

While using JavaScript to add item into list following error may occurred:

Request failed. List data validation failed.undefined

Error Message:

Request failed. List data validation failed.undefined

Problem Description:

I wrote JavaScript function to add item into SharePoint List but I getting below error shown in following figure.





I got above error because I kept column validation in my one of the Date Column,





I remove column level validation from my List, I wrote validation in JavaScript function only.





That’s all and finally my Item created using JavaScript…






Tuesday, 7 January 2014

Unable to get property get_current of undefined or null reference

Error Message:

Unable to get property 'get_current' of undefined or null reference.

Problem Description:

I wrote JavaScript function in my core .js file and calling from SharePoint list form.


SharePoint Form

Core .js file



In that function i.e."TravelRequestAdminDocumentReceived()" I am trying to get some information about the current context through the SharePoint JavaScript object model and am getting below error.




Solution:

This error is occurred because SP.js file not get loaded. To load SP.js file before our function we need to use following function

ExecuteOrDelayUntilScriptLoaded(TravelRequestAdminDocumentReceived, "sp.js"); 




Remark : After your custom function in ExecuteOrDelayUntilScriptLoaded don’t use brackets “()”.