none
Rename the document name field in a document library RRS feed

  • Question

  • Dear all,

    I am using below code to update the document name in editform.aspx.I have pasted below code by adidng scripteditor webpart in editform.aspx page but the document name is not changing.Please help me.thanks in advance.

    <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"> 
    </script>
    <script language="javascript" type="text/javascript"> 
    var listName='testlibrray';
    var id;
    var  clientContext ;
    $(document).ready(function() 

    $('input[value=Save]').click(function() {       
           a
          SP.SOD.executeFunc('sp.js', 'SP.ClientContext', updatedoclib);
          });  


    id = GetParameterValues('ID');  
          
            function GetParameterValues(param) {  
                var url = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');  
                for (var i = 0; i < url.length; i++) {  
                    var urlparam = url[i].split('=');  
                    if (urlparam[0] == param) {  
                        return urlparam[1];  
                    }  
                }  
            }  
    alert(id);


    }); 


    function updatedoclib() {
        clientContext = new SP.ClientContext.get_current();
        var oList = clientContext.get_web().get_lists().getByTitle(listName);
            
        var camlQuery = new SP.CamlQuery();
        camlQuery.set_viewXml(
            '<View><Query><Where><Eq><FieldRef Name=\'ID\' />' + 
            '<Value Type=\'Text\'>'+id+'</Value></Eq></Where></Query></View>'
        );
        this.collListItem = oList.getItems(camlQuery);
            
        clientContext.load(collListItem);

        clientContext.executeQueryAsync(
            Function.createDelegate(this, this.onQuerySucceededRetrieve), 
            Function.createDelegate(this, this.onQueryFailed)
        ); 
    }

    function onQuerySucceededRetrieve(sender, args) {
        var listItemInfo = '';
        var listItemEnumerator = collListItem.getEnumerator();
            
        while (listItemEnumerator.moveNext()) {
            var oListItem = listItemEnumerator.get_current();
            listItemInfo += '\nID: ' + oListItem.get_id() + 
                '\nYear: ' + oListItem.get_item('Year') + 
                  '\ntestid: ' + oListItem.get_item('Tid'); 
                      var year=oListItem.get_item('Year')
                      var tid=oListItem.get_item('Tid')
                      oListItem.set_item("FileLeafRef", "year-tid");
                      oListItem.update();
                    
                
        }

        alert(listItemInfo.toString());
    }

    function onQueryFailed(sender, args) {
        alert('Request failed. ' + args.get_message() + 
            '\n' + args.get_stackTrace());
    }


    </script> 


    Thursday, July 5, 2018 12:51 PM

Answers

  • Hi,

    It seems you are trying to update current item through edit form which will conflicts. Can you please help me to understand the logic for filename name? How should it work?

    You can try to get current item year and tid and set it directly to name field something like below. You don't need extra calls.

    $("input[title='Name Required Field']").val("year-tid");

    Regards.

    • Marked as answer by Praveenrangu Friday, July 6, 2018 10:26 AM
    Friday, July 6, 2018 4:45 AM
  • Hi,

    If you want to rename filename in EditForm.aspx, you can just use JavaScript script.

    Overwrite PreSaveAction function on list form and set the field value.

    Edit the EditForm.aspx, type the following scrip to the Script Editor web part:

    <script src="http://code.jquery.com/jquery-1.10.2.min.js" type="text/javascript"></script>
    <script language="javascript" type="text/javascript"> 
    function PreSaveItem(){
    var button = $('input[name$="SaveItem"]');  
    var year = $('input[id$="Year"]').val(); 
    var tid = $('input[id$="Tid"]').val(); 
    $('input[title="Name"]').val(year+"-"+tid);
    return true;    
    }
    </script>


    Best regards,

    Linda Zhang


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.


    Click here to learn more. Visit the dedicated forum to share, explore and talk to experts about Microsoft Teams.

    • Marked as answer by Praveenrangu Friday, July 6, 2018 10:26 AM
    Friday, July 6, 2018 4:53 AM
    Moderator

All replies

  • Hi,

    It seems you are trying to update current item through edit form which will conflicts. Can you please help me to understand the logic for filename name? How should it work?

    You can try to get current item year and tid and set it directly to name field something like below. You don't need extra calls.

    $("input[title='Name Required Field']").val("year-tid");

    Regards.

    • Marked as answer by Praveenrangu Friday, July 6, 2018 10:26 AM
    Friday, July 6, 2018 4:45 AM
  • Hi,

    If you want to rename filename in EditForm.aspx, you can just use JavaScript script.

    Overwrite PreSaveAction function on list form and set the field value.

    Edit the EditForm.aspx, type the following scrip to the Script Editor web part:

    <script src="http://code.jquery.com/jquery-1.10.2.min.js" type="text/javascript"></script>
    <script language="javascript" type="text/javascript"> 
    function PreSaveItem(){
    var button = $('input[name$="SaveItem"]');  
    var year = $('input[id$="Year"]').val(); 
    var tid = $('input[id$="Tid"]').val(); 
    $('input[title="Name"]').val(year+"-"+tid);
    return true;    
    }
    </script>


    Best regards,

    Linda Zhang


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.


    Click here to learn more. Visit the dedicated forum to share, explore and talk to experts about Microsoft Teams.

    • Marked as answer by Praveenrangu Friday, July 6, 2018 10:26 AM
    Friday, July 6, 2018 4:53 AM
    Moderator