The JavaScript Object Model is used to fetch the required information. The following code snippet depicts the flow.
var webpartExists = null;
$(document).ready(function(){
ExecuteOrDelayUntilScriptLoaded(LoadwpMethod, "sp.js");
});
function LoadwpMethod(){
var webpartContext = new SP.ClientContext();
var webpartContextWeb = webpartContext.get_web();
// The page url should be server relative url
var oFile = webpartContextWeb.getFileByServerRelativeUrl("/Pages/ToolPage4.aspx");
// Sets the personalization scope and gets the web part
var wpm = oFile.getLimitedWebPartManager(SP.WebParts.PersonalizationScope.shared);
webpartExists = wpm.get_webParts();
webpartContext.load(webpartExists);
webpartContext.executeQueryAsync(FindWebPartinPage, QueryFailed);
}
function FindWebPartinPage() {
// Gets the webpart count
pageWebPartCount = webpartExists.get_count();
}
function QueryFailed(sender, args){
console.log(args.get_message());
}The above code gets the webpart count of a SharePoint page using relative path specified.