0

I get the error

The file "Style Library/FILENAME.js" is not checked out.

when uploading a new file (FILENAME.js) using the following code.

var uploadFile = function (sourcefolder, scriptName) {

        var deferred = $.Deferred();
        var sourceFile = appweburl + sourcefolder + scriptName;

        $.ajax({
            url: sourceFile,
            type: 'GET',
            cache: false,
            dataType: 'text',
            success: function (contents) {

                var context = new SP.ClientContext.get_current();
                var factory = new SP.ProxyWebRequestExecutorFactory(appweburl);
                context.set_webRequestExecutorFactory(factory);
                var appContextSite = new SP.AppContextSite(context, hostweburl);

                var createInfo = new SP.FileCreationInformation();
                createInfo.set_content(new SP.Base64EncodedByteArray());
                for (var i = 0; i < contents.length; i++) {
                    createInfo.get_content().append(contents.charCodeAt(i));
                }
                createInfo.set_overwrite(true);
                createInfo.set_url(scriptName);

                var files = appContextSite.get_web().getFolderByServerRelativeUrl(targetFolder).get_files();
                context.load(files);
                var file = files.add(createInfo);
                file.checkIn("Uploaded during installation.");
                context.executeQueryAsync(
                    function () {
                        deferred.resolve();
                    },
                    function (sender, args) {
                        var errorMessage = "Error occurred while uploading " + scriptName;
                        console.log(errorMessage + " " + args.get_message());
                        deferred.reject(errorMessage);
                    });
            },
            error: function (sender, args) {
                var errorMessage = "Error occurred while uploading " + scriptName;
                console.log(errorMessage + " " + args);
                deferred.reject(errorMessage);
            }
        });

        return deferred.promise();
    };

I don't understand what I need to check out? This code works fine in one environment, but not in another one (where I get this error).

Anyone that know what the reason is?

0

There are numerous reason why a file may or may not need to be checked in upon initial upload, including whether "requires checkout" is enabled on the library combined with whether or not there is any missing required metadata fields, and whether the list in on a modern site or not. The best thing to do, if you want your code to be functional against different lists that may have different configurations, is to check the CheckOutType property of the file to determine whether it needs to be checked in or not. See if this works better for you:

var onuploadError = function (sender, args) {
    var errorMessage = "Error occurred while uploading " + scriptName;
    console.log(errorMessage + " " + args);
    deferred.reject(errorMessage);
}       

var uploadFile = function (sourcefolder, scriptName) {

    var deferred = $.Deferred();
    var sourceFile = appweburl + sourcefolder + scriptName;

    $.ajax({
        url: sourceFile,
        type: 'GET',
        cache: false,
        dataType: 'text',
        success: function (contents) {

            var context = new SP.ClientContext.get_current();
            var factory = new SP.ProxyWebRequestExecutorFactory(appweburl);
            context.set_webRequestExecutorFactory(factory);
            var appContextSite = new SP.AppContextSite(context, hostweburl);

            var createInfo = new SP.FileCreationInformation();
            createInfo.set_content(new SP.Base64EncodedByteArray());
            for (var i = 0; i < contents.length; i++) {
                createInfo.get_content().append(contents.charCodeAt(i));
            }
            createInfo.set_overwrite(true);
            createInfo.set_url(scriptName);

            var files = appContextSite.get_web().getFolderByServerRelativeUrl(targetFolder).get_files();
            context.load(files);
            var file = files.add(createInfo);
            context.load(file, 'Include(CheckOutType,Name,CheckedOutByUser)'); 

            context.executeQueryAsync(
                function () {
                    if (file.get_checkOutType() != 2) {
                        file.checkIn("Uploaded during installation.");
                        context.executeQueryAsync(function () {
                            deferred.resolve();
                        },
                        onuploadError);
                    } else {
                        deferred.resolve();
                    }
                },
                onuploadError);
        },
        onuploadError
    });

    return deferred.promise();
};

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.