Saturday, March 28, 2015

Calculate date from date picker inside InfoPath forms in SharePoint 2013

In this post we will discuss how to calculate date from a date picker inside a InfoPath forms in SharePoint 2013.

Recently I got a requirement to calculate some date based on a dropdown selected value. Like I have a dropdownlist which contains value like Yearly, half yearly, quarterly, months, week like that. And there is a date picker where user can select a date. If a user select a date from the date picker and then s/he select yearly from the dropdown then anther text should be populate with adding a year. So if user select 28-03-2015 then it should autopopulat 28-03-2016. Like that it should do for quarterly, monthly etc.

You may like to see:
- Format Date in InfoPath form in SharePoint 2013

- Alternative way to promote people picker in infopath form library in SharePoint 2013

- Deploy infopath form using PowerShell or STSADM command

We did not got any proper formula to do this inside the SharePoint infopath form. So we did that through client side code in the page. We have added a script editor webpart in the page and wrote the following code to do the calculation in the page.

Here ctl00_ctl29_g_0d2d6eff_f266_41bf_b14f_ce80bdc728ed_FormControl0_V1_I1_T11 is the id of the calendar control which user selecting.

<script type="text/javascript">
function test(){
var x = 12;
var CurrentDate = new Date();
CurrentDate.setMonth(CurrentDate.getMonth() + x);
var d1=document.getElementById("ctl00_ctl29_g_0d2d6eff_f266_41bf_b14f_ce80bdc728ed_FormControl0_V1_I1_T11").value;
var date = new Date(d1);
date.setMonth(date.getMonth() + x);
<input onclick="test()" type="button" value="Alert value in form control"/>

Friday, March 13, 2015

Get all items selected using JavaScript in SharePoint 2013 or SharePoint Online

In this post we will discuss how to get id of all the selected items from the SharePoint list using JavaScript. The same code will work for SharePoint Online also.

Just put the below code inside a script editor web part. Then on click on the button, it will display you id of all the items you have selected.

<input type='button' id='123' value='Click Me' onclick="getSelectedItems();"/>

<script language="javascript" type="text/javascript">
function getSelectedItems()
var ctx = SP.ClientContext.get_current();
var items = SP.ListOperation.Selection.getSelectedItems(ctx);
var myItems = '';
var i;
for (i in items){
    myItems = items[i].id;

Twitter Delicious Facebook Digg Favorites More