Answered Show current month in newform.aspx base on system time

  • domingo, 20 de mayo de 2012 3:17
     
     

    Hi

    In one of my sharepoint list, I have choice field that show months. I want to when user open newform.aspx this choice field show current month base on system time. How I can do it?

    Thanks.

Todas las respuestas

  • domingo, 20 de mayo de 2012 4:51
     
     Respuesta propuesta
    You could use some javascript on the page to get the Month value of the current date. then set the value of the choicce field = to that value.
    • Propuesto como respuesta Aviw_ domingo, 20 de mayo de 2012 6:58
    •  
  • domingo, 20 de mayo de 2012 5:04
     
     

    Can you help me what javascript code should I use? Can you explain more or give me some helpful link or exact code?

    thanks.

  • domingo, 20 de mayo de 2012 6:57
     
      Tiene código

    Hello!

    Add Content Editor Web Part and add following code:

    <div>
      <script>
        var monthNames = "January", "February", "March", "April", "May", "June",
        "July", "August", "September", "October", "November", "December" ];
        var month = (new Date()).getMonth();
        document.write("The current month is " + monthNames[month]);
      </script>
    </div>

    My contributions: SharePoint 2010 Solution Installer
    SharePoint How-To: Create WebPart Page programmatically

  • lunes, 21 de mayo de 2012 3:31
     
     

    Hi Aviw,

    In choice field I add January to December as a month, also add your JavaScript code in Content Editor WebPart,but when i add new item I did not see for example “The current month is May”.

    I want to when newform.aspx appears,base on system time load appropriate month in choice field automatically. 

  • lunes, 21 de mayo de 2012 6:18
     
     

    Open the Form in Infopath and add Control to it.


    Ashish Mishra MCTS, MCPD in SharePoint 2010

  • lunes, 21 de mayo de 2012 7:03
     
      Tiene código

    Go to edit new form page:

    and add new Content Editor Web Part.

    Add the following script to it:

    $(document).ready(function(){
      var monthNames = "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" ];
      var month = (new Date()).getMonth();
      $("input[title='YourFieldTitle']").val(monthNames[month]);
    });

    Also ensure field title (in script) and link to jQuery library.

    My contributions: SharePoint 2010 Solution Installer
    SharePoint How-To: Create WebPart Page programmatically


  • martes, 22 de mayo de 2012 3:21
     
     

    Thanks Aviw,

    What’s your mean about link to jQuery library? I’m beginner to use java script code.can you explain more.

    Thanks a lot.

  • martes, 22 de mayo de 2012 7:11
     
      Tiene código

    1. Download jQuery library from here.

    2. Put it to this folder: "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\LAYOUTS\"

    3. Add link to library before your script: "<script type="text/javascript" src="/_layouts/jquery-1.7.2.min.js"></script>"

    Your result script will be something like this:

    <script type="text/javascript" src="/_layouts/jquery-1.7.2.min.js"></script>
    <script type="text/javascript">
    $(document).ready(function(){
      var monthNames = "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" ];
      var month = (new Date()).getMonth();
      $("input[title='YourFieldTitle']").val(monthNames[month]);
    });
    </script>
    Also look here about the ways how to integrate jQuery library to your site. (If your are going to use it in the sequel)

    My contributions: SharePoint 2010 Solution Installer

  • domingo, 17 de junio de 2012 16:01
     
     

    Aviw,

    I set this settings step by step, but it does not work!

    1-     In my choice field add month name: January, February, March, April, May, June, July, August, September, October, November, December. Without  Default Value.

    2-     Download jQuery library from your link.

    3-     Put it to "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\LAYOUTS\" .

    4-     In NewForm.aspx page go to site settings>Edit Page and add Content Editor Web Part. And in Edit HTML Source, add below code:

    <script type="text/javascript" src="/_layouts/jquery-1.7.2.min.js"></script>

    <script type="text/javascript">

    $(document).ready(function(){

      var monthNames = "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" ];

      var month = (new Date()).getMonth();

      $("input[title='Month']").val(monthNames[month]);

    });

    </script>

    Finally save this settings by click on  "stop editing" button. Now when I want to add new item , I don’t see current month in my choice field.

    What’s wrong?!


    • Editado luckstar domingo, 17 de junio de 2012 16:01
    •  
  • domingo, 17 de junio de 2012 16:47
     
      Tiene código

    Hello!

    $("input[title='Month']")

    Here Month is the title of your list column. Set your value.


    Also try to get access to jQuery file by going by url http://server/_layouts/jquery-1.7.2.min.js


    My contributions: SharePoint 2010 Solution Installer

  • lunes, 18 de junio de 2012 6:19
     
     

    Aviw,

    In $("input[title='Month']") , ‘Month’ is my list column name.

    Also change code to address jQuery file by servername(http://itc-sppr) such this:

    <script src="http://itc-sppr/_layouts/jquery-1.7.2.min.js" type="text/javascript"></script>

    But it dosen’t work yet L

  • lunes, 18 de junio de 2012 6:59
     
     Respondida
    Try to debug this code in Internet Explorer Developer Panel (press F12 in IE and go to Script tab).

    My contributions: SharePoint 2010 Solution Installer