Monday, October 10, 2016

Sharepoint Add an app - Storefront javascript listtemplate modification

Description:
To display only ListTemplate that contains 'CG_', which can be customised as per your need. This script was added into masterpage using ScriptLink customactions javascript embed. 

Note:
The $2b_3 and $M_3 collection variables tend to change based on MS updates. You have to find the right one using browser devtools in your 'add an app page' to check the OOTB Storefront collection's and get the right ones.

Code:
//Function to hide OOTB apps in 'AddAnApp' page
function CGRefreshUI() {
            var listedApps = SP.Storefront.StorefrontApp.get_currentView();  
             //remove  Noteworthy
            listedApps.$2b_3.length = 0;
              //remove all except List templates starting with 'CG_'
                  for (var i = listedApps.$M_3.length-1; i--;) {
               if (listedApps.$M_3[i].$2R_0.Title.indexOf("CG_") < 0 ) {
                  listedApps.$M_3.splice(i, 1);
               }
            }
            SP.Storefront.StorefrontApp.get_currentView().updateUI();
}

//Function check if SP.Storefront js is loaded and call core method
function CGAlterStorefront() {
        if (SP.Storefront != undefined) {                       
var listedApps = SP.Storefront.StorefrontApp.get_currentView();
if (listedApps == undefined || listedApps.$2b_3 == null || listedApps.$M_3 == null) { setTimeout(CGAlterStorefront, 300); return; }
listedApps.spProxy.add_getAppsCompleted(CGRefreshUI);
CGRefreshUI();
        }
    }


//Call Alter 'Add an Apps/StoreFront' function
   _spBodyOnLoadFunctionNames.push("CGAlterStorefront");


Reference:
https://spmatt.wordpress.com/2014/05/21/hiding-the-out-of-the-box-document-library-in-sharepoint-2013/