Wednesday, November 2, 2016

Check if file exist in SharePoint Document library using JSON


Check if file exist in SharePoint Document library using JSON

<script type="text/javascript" src="//ajax.aspnetcdn.com/ajax/4.0/1/MicrosoftAjax.js"> </script>
<script type="text/javascript" src="/_layouts/15/sp.runtime.js"> </script>
<script type="text/javascript" src="/_layouts/15/sp.js"> </script>


<script type="text/javascript">
       
        var retunValue = true;
        //var url = "http://ravimaloth:9870/Aaaaj/Test1/sample.doc";
        var listTitle = 'Aaaaj';
        var fileUrl = 'http://ravimaloth:9870/Aaaaj/Test1/sample.doc';

        var ctx = SP.ClientContext.get_current();
        var list = ctx.get_web().get_lists().getByTitle(listTitle);
        var qry = new SP.CamlQuery();
        qry.set_viewXml('<View Scope="RecursiveAll"><Query><Where><Eq><FieldRef Name="FileRef"/><Value Type="Url">' + fileUrl + '</Value></Eq></Where></Query></View>');
        var items = list.getItems(qry);
        ctx.load(items);
        ctx.executeQueryAsync(function () {
          
            var fileFound = (items.get_count() > 0);
            debugger;
            if (fileFound) {
                if (!window.confirm('File already exist, Are you sure you want to replace?'))                 {
                     retunValue = false;
                }              
            }          
        },
        function(sender, args) {
            console.log(args.get_message());
        });


</script>


No comments:

Post a Comment