0
votes

In SharePoint and using SPServices, I am trying to update the content of the files uploaded in the sub folders This is the image and also the file metatdata i want to update. sharepoint image The library structure looks like this:

List Name: Shared Documents

Folder #1 - Documents Subfolder #1 - Procurement File #1 - uploaded files File #2

So basically i want to update the requestID, filetype, etc

Can anyone help? Thanks!

Here is my code

                var oldFolderName = "Procurement";
                $().SPServices({
                    operation: "GetListItems",
                    async: false,
                    listName: 'Documents',
                    CAMLViewFields: "<ViewFields Properties='True' />",
                    CAMLQuery: "<Query><Where><Eq><FieldRef Name='FSObjType' /><Value Type='Integer'>1</Value></Eq></Where></Query>",
                    CAMLQueryOptions: "<QueryOptions><IncludeMandatoryColumns>FALSE</IncludeMandatoryColumns></QueryOptions>",
                    completefunc: function (xData, Status) {
                        $(xData.responseXML).SPFilterNode('z:row').each(function () {
                            var existingFolderName = $(this).attr("ows_FileLeafRef").split(";#")[1];
                            if (existingFolderName == oldFolderName) {
                                var Folder_ID = $(this).attr("ows_ID");
                                $().SPServices({
                                    operation: "UpdateListItems",
                                    async: false,
                                    batchCmd: "Update",
                                    listName: 'Documents',
                                    valuepairs: [["Title", "Working"], ["requestID", "Working"]],
                                    ID: Folder_ID,
                                    completefunc: function (xData, Status) {
                                        console.log("Folder Name Updated Successfully...");
                                    }
                                });
                            }
                        });
                    }
                });

But the code is only updating the Sub folder metadata (Procurement) not the list in the procurement folder.

This is what i actually mean enter image description here

1

1 Answers

0
votes

No need to use SPService to loop folder firstly and then update file metadata, instead,use Rest API getfilebyserverrelativeurl to get file and update metadata like below:

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
updateFileMetadata();
function updateFileMetadata() {
        var def = jQuery.Deferred();
        
        var restSource= _spPageContextInfo.webAbsoluteUrl+"/_api/Web/getfilebyserverrelativeurl('/sites/Sitename/Shared%20Documents/Procurement/filename')/ListItemAllFields";
                
        var itemPayload = {"__metadata": {"type":"SP.Data.Shared_x0020_DocumentsItem"},"Title":"Updated","requestID","Working","filetype":"Working"};
        var dfd = jQuery.Deferred();
        $.ajax(
        {
            url: restSource,
            method: "POST",
            contentType: "application/json;odata=verbose",
            data: JSON.stringify(itemPayload),
            headers:
                {
                    "Accept": "application/json;odata=verbose",
                    "X-RequestDigest": $('#__REQUESTDIGEST').val(),
                    "X-HTTP-Method":"MERGE",
                    "If-Match": "*"

                },
            success: function (data) {
                alert("Success");
                dfd.resolve(data);
            },
            error: function (err) {
                dfd.reject(err);
                alert("Error");
            }
        });
        return dfd.promise();
    }
</script>

For _api/Web/getfilebyserverrelativeurl:

the url value should be

/sites/sitename/Shared%20Documents/Procurement/filename in this case,

if it's a root site which no "sites" character in the site url, then change like this:

/Shared%20Documents/Procurement/filename