Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I have a month column (a choice column) in my document library. Its format is, for example, "Jan 2013".

I have the following requirements:

  1. The month dropdown should always show only the previous 6 months relative to the current month.

    For example, if the current month is March 2013, then only the months starting from October 2012 to March 2013 should be populated in the dropdown

  2. There is another column in my document library called "date", which is in a date format. It should not be shown in the default view. The date field should automatically be set as the 1st date of the month selected from the month column.

    For example: If the month selected is March 2013, the date should be 1/3/2013.

share|improve this question

migrated from stackoverflow.com Mar 8 '13 at 13:56

This question came from our site for professional and enthusiast programmers.

2 Answers 2

up vote 1 down vote accepted

Sorry for late response.. You're right, there is no "NewForm.aspx" on document libraries. So what can you do is adding the following javascript to the "EditForm.aspx" file of the related document library. You've to edit "EditForm.aspx" file in advanced mode by using SharePoint Designer. Add these code after PlaceHolderPageTitleInTitleArea tag.

In your document library one single line of text field (called as Month in my code) and a Datetime field (called as Day in my code).

This javascript first hides the textfield for Month field and shows a dropdown list that includes last three months.

Probably you've to optimize the javascript code :) But I tried it in my environment, it works.

<script type="text/javascript">

_spBodyOnLoadFunctionNames.push("addItemsToCombo");

var month = new Array();
month[0] = "January";
month[1] = "February";
month[2] = "March";
month[3] = "April";
month[4] = "May";
month[5] = "June";
month[6] = "July";
month[7] = "August";
month[8] = "September";
month[9] = "October";
month[10] = "November";
month[11] = "December";

function addItemsToCombo()
{
    var today = new Date();

    var inputs = document.getElementsByTagName("input");
    for (i=0; i<inputs .length; i++)
    {
        if (inputs[i].getAttribute("Title")=="Month")
        {
            inputs[i].setAttribute("style","display:none;");
            var select = document.createElement("select");
            select.setAttribute("name","tempMonth");
            select.setAttribute("id","tempMonth");
            var currentMonth = today.getMonth();
            var currentYear = today.getFullYear();
            var option = document.createElement("option");
            option.innerHTML = "Select";
            option.setAttribute("value", "");
            select.appendChild(option);
            for(j=0; j<6; j++)
            {

                var option = document.createElement("option");
                if (currentMonth<0)
                {
                    option.innerHTML = month[currentMonth+12] + " " + (currentYear -1);
                    option.setAttribute("value", currentMonth +12 + " " + (currentYear -1));

                }
                else    
                {
                    option.innerHTML = month[currentMonth] + " " + currentYear;
                    option.setAttribute("value", currentMonth + " " + currentYear);
                }
                select.appendChild(option);
                currentMonth = currentMonth-1;
            }
            select.onchange = function() {monthChange();};
            inputs[i].parentElement.appendChild(select);
        }

    }

}

function monthChange()
{
    var combo = document.getElementById("tempMonth");
    var inputs = document.getElementsByTagName("input");
    for (i=0; i<inputs .length; i++)
    {
        if (inputs[i].getAttribute("Title")=="Month")
        {
            inputs[i].value = combo.options[combo.selectedIndex].text;
        }
        if (inputs[i].getAttribute("Title")=="Day")
        {
            var selectedMonthYear = combo.value.split(" ");
            var mm = parseInt(selectedMonthYear[0]) + 1;
            var yyyy = selectedMonthYear[1];
            if (mm<10)
            {
                mm = "0"+mm;
            }
            var firstOfMonth = "01/"+mm+"/"+yyyy;
            inputs[i].value = firstOfMonth;
            inputs[i].setAttribute("disabled","disabled");
        }
    }
}

</script> 
share|improve this answer
    
Thank u So much Kerem...For ur help and support...But I am Facing a couple of issues.. 1.When I am Placing the code inside PlaceHolderPageTitleInTitleArea the month column is populating fine..but in th month column i also need the current month to be populated. 2. When i am selecting an element from the month column.. message box pops out saying "objectHTMLSelectElement".. 3.The Date field is showing the 1stday of current month...But I want It to be 1st day of selected month. Thanks in Advance... –  nivi Mar 14 '13 at 11:59
    
Thank u So much Kerem...For ur help and support...But I am Facing a couple of issues.. 1.When I am Placing the code inside PlaceHolderPageTitleInTitleArea the month column is populating fine..but in the month column i also need the current month to be populated. 2. When i am selecting an element from the month column.. message box pops out saying "objectHTMLSelectElement".. 3.The Date field is showing the 1stday of current month...But I want It to be 1st day of selected month. Thanks in Advance...:) :) :) :) –  nivi Mar 14 '13 at 12:01
    
Please Give Me the Solution Soon..Its Really Urgent...Thankss..:) –  nivi Mar 14 '13 at 12:46
    
Code is updated. Now it should fit you needs :) –  Kerem Mar 15 '13 at 9:56
    
Thanks A lot kerem...You Are an angel....You Made My Day..Thanks A ton.....:):):) –  nivi Mar 15 '13 at 12:20

I think it's not possible by default SharePoint columns. Here are the possible solutions:

  • In the Page_Load event of the NewForm.aspx you can write a code that changes or inserts the items in the combobox.
  • You can write a javascript in NewForm.aspx that uses DOM to insert new items to the combobox.

You can use a javascript code like this: http://keremozen.com/wp-content/uploads/2013/03/addItemToCombo.js

Hope these help

share|improve this answer
    
Kerem ..Thanks A lot For your solution..But Can u Please Elaborate On the second solution you gave ...about Javascript?? –  nivi Mar 7 '13 at 11:58
    
I am totally new To Javascript..So have absolutely no idea about it –  nivi Mar 7 '13 at 12:00
    
I added a sample js file to my answer. It should work with one or two modifications. You have to use this code in NewForm.aspx file of your document library. –  Kerem Mar 7 '13 at 14:38
    
Thanks A Lot For Your Help!! –  nivi Mar 11 '13 at 6:40
    
But I am Working On a Document Library ..Not A List ...So NewForm.aspx is not available by default..plus my requirement also states that the 'date' column field should automatically be populated with the 1st day of the month selected from the month column..I am Very Confused...Please Help –  nivi Mar 11 '13 at 6:58

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.