Solved

Set Default Values in SharePoint List Edit View using JavaScript

Posted on 2016-11-08
5
85 Views
Last Modified: 2016-11-13
I want to set the values of 3 SharePoint list columns when it goes into edit mode using JavaScript in SharePoint Online.

I have a list in a popup form that looks like the below. When the user clicks the Edit button to go into edit mode, I want the EntryDateTime to be set to the current date/time, initials to be set to the initials of the current user, and Project to be set to the current project. How/where can I fire the JavaScript?

List
Fields to Populate
0
Comment
Question by:Michael Vasilevsky
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 82

Expert Comment

by:leakim971
ID: 41884684
could you post the two html corresponding to your screenshots pages?
0
 
LVL 10

Author Comment

by:Michael Vasilevsky
ID: 41884691
As you wish. These are SharePoint pages so there's a bunch of code :-/

See attached.
projectCommuncations.html
projectCommuncationsEdit.html
0
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 41884731
try this, this is not very nice but that should do the work at least for today date and project number :

<script>
$(document).on("click", "#Hero-WPQ2 a.ms-heroCommandLink:contains('edit')", function(evt) { 
	var t = setInterval(function() {
        if($("#spgridcontainer_WPQ2_leftpane_mainTable").length) {
        	clearInterval(t);
        	var d = formatDate(new Date());
        	$("#spgridcontainer_WPQ2_leftpane_mainTable tr:last td:eq(1)").text(d);

        	var name = ""; // can't be done with this method as the name don't appear on the page, only something like : Jim's Mega project
        	$("#spgridcontainer_WPQ2_leftpane_mainTable tr:last td:eq(2)").text(name);

        	var currentproject = "";
        	var arr = $("h1:contains('Project Communications: Project ')").text().match(/Project\sCommunications:\sProject\s(\d*)\s/);
        	if(arr && arr[1])
        		currentproject = arr[1];
        	$("#spgridcontainer_WPQ2_leftpane_mainTable tr:last td:eq(6)").text(currentproject)
        }
	},100);
});

function formatDate(dateVal) {
    var newDate = new Date(dateVal);

    var sMonth = padValue(newDate.getMonth() + 1);
    var sDay = padValue(newDate.getDate());
    var sYear = newDate.getFullYear();
    var sHour = newDate.getHours();
    var sMinute = padValue(newDate.getMinutes());
    var sAMPM = "AM";

    var iHourCheck = parseInt(sHour);

    if (iHourCheck > 12) {
        sAMPM = "PM";
        sHour = iHourCheck - 12;
    }
    else if (iHourCheck === 0) {
        sHour = "12";
    }

    sHour = padValue(sHour);

    return sMonth + "/" + sDay + "/" + sYear + " " + sHour + ":" + sMinute + " " + sAMPM;
}

function padValue(value) {
    return (value < 10) ? "0" + value : value;
}
</script>

Open in new window

0
 
LVL 10

Author Comment

by:Michael Vasilevsky
ID: 41885405
Wow, I was just expecting a push in the right direction but you solved it for me! Thanks so much - next time I'm paying!
0
 
LVL 10

Author Closing Comment

by:Michael Vasilevsky
ID: 41885406
Very good - thanks so much!
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
JavaScript Loan Calculator Error 3 34
Date on a table 16 33
What is the correct way to check for empty value in input field 2 16
HTML editor custom button 3 56
Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question