none
How to update the Existing item details in Document library using Rest API /Jquery in Sp 2013? RRS feed

  • Question

  • My requirement:

    I have a document library with some junk files.In one list after all approvals this junk file is replaced with some other file based on title/ID column.

    Is it possible to update the library with Title column.

    Let me know how to achieve this by using Rest API/Jquery in Share point 2013?



    Tuesday, January 24, 2017 10:27 AM

Answers

  • Hi,

    Please check and modify the code below(change listName and itemId), then add the code into a content editor web part in document library view page.

    <script src="//code.jquery.com/jquery-3.1.0.min.js" type="text/javascript"></script>
    <script type="text/javascript">
    // occurs when a user clicks the update button
    function Update() {
        var listName = "DL";   
        var itemId = "1"; // Update Item Id here
        var titleVal = "New Updated Title";
        UpdateListItem(listName,itemId,titleVal);
    }
    function UpdateListItem(listName,itemId,titleVal){
    	var itemType = GetItemTypeForListName(listName);
        var item = {
            "__metadata": { "type": itemType },
            "Title": titleVal    
        };
        
        $.ajax({
            url: _spPageContextInfo.siteAbsoluteUrl + "/_api/web/lists/getbytitle('" + listName + "')/items("+itemId+")",
            type: "POST",
            contentType: "application/json;odata=verbose",
            data: JSON.stringify(item),
            headers: {
                "Accept": "application/json;odata=verbose",
                "X-RequestDigest": $("#__REQUESTDIGEST").val(),
                "X-HTTP-Method": "MERGE",
                "If-Match": "*"
            },
            success: function (data) {
                alert('Success');
                window.location.href=window.location.href;
            },
            error: function (data) {
                alert("Error");
            }
        });
    }
    // Get List Item Type metadata
    function GetItemTypeForListName(name) {
        return "SP.Data." + name.charAt(0).toUpperCase() + name.split(" ").join("").slice(1) + "Item";
    }  
    </script>
    <input type="button" value="Update" onclick="Update()"/>

    More information:

    SharePoint 2013: Working with REST API using jQuery Ajax

    https://social.technet.microsoft.com/wiki/contents/articles/31995.sharepoint-2013-working-with-rest-api-using-jquery-ajax.aspx

    SharePoint 2013 – CRUD on List Items Using REST Services & jQuery

    https://www.plusconsulting.com/blog/2013/05/crud-on-list-items-using-rest-services-jquery/

    Best Regards,

    Dennis


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

    Wednesday, January 25, 2017 7:55 AM
    Moderator
  • Hi,

    If you want to update file name, please change below

      var item = {
            "__metadata": { "type": itemType },
            "Title": titleVal    
        };
    

    to

      var item = {
            "__metadata": { "type": itemType },
            "FileLeafRef": titleVal    
        };
    

    Best Regards,

    Dennis


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

    Monday, January 30, 2017 1:57 AM
    Moderator

All replies

  • You can update library title, file name column using Rest + JQuery in SharePoint 2013.

    There is  SPService wrapper for easy to use the process on CodePlex.


    Thanks, Ashish | Please mark a post helpful/answer if it is helpful or answer your query.


    Wednesday, January 25, 2017 1:10 AM
  • HI Ashish,

    Can you please elaborate the above. Provide some idea how to implement this step by step.

    What is <g> and inside tags

    code? 

    Wednesday, January 25, 2017 7:22 AM
  • Hi,

    Please check and modify the code below(change listName and itemId), then add the code into a content editor web part in document library view page.

    <script src="//code.jquery.com/jquery-3.1.0.min.js" type="text/javascript"></script>
    <script type="text/javascript">
    // occurs when a user clicks the update button
    function Update() {
        var listName = "DL";   
        var itemId = "1"; // Update Item Id here
        var titleVal = "New Updated Title";
        UpdateListItem(listName,itemId,titleVal);
    }
    function UpdateListItem(listName,itemId,titleVal){
    	var itemType = GetItemTypeForListName(listName);
        var item = {
            "__metadata": { "type": itemType },
            "Title": titleVal    
        };
        
        $.ajax({
            url: _spPageContextInfo.siteAbsoluteUrl + "/_api/web/lists/getbytitle('" + listName + "')/items("+itemId+")",
            type: "POST",
            contentType: "application/json;odata=verbose",
            data: JSON.stringify(item),
            headers: {
                "Accept": "application/json;odata=verbose",
                "X-RequestDigest": $("#__REQUESTDIGEST").val(),
                "X-HTTP-Method": "MERGE",
                "If-Match": "*"
            },
            success: function (data) {
                alert('Success');
                window.location.href=window.location.href;
            },
            error: function (data) {
                alert("Error");
            }
        });
    }
    // Get List Item Type metadata
    function GetItemTypeForListName(name) {
        return "SP.Data." + name.charAt(0).toUpperCase() + name.split(" ").join("").slice(1) + "Item";
    }  
    </script>
    <input type="button" value="Update" onclick="Update()"/>

    More information:

    SharePoint 2013: Working with REST API using jQuery Ajax

    https://social.technet.microsoft.com/wiki/contents/articles/31995.sharepoint-2013-working-with-rest-api-using-jquery-ajax.aspx

    SharePoint 2013 – CRUD on List Items Using REST Services & jQuery

    https://www.plusconsulting.com/blog/2013/05/crud-on-list-items-using-rest-services-jquery/

    Best Regards,

    Dennis


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

    Wednesday, January 25, 2017 7:55 AM
    Moderator
  • The above is a solution using your requirements.

    Thanks, Ashish | Please mark a post helpful/answer if it is helpful or answer your query.

    Thursday, January 26, 2017 2:00 AM
  • Let me know how to update the file name based on the ID/Title column in Document library?
    Friday, January 27, 2017 9:26 AM
  • In above, function update() has itemid, which is passed to the next function in which REST Url has filter and get only item matching with item id. Here you can pass your item id dynamically based on your scenario.

    For File Name, It is FileName column.


    Thanks, Ashish | Please mark a post helpful/answer if it is helpful or answer your query.

    Saturday, January 28, 2017 1:02 PM
  • Hi,

    If you want to update file name, please change below

      var item = {
            "__metadata": { "type": itemType },
            "Title": titleVal    
        };
    

    to

      var item = {
            "__metadata": { "type": itemType },
            "FileLeafRef": titleVal    
        };
    

    Best Regards,

    Dennis


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

    Monday, January 30, 2017 1:57 AM
    Moderator