2

In SP2013, how can I check if the current page is the default page of the current site using javascript object model or REST api?

Thanks

3
  • which template are you using? Aug 24, 2016 at 22:08
  • both publishing and team site.
    – omega
    Aug 24, 2016 at 22:09
  • I used a powershell script some time ago, that iterates throug all the Web's RootFolder files, and maybe if you compare the names you can achieve what you need.. I know that's not exactly what you want because you need this to work using the REST api, but maybe you find it useful: $site = Get-SPSite 'yoursite' $web = $site.RootWeb foreach($file in $web.RootFolder.Files){ write-host $file.Name -ForegroundColor Magenta } Aug 24, 2016 at 23:34

1 Answer 1

5

At first get the current page from _spPageContextInfo

var currentPage = _spPageContextInfo.serverRequestPath

Now make a GET request in following end-point to get default or welcome page

_api/Web/RootFolder/WelcomePage

After getting the response, check if both are same.

PS: It works in team & publishing site both.

Sample code is jQuery & REST API

SP.SOD.executeFunc("SP.js", "SP.ClientContext", function() {
    var currentPage = _spPageContextInfo.serverRequestPath
    $.ajax({
        url: _spPageContextInfo.webAbsoluteUrl + "/_api/Web/RootFolder/WelcomePage",
        type: "GET",
        headers: {
            "accept": "application/json;odata=verbose",
        },
        success: function(data) {
            if (currentPage.indexOf(data.d.WelcomePage) > -1) {
                console.log("Current page is default");
            } else {
                console.log("Current page is not default");
            }
        },
        error: function(error) {
            alert(JSON.stringify(error));
        }
    });
});

SP.SOD.executeFunc is important, you are executing code on publishing site

4
  • This is for team site? Is there one for publishing site?
    – omega
    Aug 25, 2016 at 5:40
  • Did you try in publishing site? Aug 25, 2016 at 5:43
  • It works in team and publishing site both. @omega Aug 25, 2016 at 6:00
  • 1
    Post is a little old, but thought I would comment that if you're working in SPO, there's a _spPageContextInfo.isWebWelcomePage property. Might be worth checking if the latest SP 2013/2016 OnPrem code has it before going REST call. Oct 10, 2017 at 17:38

Your Answer

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

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