locked
Copy item title to the another list on click of list item hyperlink RRS feed

  • Question

  • Hi All,

    I have a SharePoint 2013 list 'Training', having 2 columns 'Course title' and 'Register' (hyperlink type column). Now the requirement is to copy the title and user who clicked the hyperlink to another list.

    Please help me on this.


    arun singh

    Monday, November 16, 2015 12:16 PM

Answers

  • Hi Arun,

    I recommend to use JSLink to achieve this goal.

    Here is a code demo for your reference:

    (function () {
        var overrideCtx = {};
        overrideCtx.Templates = {};
    
        overrideCtx.Templates.Fields = {
            'Register': { 'View' : CopyItem }
        };
        SPClientTemplates.TemplateManager.RegisterTemplateOverrides(overrideCtx);
    })();
    
    function CopyItem(ctx){
        var value =ctx.CurrentItem["Register"];
        var title=ctx.CurrentItem["Title"];//change the field to the internal name of your field
        var html="";
        if(value!="")
        {
            var html= '<a id="CopyItem" href="#" onclick="copy(\''+title+'\')">'+value+'</div>';
        }
        return html;
    }
    
    function copy(title) {
        var oListItem;
        var context = new SP.ClientContext("http://sp");//change the URL to your site URL
        var oList = context.get_web().get_lists().getByTitle('List2');//change the List name to the list that you want to copy item to
        var userid = _spPageContextInfo.userId;
        console.log(userid);
        var  user = new SP.FieldUserValue();
        user = userid; // this is the ID for User or Group in SharePoint        
        var itemCreateInfo = new SP.ListItemCreationInformation();
        oListItem = oList.addItem(itemCreateInfo);         
        oListItem.set_item('Title', title);
        oListItem.set_item('user', user);  
        oListItem.update();
        context.load(oListItem);       
        context.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed)); 
    }
    
    function onQuerySucceeded() {
        alert('Item created');
    }
    
    function onQueryFailed(sender, args) {
        alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
    }
    

    About how to use JSLink, please refer to the link below:

    http://www.rbradbrook.co.uk/blog/2013/04/14/introduction-to-client-side-rendering-in-sharepoint-2013/

    Thanks,

    Victoria


    TechNet Community Support
    Please remember to mark the replies as answers if they help, and unmark the answers if they provide no help. If you have feedback for TechNet Support, contact tnmff@microsoft.com.

    • Marked as answer by Sonu888 Thursday, November 19, 2015 6:24 AM
    Wednesday, November 18, 2015 3:08 AM

All replies

  • Hi Arun,

    On click of Register button you can call javascript function that posts the title and current user details to another list.

    <a href='Javascript:doRegistration()' > Register</a>
    
    <script type='text/javascript'>
    
    function doRegistration()
    
    {
    
    // write script to get title respective to the register button
    
    // wirte script to get current user
    
    // write script to post data to list
    
    }
    
    </script>



    Regards,
    Gunesh v

    Monday, November 16, 2015 12:25 PM
  • Hi Gunesh,

    'Register' is just a hyperlink column not a button. How the JavaScript function will get called on click of hyperlink.


    arun singh

    Monday, November 16, 2015 1:21 PM
  • Hi Arun,

    I recommend to use JSLink to achieve this goal.

    Here is a code demo for your reference:

    (function () {
        var overrideCtx = {};
        overrideCtx.Templates = {};
    
        overrideCtx.Templates.Fields = {
            'Register': { 'View' : CopyItem }
        };
        SPClientTemplates.TemplateManager.RegisterTemplateOverrides(overrideCtx);
    })();
    
    function CopyItem(ctx){
        var value =ctx.CurrentItem["Register"];
        var title=ctx.CurrentItem["Title"];//change the field to the internal name of your field
        var html="";
        if(value!="")
        {
            var html= '<a id="CopyItem" href="#" onclick="copy(\''+title+'\')">'+value+'</div>';
        }
        return html;
    }
    
    function copy(title) {
        var oListItem;
        var context = new SP.ClientContext("http://sp");//change the URL to your site URL
        var oList = context.get_web().get_lists().getByTitle('List2');//change the List name to the list that you want to copy item to
        var userid = _spPageContextInfo.userId;
        console.log(userid);
        var  user = new SP.FieldUserValue();
        user = userid; // this is the ID for User or Group in SharePoint        
        var itemCreateInfo = new SP.ListItemCreationInformation();
        oListItem = oList.addItem(itemCreateInfo);         
        oListItem.set_item('Title', title);
        oListItem.set_item('user', user);  
        oListItem.update();
        context.load(oListItem);       
        context.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed)); 
    }
    
    function onQuerySucceeded() {
        alert('Item created');
    }
    
    function onQueryFailed(sender, args) {
        alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
    }
    

    About how to use JSLink, please refer to the link below:

    http://www.rbradbrook.co.uk/blog/2013/04/14/introduction-to-client-side-rendering-in-sharepoint-2013/

    Thanks,

    Victoria


    TechNet Community Support
    Please remember to mark the replies as answers if they help, and unmark the answers if they provide no help. If you have feedback for TechNet Support, contact tnmff@microsoft.com.

    • Marked as answer by Sonu888 Thursday, November 19, 2015 6:24 AM
    Wednesday, November 18, 2015 3:08 AM