Syntax
REST API Endpoint:
https://SharePointSiteURL/_api/web?$select=MasterUrl
REST API endpoint to use in Add-ins:
<appweburl>/_api/SP.AppContextSite(@target)/web?$select=MasterUrl&@target=<hostweburl>
Embed Code Snippet
The following code snippet can be added to SharePoint page or in content editor web part as a script. This example is used to retrieve the default master page URL of a SharePoint site and this returns the site relative URL.
- <script type="text/javascript" src="/SiteAssets/Scripts/jquery.min.js"></script>
- <script type="text/javascript">
- $.ajax(
- {
- url: _spPageContextInfo.webAbsoluteUrl + "/_api/web?$select=masterurl",
- method: "GET",
- headers:
- {
- "Accept": "application/json; odata=verbose"
- },
- success: function(data)
- {
-
- console.log('Default master page URL: ' + data.d.MasterUrl)
- alert('Default master URL: ' + data.d.MasterUrl);
-
- }
- });
- </script>
Add-in Code Snippet
The following code snippet is used in SharePoint Add-in to get the default master page url of a SharePoint site. This returns a site relative URL of a default master page.
-
- $.getScript(scriptbase + "SP.RequestExecutor.js", execCrossDomainRequest);
-
-
-
- function execCrossDomainRequest()
- {
-
- var executor = new SP.RequestExecutor(appweburl);
-
-
-
-
- executor.executeAsync(
- {
- url: appweburl + "/_api/SP.AppContextSite(@target)/web?$select=masterurl&@target='" + hostweburl + "'",
- method: "GET",
- headers:
- {
- "Accept": "application/json; odata=verbose"
- },
- success: successHandler,
- error: errorHandler
- });
- }
-
- function successHandler(data)
- {
- var jsonObject = JSON.parse(data.body)
-
- console.log('Default master page URL: ' + jsonObject.d.MasterUrl)
- alert('Default master page URL: ' + jsonObject.d.MasterUrl);
- }
-
- function errorHandler(data, errorCode, errorMessage)
- {
- console.log("Could not complete cross-domain call: " + errorMessage);
- }