0

In a webpart I added the following code :

<script src="http://code.jquery.com/jquery-1.8.3.js"></script>
<script src="http://code.jquery.com/ui/1.10.0/jquery-ui.js"></script>
<script src="js/index.js"></script>

<script type="text/javascript">
  document.title = "La salute della tiroide";
  document.getElementById('welcome').innerHTML='Benvenuto '+username[1]+',</br> se vuoi partecipare a uno dei due eventi, </br> scegli la data e registra la tua partecipazione:';
  document.getElementById('welcome').style.cssText="font-size:11px; margin-left: 30px;";     
</script>

<div class="container">
    <div id="welcome" style="color: #fff;" ></div>
    <div>
        <input type="checkbox" > 
        <p>Plase check the box to confirm presence</p>
    </div>
    <div>
        <img src="img/invia_button-158x36.png" width="158" height="36" border="0" id="submit" onclick="myFunction()"/></a>
    </div>
</div>

I want to create a script that when the users accesses the Form, after checking the box and hit send to be added in a SharePoint2013 list?

How can I do that? Any suggestions?

3
  • What you want to insert in SharePoint list? are there same fields available in your list?
    – Hardik
    Commented Jun 29, 2016 at 12:07
  • I want to add the users that are accessing the form. After they select the check-box and hit click on the button with id "submit" they are added to the list. Commented Jun 29, 2016 at 12:11
  • For example: I have an event on 7th of July and I want the users to confirm the presence at the event based on submitting of this form. Commented Jun 29, 2016 at 12:14

1 Answer 1

0

I have tried to create sample code to insert items in SharePoint list with your code.

Please check the myFunction function in javascript to add item.

<script src="http://code.jquery.com/jquery-1.8.3.js"></script>
<script src="http://code.jquery.com/ui/1.10.0/jquery-ui.js"></script>
<script src="js/index.js"></script>

<script type="text/javascript">
  document.title = "La salute della tiroide";
  document.getElementById('welcome').innerHTML='Benvenuto '+username[1]+',</br> se vuoi partecipare a uno dei due eventi, </br> scegli la data e registra la tua partecipazione:';
  document.getElementById('welcome').style.cssText="font-size:11px; margin-left: 30px;";    

function myFunction() {
    var CheckedStatus;
//Retrieve the chkConfirm and check if it has some text.
var chkConfirm = document.getElementById("chkConfirm");
if (chkConfirm.checked) {
CheckedStatus = true;
}
else
{
    CheckedStatus = true;
}
//Retrieve the clientContext
var clientContext = SP.ClientContext.get_current();
//Retrieve the current website
var webSite = clientContext.get_web();
//Retrieve the lists
var lists = webSite.get_lists();
//Get the Cars List
var carsList = lists.getByTitle("Your List Name Here");
//Create new IteamCreationInformation
var itemCreationInfo = new SP.ListItemCreationInformation();
//the addItem takes the itemCreationInformation object
//and returns the listItem
var listItem = carsList.addItem(itemCreationInfo);
//Set the title of the list Item
//to the value in the chkConfirm
listItem.set_item("Your column with yes/no option", CheckedStatus);

////Add the user in another column by same method listItem.set_item
////Add other fields in different columns by same method listItem.set_item

//Call Update the listItem
listItem.update();
//Finally execute the operation
//pass to it a 2 callBack methods OnSucceed and OnFailure
clientContext.executeQueryAsync(
Function.createDelegate(this,this.Succeeded)
,Function.createDelegate(this,this.Failed));
}



function Succeeded() {
alert("Succeeded");
}
function Failed(sender,args) {
alert("fail");
}  
</script>

<div class="container">
    <div id="welcome" style="color: #fff;" ></div>
    <div>
        <input type="checkbox" id="chkConfirm"> 
        <p>Plase check the box to confirm presence</p>
    </div>
    <div>
        <img src="img/invia_button-158x36.png" width="158" height="36" border="0" id="submit" onclick="myFunction()"/></a>
    </div>
</div>

To add current user please check below given reference URL.

Adding the current user to a list item by using SharePoint 2013 CSOM

To know all CRUD operations using JavaScript for SharePoint, Kindly refer below link.

How to: Create, Update, and Delete List Items Using JavaScript

NOTE :- You should atleast try to search on the internet first than after you can ask question here for your queries.

8
  • thank you very much for your answer. Ad this point is failing but I am sure I will make it work. Commented Jun 29, 2016 at 13:13
  • Did you get it working?
    – Hardik
    Commented Jun 29, 2016 at 13:14
  • I will let you know if I don't succeed. thanks also for the tutorials. Commented Jun 29, 2016 at 13:15
  • Hello Hardik, I've succeded adding the user to the list but now I need the form to identify my name and not let me register again. Any advise? or tutorials? Commented Jul 1, 2016 at 9:16
  • You need apply query with the user field & check items are available or not. take the output as Boolean at true or false, If it is there than Boolean will be true, than you can check the condition before the carsList.addItem.
    – Hardik
    Commented Jul 1, 2016 at 9:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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