1

I have two scripts that work on their own, I found them and got them to work by searching online, but I'm not sure how to combine them. The first script will replace an HTML tag with the id logUser with the current user's name:

<script type="text/javascript">
 SP.SOD.executeFunc('sp.js', 'SP.ClientContext', init);
 ExecuteOrDelayUntilScriptLoaded(init,"sp.js");

 var currentUser;

 function init(){
  this.clientContext = new SP.ClientContext.get_current();
  this.oWeb = clientContext.get_web();
  currentUser = this.oWeb.get_currentUser();
  this.clientContext.load(currentUser);
  this.clientContext.executeQueryAsync(Function.createDelegate(this,this.onQuerySucceeded),Function.createDelegate(this,this.onQueryFailed));
 }

 function onQuerySucceeded(){
  document.getElementById("logUser").innerHTML = currentUser.get_title();
 }

 function onQueryFailed(sender, args){
  alert('Request failed. \nError: ' + args.get_message() + '\nStackTrace: ' + args.get_stackTrace());
 }
</script>

I use this script to personalize a message on the first page of a SharePoint survey.

I also have found the following script that will add an item to a list upon entering each consecutive page of the survey:

<script type="text/javascript">
SP.SOD.executeFunc('sp.js', 'SP.ClientContext', createListItem);
ExecuteOrDelayUntilScriptLoaded(createListItem,"sp.js");

var siteUrl = 'https://mysiteurl/';

function createListItem(){
 var clientContext = new SP.ClientContext(siteUrl);
 var oList = clientContext.get_web().get_lists().getByTitle('Survey Participants');
 var itemCreateInfo = new SP.ListItemCreationInformation();
 this.oListItem = oList.addItem(itemCreateInfo);
 oListItem.set_item('Title', USERNAME HERE PLEASE);
 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>

I am wondering how I can combine these two so that when I am creating a new list item, the title of the item is the username. A step further that would be awesome would be if there was already an item with that username, it wouldn't add a duplicate.

Any help would be greatly appreciated.

0

Through trial and error with different combinations of the two scripts above, I was able to get it working. My final CEWP has the following source code:

<script type="text/javascript">

  SP.SOD.executeFunc('sp.js', 'SP.ClientContext', init);

  ExecuteOrDelayUntilScriptLoaded(init,"sp.js");

  var currentUser;
  var currentUserName;

  function init(){
   this.clientContext = new SP.ClientContext.get_current();
   this.oWeb = clientContext.get_web();
   currentUser = this.oWeb.get_currentUser();
   this.clientContext.load(currentUser);
   this.clientContext.executeQueryAsync(Function.createDelegate(this,this.onQuerySucceeded1),Function.createDelegate(this,this.onQueryFailed1));
  }

  function onQuerySucceeded1(){

   ExecuteOrDelayUntilScriptLoaded(createListItem,"sp.js");

   var siteUrl = 'my sharepoint site url';

   function createListItem(){
    var clientContext = new SP.ClientContext(siteUrl);
    var oList = clientContext.get_web().get_lists().getByTitle('Survey Participants');
    var itemCreateInfo = new SP.ListItemCreationInformation();
    this.oListItem = oList.addItem(itemCreateInfo);
    oListItem.set_item('Title', currentUser.get_title());
    oListItem.update();
    clientContext.load(oListItem);
    clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
   }

   function onQuerySucceeded(){
   }

   function onQueryFailed(sender, args){
   }

  }

  function onQueryFailed1(sender, args){
  }
</script>

At this point, I can live with the results, but if anyone can help I would still definitely appreciate being able to:

  1. Write this code the correct way vs whatever hob cobbled way I've done.
  2. Only add the username to the separated list if it doesn't already exist (prevent duplicates).
  • Just some background too. This will give me a list of everyone who starts my survey. That way I can compare that list to the list of completed responses and find out who has an incomplete survey response that isn't viewable. – Tim Feb 28 '17 at 18:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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