none
Sharepoint on premises 2016 RRS feed

  • Question

  • Two versions are getting created when we create a document with mandatory properties.This happening only in Sharepoint Onpremises 2016 when we are trying to create a document with mandatory property else we are having only one version.We are using rest services of sharepoint, The same rest services are been used for sharepoint online 2016 which is working as expected(only one version is been created, while trying to upload document with mandatory property)

    below are the steps we have followed.

    Below are the steps while creating the document.

    Step 1: Add file to folder./_api/web/GetFolderById('" + folderguid/_api/web/GetFolderById('" + folderguid + "')/Files/add(url='" + fileName + "',overwrite=true)

    Step 2:StartUpload using guid generated from step 1 . "/_api/web/GetFileById('" + currObjGUID + "')/startUpload('" + currObjGUID + "')

    Step 3:Continue upload till you reach end of chuncks. /_api/web/GetFileById('" + currObjGUID + "')/continueUpload(uploadId=guid'"/_api/web/GetFileById('" + currObjGUID + "')/continueUpload(uploadId=guid'" + currObjGUID + "',fileOffset=" + uploadedDataLength + ")

    Step 4:Finishupload to commit the content. /_api/web/GetFileById('" + currObjGUID/_api/web/GetFileById('" + currObjGUID + "')/finishUpload(uploadId=guid'" + currObjGUID + "',fileOffset=" + uploadedDataLength + ")

    we are getting two versions of document while we are trying to finish upload.this is the mandatory call we have to make to flush the content.(in all the steps above we are using same session Id)2nd version is created immediately once step 4 is executed and 1st version size remains 0

     

    Tuesday, January 21, 2020 7:26 AM

All replies

  • Hi Pushpanjali,

    Please check the following setting in SharePoint 2016 uploading library:

    Library Settings -> Versioning Settings:

    Please set to Yes, so when uploading the file and then set metadata column won't be checked in and generate two version.

    If set to No, the first version will be file itself, the second version is to set the metadata column value.

    Tested with the following code snippet:

    <script type="text/javascript" src="https://code.jquery.com/jquery-3.4.1.js"></script>
    <script type="text/javascript">
    
    function uploadDocument() {
        if (!window.FileReader) {
            alert("This browser does not support the HTML5 File APIs");
            return;
        }
    
        var element = document.getElementById("uploadInput");
        var file = element.files[0];
        var parts = element.value.split("\\");
        var fileName = parts[parts.length - 1];
    
        var reader = new FileReader();
        reader.onload = function (e) {
            addItem(e.target.result, fileName);
        }
        reader.onerror = function (e) {
            alert(e.target.error);
        }
        reader.readAsArrayBuffer(file);
    
        function addItem(buffer, fileName) {
            var call = uploadDocument(buffer, fileName);
            call.done(function (data, textStatus, jqXHR) {
                var call2 = getItem(data.d);
                call2.done(function (data, textStatus, jqXHR) {
                    var item = data.d;
                    var call3 = updateItemFields(item);
                    call3.done(function (data, textStatus, jqXHR) {
                        var div = jQuery("#message");
                        div.text("Item added");
                    });
                    call3.fail(function (jqXHR, textStatus, errorThrown) {
                        failHandler(jqXHR, textStatus, errorThrown);
                    });
                });
                call2.fail(function (jqXHR, textStatus, errorThrown) {
                    failHandler(jqXHR, textStatus, errorThrown);
                });
            });
            call.fail(function (jqXHR, textStatus, errorThrown) {
                failHandler(jqXHR, textStatus, errorThrown);
            });
        }
    
        function uploadDocument(buffer, fileName) {
            var url = String.format(
                "{0}/_api/Web/Lists/getByTitle('Documents')/RootFolder/Files/Add(url='{1}', overwrite=true)",
                _spPageContextInfo.webAbsoluteUrl, fileName);
            var call = jQuery.ajax({
                url: url,
                type: "POST",
                data: buffer,
                processData: false,
                headers: {
                    Accept: "application/json;odata=verbose",
                    "X-RequestDigest": jQuery("#__REQUESTDIGEST").val()            
                    }
            });
    
            return call;
        }
    
        function getItem(file) {
            var call = jQuery.ajax({
                url: file.ListItemAllFields.__deferred.uri,
                type: "GET",
                dataType: "json",
                headers: {
                    Accept: "application/json;odata=verbose"
                }
            });
    
            return call;
        }
    
        function updateItemFields(item) {
            var now = new Date();
            var call = jQuery.ajax({
                url: _spPageContextInfo.webAbsoluteUrl +
                    "/_api/Web/Lists/getByTitle('Documents')/Items(" +
                    item.Id + ")",
                type: "POST",
                data: JSON.stringify({
                    "__metadata": { type: "SP.Data.Shared_x0020_DocumentsItem" },
                    Title: "Test"
                }),
                headers: {
                    Accept: "application/json;odata=verbose",
                    "Content-Type": "application/json;odata=verbose",
                    "X-RequestDigest": jQuery("#__REQUESTDIGEST").val(),
                    "IF-MATCH": item.__metadata.etag,
                    "X-Http-Method": "MERGE"
                }
            });
    
            return call;
        }
    
        function failHandler(jqXHR, textStatus, errorThrown) {
            var response = JSON.parse(jqXHR.responseText);
            var message = response ? response.error.message.value : textStatus;
            alert("Call failed. Error: " + message);
        }
    }
    </script>
    
    <input type="file" id="uploadInput"/>
    <input type="button" id="Upload" value="Upload" onclick="uploadDocument()"/>
    <div id="message"></div>
    Upload and set Title field metadata:

    Thanks

    Best Regards


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

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.


    Wednesday, January 22, 2020 8:34 AM
  • Thank Jerry for taking time to look into my issue, It Helped me.
    Monday, January 27, 2020 6:20 AM
  • Hi Pushpanjali,

    You are welcome. And if my reply is helpful to your question, I suggest you can Mark it as answer so that it could also help others in the forum.

    Thanks

    Best Regards


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

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.

    Tuesday, January 28, 2020 1:20 AM