creating a new list item from a .htm form

Sep 15, 2014 at 1:37 PM
I can't get this below code to work for me when I click the submit button on my form does anyone have any suggestions as to what I am doing wrong?



var SP = new SharePointService();
var siteUrl = 'https://teams.bizerba.com/sites/technik/technischebereiche/industrie/BVS/default.aspx';
var clientContext = new SP.ClientContext(siteUrl);
var oList = clientContext.get_web().get_lists().getByTitle('BVS_Change_Request');

var itemCreateInfo = new SP.ListItemCreationInformation();
this.oListItem = oList.addItem(itemCreateInfo);

oListItem.set_item('Title', subject);   
oListItem.set_item('Name', name);
oListItem.set_item('Department', department);
oListItem.set_item('E-mail', email); 
oListItem.set_item('Is Requested', isRequested);
oListItem.set_item('Root Cause', rootCause);
oListItem.set_item('Material number/Version of the program', priority);
oListItem.set_item('Priority',version);
oListItem.set_item('Subject', subject);
oListItem.set_item('Detail Description', detailDescrip);

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());
}
Coordinator
Sep 15, 2014 at 2:04 PM
You're not using SPServices anywhere here, so this isn't really the right forum.

I'm still wondering why you're trying to create a new form from whole cloth when SharePoint gives you forms for "free".

M.
Sep 15, 2014 at 3:05 PM
I am using it I just didn't add it in there because it is all the way at the top of my code. Here is all my java I am using for my form. Like I said before I am doing what my boss told me to do. We are a World Wide Company he doesn't want everyone in the company to have access to the list that is why I have to do it this way.

<html>
<head>
<script language="javascript" type="text/javascript" src="http://portal.bizerba.com/Javascript/jquery-1.11.0.min.js"></script>
<script language="javascript" type="text/javascript" src="http://portal.bizerba.com/Javascript/jquery.SPServices-2014.01.min.js"></script>
<script language="JavaScript"> // Define a function for later use function print_todays_date( ) { var d = new Date( ); // Get today's date and time document.write(d.toLocaleString( )); // Insert it into the document } </script> <script language="JavaScript"> function validateForm() { var name = document.forms["myForm"]["name"].value; var department = document.forms["myForm"]["department"].value; var email = document.forms["myForm"]["email"].value; var changeRequest = document.getElementById("change").checked; var techRequest = document.getElementById("technical").checked; var extRequest = document.getElementById("extension").checked; var investRequest = document.getElementById("investigation").checked; var complaintCause = document.getElementById("complaint").checked; var marketCause = document.getElementById("market").checked; var cipCause = document.getElementById("cip").checked; var bugCause = document.getElementById("bug").checked; var legalCause = document.getElementById("legal").checked; var investCause = document.getElementById("invest").checked; var changeCause = document.getElementById("changefrom").checked; var priority = document.getElementById("dropdownID").value; var version = document.forms["myForm"]["number"].value; var subject = document.forms["myForm"]["subject"].value; var detailDescrip = document.forms["myForm"]["detail"].value; if (name==null || name=="") { alert("First name must be filled out"); return false; } if (department==null || department=="") { alert("Department name must be filled out"); return false; } if (email==null || email=="") { alert("E-mail must be filled out"); return false; } if (changeRequest == true){ var isRequested = document.getElementById("change").value; } else if (techRequest == true){ isRequested = document.getElementById("technical").value; } else if (extRequest == true){ isRequested = document.getElementById("extension").value; } else if (investRequest == true){ isRequested = document.getElementById("investigation").value; } else { alert("You must select a Is Requested"); return false; } if (complaintCause == true){ var rootCause = document.getElementById("complaint").value; } else if (marketCause == true){ rootCause = document.getElementById("market").value; } else if (cipCause == true){ rootCause = document.getElementById("cip").value; } else if (bugCause == true){ rootCause = document.getElementById("bug").value; } else if (legalCause == true){ rootCause = document.getElementById("legal").value; } else if (investCause == true){ rootCause = document.getElementById("invest").value; } else if (changeCause == true){ rootCause = document.getElementById("changefrom").value; } else { alert("You must select a Root Cause"); return false; } if (version==null || version=="") { alert("You must a material number or version of the program"); return false; } if (subject==null || subject=="") { alert("You must enter a subject"); return false; } if (detailDescrip==null || detailDescrip=="") { alert("You must enter a detailed description"); return false; } //var SP = new SharePointService(); var SP = $().SPServices(); var siteUrl = 'https://teams.bizerba.com/sites/technik/technischebereiche/industrie/BVS/'; console.log(siteURL); var clientContext = new SP.ClientContext(siteUrl); var oList = clientContext.get_web().get_lists().getByTitle('BVS_Change_Request'); var itemCreateInfo = new SP.ListItemCreationInformation(); this.oListItem = oList.addItem(itemCreateInfo); oListItem.set_item('Title', subject); oListItem.set_item('Name', name); oListItem.set_item('Department', department); oListItem.set_item('E-mail', email); oListItem.set_item('Is Requested', isRequested); oListItem.set_item('Root Cause', rootCause); oListItem.set_item('Material number/Version of the program', priority); oListItem.set_item('Priority',version); oListItem.set_item('Subject', subject); oListItem.set_item('Detail Description', detailDescrip); 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>
Coordinator
Sep 15, 2014 at 3:21 PM
You're referencing SPServices and you have the line var SP = $().SPServices();. That line is redefining the SP namespace, which is what's causing your problems. You're not actually using any SPServices-enabled functionality. Instead, you're using CSOM calls. You don't need SPServices in this code at all.

M..
Sep 15, 2014 at 4:20 PM
I still can't get it to work and I am sorry if I sounded like I was getting aggravated earlier I am just doing what I am told to do I am sure you understand.
Sep 16, 2014 at 12:16 PM
Edited Sep 16, 2014 at 12:17 PM
everyone in the company to have access to the list that is why I have to do it this way
That is confusing to me. Whomever is going to use your custom form to create/read/update/delete will need to have proper access on the list itself. Masking it with a custom form will not change that fact.

I'm reminded of a great quote that I came across yesterday. I hope you find value in it.
Your job is not to make your client happy, it’s to make them successful. If the former happens, so be it. But don’t conflate the two.
Cheers,
Matthew
Sep 16, 2014 at 12:28 PM
Yes it will because my form is an HTML page they go to and fill out the information and the submit button is linked to the list and creates a new list no one will see the list except the ones who have access to it.
Coordinator
Sep 16, 2014 at 12:53 PM
Matt is right: you many be confusing security and obscurity. Everyone you want to be able to create items in the list must have permissions to do so. If they know anything about SharePoint, they can hack the URL to find the list.

M.
Sep 16, 2014 at 3:43 PM
+1 to what Marc said. Plus it's real easy to go to this URL and see all of the lists of a site:
http://siteName/site/_layouts/viewlsts.aspx
This is a common SharePoint URL that you cannot prevent users from seeing manipulating the server.

Cheers,
Matthew
Sep 16, 2014 at 6:27 PM
I got it to work I was missing one line of code I don't need to create a custom list and they add it. I knew I could get it to work some way