Tuesday, January 8, 2019

Show Specific Folder items of Document library in WebPart using Script edit WebPart in SharePoint online


<script type="text/javascript">

// Call the function once page is loaded
SP.SOD.executeFunc('sp.js','SP.ClientContext',runWhenLoaded);

//Shared Global Variables
var clientContext;
var oWeb;
var oList;
var currentUserIndex;
var userCount;

function runWhenLoaded() {
// Create context, get the rootweb, then get the list
        clientContext = SP.ClientContext.get_current();
        oWeb =  clientContext.get_web();
        oList = oWeb.get_lists().getByTitle("Documents");
        clientContext.load(oList);
var webPartView = oList.getView('{29E784E3-089B-4CF5-A415-018ED441A357}');



clientContext.load(webPartView);

        // Execute the queued context commands (passes back to the global vars)
        clientContext.executeQueryAsync(function() {


if (webPartView) {

        alert('webpartview valid');
webPartView.set_scope(2); // recursiveall

        webPartView.set_viewQuery('<Where><Contains><FieldRef Name="FileDirRef"/><Value Type="Text">Strategy</Value></Contains></Where>');

        webPartView.update();

        clientContext.load(webPartView);

        clientContext.executeQueryAsync(Function.createDelegate(this, updateSuccess), Function.createDelegate(this, updateFailed));
function updateSuccess(sender, args) { alert('changed the view'); }

function updateFailed(sender, args) { alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace()); }

}
else {

        alert('Unable to get view');

    }

},function() { alert("Error"); } );
    }
</script>

No comments:

Post a Comment