In SharePoint 2013 when you create any SharePoint Hosted App it will create separate web for the app you created. You can find it easily by verifying the URL at the browser.
1) SPHostUrl 2) SPAppWebUrl
1) SPHostUrl 2) SPAppWebUrl
Just do copy/paste the below code and execute it.
Note: The below shared code is 100% verified/executed.
Please reach the below post URL to get the hostweb url through REST service
http://sharepointquicksolutions.blogspot.com/2016/12/get-sharepoint-host-web-url-using-rest.htmlAppmanifest.xml:
Set read level permisisons to the web
<p id="getHostWebURL"> </p>
'use strict';
var hostweburl, appweburl;
var website;
$(document).ready(function () {
appweburl = window.location.protocol + "//" + window.location.host + _spPageContextInfo.webServerRelativeUrl;
hostweburl = _spPageContextInfo.siteAbsoluteUrl;
//hostweburl = decodeURIComponent(getQueryStringParameter("SPHostUrl"));
//appweburl = decodeURIComponent(getQueryStringParameter("SPAppWebUrl"));
var scriptbase = hostweburl + "/_layouts/15/";
$.getScript(scriptbase + "SP.Runtime.js",
function () {
$.getScript(scriptbase + "SP.js", retrieveListItems);
}
);
});
// Function to retrieve a query string value.
// For production purposes you may want to use
// a library to handle the query string.
function getQueryStringParameter(paramToRetrieve) {
var params =
document.URL.split("?")[1].split("&");
var strParams = "";
for (var i = 0; i < params.length; i = i + 1) {
var singleParam = params[i].split("=");
if (singleParam[0] == paramToRetrieve)
return singleParam[1];
}
}
function retrieveListItems() {
var context = new SP.ClientContext(appweburl);
var appContextSite = new SP.AppContextSite(context, hostweburl);
website = appContextSite.get_web();
context.load(website);
context.executeQueryAsync(onGetWebSuccess, onGetWebFail);
//the remaining code is omitted for clarity
}
function onGetWebSuccess() {
alert("calling" + website.get_title());
$('#getHostWebURL').text("The title of the host web of this app is " + website.get_title());
}
function onGetWebFail(sender, args) {
alert('Failed to get lists. Error:' + args.get_message());
}
function createFields()
{
currentcontext = new SP.ClientContext.get_current();
web = clientContext.get_web();
this.list = web.get_lists().getByTitle(‘ListName’);
//Add new fields to the list
//Single line of text Field
this.newColumn = oList.get_fields().addFieldAsXml(" ", true,
SP.AddFieldOptions.defaultValue);
//Multi line of text Field
this.newColumn = oList.get_fields().addFieldAsXml(" ", true,
SP.AddFieldOptions.defaultValue);
//Boolean Field
this.newColumn = oList.get_fields().addFieldAsXml(" ", true,
SP.AddFieldOptions.defaultValue);
//Number Field
this.newColumn = oList.get_fields().addFieldAsXml(" ", true,
SP.AddFieldOptions.defaultValue);
//DateTime Field
this.newColumn = oList.get_fields().addFieldAsXml(" ", true,
SP.AddFieldOptions.defaultValue);
//People Picker Field
this.newColumn = oList.get_fields().addFieldAsXml(" ", true,
SP.AddFieldOptions.defaultValue);
//Choice field(dropdown)
this.newColumn= oList.get_fields().addFieldAsXml("ChoiceA ChoiceA ChoiceB ChoiceC ChoiceD ", true,
SP.AddFieldOptions.defaultValue);
currentcontext.load(this.newfield);
currentcontext.executeQueryAsync(Function.createDelegate(this, this.onListCreateSuccess), Function.createDelegate(this, this.onQueryFailed));
}
function onListCreateSuccess(sender, args) {alert('sucess');
alert("Field Name: " + this.newfield.get_title());
}
function onQueryFailed(sender, args) {
alert('request failed ' + args.get_message() + '\n' + args.get_stackTrace());
}
Note: SP.Field.typeAsString Property is used to Gets or sets a value that specifies the type of the field.[From Msdn:http://msdn.microsoft.com/en-us/library/ee553500.aspx]