locked
Copy data from current item, current list to a new item on another list using Javascript RRS feed

  • Question

  • Hello everyone,

    The objective of this code is to copy the data entered in the current Item to a New item in List 'MasterList'. i.e. I am copying the current item to a new item in a list located at the root site. 

    The issue I feel is that I am not accessing the entries that the user entered in the current Item properly. Please see the underlined code below where I think the problem is stemming. 

    Please let me know if there are any suggestions on fixing the code. Also, if there is simpler approach to achieve this would be great help.

    • Added a control editor webpart on NewForm.aspx page of a List 'Tasks'
    • Here is the Javascript code that is added
    <SharePoint:ScriptLink ID="ScriptLink1" Name="SP.js" runat="server" OnDemand="true" Localizable="false" />
    <SharePoint:FormDigest ID="FormDigest1" runat="server" />
    <script type="text/javascript">
    
    var siteUrl = '/sites/ABC';
    
    function createListItem() {
    
        var clientContext = new SP.ClientContext(siteUrl);
        var oList = clientContext.get_web().get_lists().getByTitle('MasterList');
        
        var itemCreateInfo = new SP.ListItemCreationInformation();
        this.oListItem = oList.addItem(itemCreateInfo);
        
        var vTitle = document.getTagFromIdentifierAndTitle(“INPUT”,"TextField", title);
        oListItem.set_item('Title', vTitle);
        oListItem.set_item('Body', 'Hello World!');
            
        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());
    }
    
    function PreSaveAction() {
        alert("here");
    	ExecuteOrDelayUntilScriptLoaded(createListItem, "sp.js");
    	createListItem();
    }
    
    function getTagFromIdentifierAndTitle(tagName, identifier, title)
    {
        var len = identifier.length;
        var tags = document.getElementsByTagName(tagName);
        for (var i=0; i < tags.length; i++)
       {
          var tempString = tags[i].id;
          if (tags[i].title == title && (identifier == "" || tempString.indexOf(identifier) == tempString.length - len))
         {
             return tags[i];
         }
      }
      return null;
    }
    
    </script>

    Wednesday, July 4, 2012 10:08 PM

Answers

  • I found a solution to this. I was referencing it wrong, should have been using 'this.' instead of 'document.'.

    Thanks much for quick response. 


    Saturday, July 7, 2012 8:36 PM

All replies

  • Hi

    Looks like you don't load the source list you are working on. Try the following:   after the

    var oList = clientContext.get_web().get_lists().getByTitle('MasterList');
    

    add the following line...

    clientContext.Load(oList);

    And move this line: 

    clientContext.load(oListItem);
    

    to the line below the

     this.oListItem = oList.addItem(itemCreateInfo);
    



    Kind Regards Bjoern
    Blog

    Wednesday, July 4, 2012 11:00 PM
  • Hello Bjoern

    Thanks for the input. I tested that, but it doesn't seem to be the issue. 

    The problem is on the following lines:

    var vTitle = document.getTagFromIdentifierAndTitle(“INPUT”,"TextField", title); oListItem.set_item('Title', vTitle);

    I am trying to access the current list item 'Title', which was entered. I think I am accessing something wrong.

    Can you please verify? 


    Thursday, July 5, 2012 12:14 AM
  • I'm not too familiar with the getTagFromIdentifierAndTitle function, but is it part of the DOM ?  I ask since you are prefixing it with document which is not defined in your code anywhere, and I haven't seen any other samples where that function is called the way you do here..


    Kind Regards Bjoern
    Blog



    Thursday, July 5, 2012 12:24 AM
  • I found a solution to this. I was referencing it wrong, should have been using 'this.' instead of 'document.'.

    Thanks much for quick response. 


    Saturday, July 7, 2012 8:36 PM
  • Sounds good, glad to hear it was fixed..

    Kind Regards Bjoern
    Blog

    Saturday, July 7, 2012 8:43 PM