Hi Shekhar,
I am providing you an example of Dynamic Page creation using ApexTooling Api.
*Here i have created a vf page in which the object name is entered(custom/standard) on entering object name in input field & clicking on 'create page', a page is been created dynamically refrencing to the object in StandardController.
* Make sure you have put your base url in Remote Site Settings, It is needed for callout.
Vf Page:-
<apex:page controller="ctrl_DynamicPageCreation">
<apex:form id="form1">
<div style="margin-left: 23px;">
<h1 style="padding-right: 10px;"> Object Name</h1>
<apex:inputText html-placeholder="Object Name..." value="{!objectName}">
</apex:inputText>
</div> <br/>
<apex:commandButton value="CreatePage" action="{!createpage}" reRender="Error" style="margin-left: 23px;"/>
</apex:form>
<apex:outputpanel id="Error">
<apex:pageMessages ></apex:pageMessages>
</apex:outputpanel>
</apex:page>
Controller:-
public class ctrl_DynamicPageCreation{
public String objectName{get;set;}
Public String name;
public ctrl_DynamicPageCreation(){}
public void createpage(){
if(objectName.contains('__c')){
name = objectName.replace('__c','');
}
system.debug('****'+objectName);
String salesforceHost = System.Url.getSalesforceBaseURL().toExternalForm();
String url = salesforceHost + '/services/data/v29.0/sobjects/ApexPage';
HttpRequest req = new HttpRequest();
req.setMethod('POST');
req.setEndpoint(url);
req.setHeader('Content-type', 'application/json');
req.setHeader('Authorization', 'Bearer ' + UserInfo.getSessionId());
//for controllerType = >0 -- no controller
//req.setBody('{"Name" : "TestPageFromRest","Markup" : "<apex:page>hello</apex:page>","ControllerType" : "0","MasterLabel":"TestPageFromRest","ApiVersion":"29.0"}');
//for controllerType => 1 -- Standard controller + extensions
req.setBody('{"Name" : "FileUploader'+name+'","Markup" : "<apex:page standardController=\''+objectName+'\' extensions=\'dynamicCreationController\'>hello</apex:page>","ControllerType" : "1","MasterLabel":"FileUploader'+objectName+'","ApiVersion":"29.0"}');
//for controllerType => 3 --custom Controller
//req.setBody('{"Name" : "DynamicPage'+objectName+'","Markup" : "<apex:page controller=\''+objectName+'\'>hello</apex:page>","ControllerType" : "3","MasterLabel":"TestPageFromRestCase23","ApiVersion":"29.0"}');
Http http = new Http();
HTTPResponse res = http.send(req);
System.debug(res.getBody());
if(string.valueof(res.getBody()).contains('success')){
ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.CONFIRM,'Your Page has been created..');
ApexPages.addMessage(myMsg);
}
else{
string errorMessage = string.valueof(res.getBody());
string test = errorMessage.substring(13,errorMessage.indexOf('","errorCode'));
system.debug('&&&&'+test);
if(test.contains('Markup')){
ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR,'Object does not exist. Please check the spelling and try again.');
ApexPages.addMessage(myMsg);
}
else if(test.contains('That page name is already in use, please choose a different one.')){
ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR,'Page for this object is already been created. Try to choose different one.');
ApexPages.addMessage(myMsg);
}
else{
ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR,test);
ApexPages.addMessage(myMsg);
}
}
}
}
Hope so this information is helpful to you.
Thanks