I already found a similar post here:


But instead of adding a content/script editor to every document library, as suggested, I'd prefer to add my code to the system master page and have it triggered only when a document library gets opened. Does any one have a solution for this? The code I developed is working great when I put it in a script editor, now how can I make it run from the master page only on document libraries?

2 Answers 2


you maybe get Doc. library by checking the url if it contains Forms/AllItems.aspx and then execute your code

  • Thanks for the reply. With your solution though I cannot create custom views for libraries... What if, for instance, I switch from AllItems.aspx to Custom.aspx? Apr 10, 2018 at 13:16

Ok, I solved my problem. Here's my solution:

        if ( ctx != null && ctx.listBaseType == 1) {
            //YOUR CODE HERE

This way the code will be executed only if the Sharepoint ctx variable is loaded and the library you open is a Document Library (that means that listBaseType == 1).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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