none
Updating metadata for file using SharePoint REST API RRS feed

  • Question

  • I have two scenarios, I either insert a file or update content of a file in a Document Library. In both cases I might have to include metadata, preferable in one request.

    Currently I'm trying to update metadata for existing file (a second request after insert/update).

    Problem at hand is that I keep getting a 500 response and I cannot find the error in my call.

    POST /sites/LaserNetTestDK/_api/Web/GetFileByServerRelativeUrl('/sites/LaserNetTestDK/inserted.xlsx')/ListItemAllFields 

    Content = {"__metadata":{"type":"SP.ListItem"} , "MyText": "new text" }

    Response: HTTP/1.1 500 Internal Server Error

    {"error":{"code":"-1, System.Collections.Generic.KeyNotFoundException","message":{"lang":"en-US","value":"The given key was not present in the dictionary."}}}

    'MyText' is a custom field I added in SharePoint. I need to both edit/insert values for stock and custom fields/columns.

    Alternatively if I can accomplish the above in the initial insert(GetFolderByServerRelativeUrl('%1')/files/add(overwrite=%2,url='%3'))/update(X-HTTP-Method=PUT for GetFileByServerRelativeUrl('%1')/$value) requests and save the second, that would be ideal.

    Microsoft documentation is pretty scares and limited, so above is made through dissecting blog posts

    Thanks for any help!

    Thursday, October 9, 2014 12:51 PM

Answers

  • Thank you guys. Jason, your example was identical to what I was basically doing but it made me notice that I was missing the content-type. Setting this to "application/json;odata=verbose", made my code work :)

    Cryptic non-helpful error from SharePoint did not reveal this problem.

    • Marked as answer by Lindali Saturday, October 25, 2014 11:13 AM
    Friday, October 10, 2014 7:53 AM
  • Hi,

    According to your post, my understanding is that you hava an issue to update metadata using REST API.

    I have made a simple code demo to update a existing item field, it works like a charm, you can have a look at it.

    <script src="http://code.jquery.com/jquery-1.10.2.min.js" type="text/javascript"></script>
    
    <script type="text/javascript">
    
    // When the body is loaded, the onload event handler executes each function whose name is contained in this array.
    
    _spBodyOnLoadFunctionNames.push("callCSOM");
    
    function callCSOM()
    
    {
    
                    $("#Button1").click(function()
    
                    {              
    
                                    ExecuteOrDelayUntilScriptLoaded(Test, "sp.js");
    
                    });           
    
    }
    
    function Test(){
    
                    //alert(1);
    
                    updateMetadata(1);
    
                    alert("Updated..");
    
    }
    
    function updateMetadata(id) {
    
        var url = _spPageContextInfo.webAbsoluteUrl +
    
                    "/_api/Web/Lists/getByTitle('Libs_1')/Items(" +
    
                    id + ")";
    
        //alert(url);
    
        var dfd = $.Deferred();
    
        $.ajax(
    
        {
    
            'url': url,
    
            'method': 'POST',
    
            'data': JSON.stringify({
    
                '__metadata': {'type':'SP.ListItem'},
    
                'MyText': 'Update Field Again'
    
            }),
    
            'headers': {
    
                'accept': 'application/json;odata=verbose',
    
                'content-type': 'application/json;odata=verbose',
    
                'X-RequestDigest': $('#__REQUESTDIGEST').val(),
    
                'X-Http-Method': 'PATCH',
    
                "If-Match": "*"
    
            },
    
            'success': function (data) {
    
                var d = data;
    
                dfd.resolve();
    
            },
    
            'error': function (err) {
    
                dfd.reject();
    
            }
    
        }
    
       );
    
        return dfd;
    
    }
    
                                                                      
    
    </script>
    
    <input id="Button1" type="button" value="Update Metadata"/>
    

    Thanks & Regards,

    Jason


    Jason Guo
    TechNet Community Support

    • Marked as answer by Lindali Saturday, October 25, 2014 11:13 AM
    Friday, October 10, 2014 6:24 AM

All replies

  • When you have sharepoint internal server error that is error 500 

    solution: do a iis restart on server.

    Click Start, click Run type cmd  then type iisreset /noforce 

    or

    Click Start, click Run type IISReset, and then click OK.


    it should fix your problem .

    generally its caused when app pool is full. 

    Thursday, October 9, 2014 2:37 PM
  • Try using the complete absolute url to the document when calling GetFileByServerRelativeUrl

    Blog | SharePoint Field Notes Dev Tools | SPFastDeploy | SPRemoteAPIExplorer

    Thursday, October 9, 2014 8:29 PM
  • Hi,

    According to your post, my understanding is that you hava an issue to update metadata using REST API.

    I have made a simple code demo to update a existing item field, it works like a charm, you can have a look at it.

    <script src="http://code.jquery.com/jquery-1.10.2.min.js" type="text/javascript"></script>
    
    <script type="text/javascript">
    
    // When the body is loaded, the onload event handler executes each function whose name is contained in this array.
    
    _spBodyOnLoadFunctionNames.push("callCSOM");
    
    function callCSOM()
    
    {
    
                    $("#Button1").click(function()
    
                    {              
    
                                    ExecuteOrDelayUntilScriptLoaded(Test, "sp.js");
    
                    });           
    
    }
    
    function Test(){
    
                    //alert(1);
    
                    updateMetadata(1);
    
                    alert("Updated..");
    
    }
    
    function updateMetadata(id) {
    
        var url = _spPageContextInfo.webAbsoluteUrl +
    
                    "/_api/Web/Lists/getByTitle('Libs_1')/Items(" +
    
                    id + ")";
    
        //alert(url);
    
        var dfd = $.Deferred();
    
        $.ajax(
    
        {
    
            'url': url,
    
            'method': 'POST',
    
            'data': JSON.stringify({
    
                '__metadata': {'type':'SP.ListItem'},
    
                'MyText': 'Update Field Again'
    
            }),
    
            'headers': {
    
                'accept': 'application/json;odata=verbose',
    
                'content-type': 'application/json;odata=verbose',
    
                'X-RequestDigest': $('#__REQUESTDIGEST').val(),
    
                'X-Http-Method': 'PATCH',
    
                "If-Match": "*"
    
            },
    
            'success': function (data) {
    
                var d = data;
    
                dfd.resolve();
    
            },
    
            'error': function (err) {
    
                dfd.reject();
    
            }
    
        }
    
       );
    
        return dfd;
    
    }
    
                                                                      
    
    </script>
    
    <input id="Button1" type="button" value="Update Metadata"/>
    

    Thanks & Regards,

    Jason


    Jason Guo
    TechNet Community Support

    • Marked as answer by Lindali Saturday, October 25, 2014 11:13 AM
    Friday, October 10, 2014 6:24 AM
  • Thank you guys. Jason, your example was identical to what I was basically doing but it made me notice that I was missing the content-type. Setting this to "application/json;odata=verbose", made my code work :)

    Cryptic non-helpful error from SharePoint did not reveal this problem.

    • Marked as answer by Lindali Saturday, October 25, 2014 11:13 AM
    Friday, October 10, 2014 7:53 AM