Hi
All,
Earlier
We have seen How to create Site Columns using Declarative XML(http://sharepointkitchen.blogspot.in/2012/12/creating-sharepoint-site.html),
In current post we will see the same using ECMA Script.
In a following example we are going to do
1. Creation of Site Column
2. Adding the Site Column to Content Type.
'use strict';
var SPAppWebUrl;
var spHostUrl;
var contentTypeCollection;
var siteColumnColl;
var web;
var field;
(function
() {
// This code runs when the DOM is ready and creates
a context object which is
// needed to use the SharePoint object model
$(document).ready(function
() {
spHostUrl =
decodeURIComponent(getQueryStringParameter('SPHostUrl'));
SPAppWebUrl =
decodeURIComponent(getQueryStringParameter("SPAppWebUrl"));
$('#btnCreate').on('click',
function () {
CreateSiteColumn();
});
});
function
CreateSiteColumn() {
var Context = new
SP.ClientContext.get_current();
if (Context !==
undefined && Context !== null) {
var parentContext = new
SP.AppContextSite(Context, spHostUrl);
web =
parentContext.get_web();
//get field collection
siteColumnColl =
web.get_fields();
//XML Field
var xmlField = '<Field ID ="{fc571c6c-51ee-48df-953d-2464628d741b}"
Type ="Text" Name ="Answer" DisplayName ="Answer"
Group ="SiteColumnGrp" Required ="FALSE" AllowDeletion
="TRUE" Overwrite ="TRUE" OverwriteInChildScopes="TRUE"></Field>';
siteColumnColl.addFieldAsXml(xmlField, false,
SP.AddFieldOptions.AddToNoContentType);
Context.load(siteColumnColl);
Context.executeQueryAsync(onCreationSuccess, onCreationFail);
}
function
onCreationSuccess() {
addToContentType();
}
function
addToContentType() {
//get all Content Types
contentTypeCollection = web.get_contentTypes();
//get the Field
field =
web.get_fields().getByTitle('Answer');
Context.load(contentTypeCollection);
Context.load(field);
Context.executeQueryAsync(onAddingToCTSuccess, onCreationFail);
}
//Used
to Add, SiteColumn to ContentType.
function
onAddingToCTSuccess() {
var isExisted = false;
var name = 'TestCType';
var exContentType;
var contentEnumerator =
contentTypeCollection.getEnumerator();
while
(contentEnumerator.moveNext()) {
var currentCT =
contentEnumerator.get_current();
//checking whether Contentype is exists or not.
if (currentCT.get_name()
== name) {
isExisted
= true;
exContentType = currentCT;
break;
}
}
if (isExisted) {
if (isExisted !== null) {
//Creating FieldLink
var fieldAdd = new
SP.FieldLinkCreationInformation();
fieldAdd.set_field(field);
//Add FieldLink to ContentType
exContentType.get_fieldLinks().add(fieldAdd);
exContentType.update(true);
Context.load(exContentType);
Context.executeQueryAsync(onAddedCTToSuccess, onCreationFail);
}
}
}
function
onAddedCTToSuccess() {
alert('Site Column Created and added to Content Type');
}
function
onCreationFail(sender, args) {
alert('Request failed. ' +
args.get_message() + '\n' + args.get_stackTrace());
}
}
function
getQueryStringParameter(urlParameterKey) {
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] ==
urlParameterKey)
return
decodeURIComponent(singleParam[1]);
}
}
Thanks.
Related Post
SharePoint 2013
- List view render using JSLink with Fabric UI
- Delete Quick Links using Powershell
- delete/Add Top Navigation Links using Powershell
- Get CheckedOut Files from SharePoint 2013 and MOSS2007
- PowerShell::Get Content Database Size in SharePoint
- PowerShell :: Delete All List Items
- Powershell :: Apply Master Page to all sub sites under site collection
- Internet Explorer cannot display this feed - while accessing REST URL in SharePoint
- SharePoint Installation: The tool was unable to install application server role web server (IIS) role SharePoint 2013.
- Basics: Difference between Synchronous Events and Asynchronous Events in SharePoint
- Remove-SPSite 0x80070003 - Forcely delete Site Collection that cannot be deleted
- This operation can be performed only on a computer that is joined to a server farm by users who have permissions in SQL Server to read from the configuration database. To connect this server to the server farm, use the SharePoint Products Configuration Wizard, located on the Start menu in Microsoft SharePoint 2010 Products.
- Restore-SPSite: The operation that you are attempting to perform cannot be completed successfully.
- Migrate Multiple site(sub sites) using MetaLogix and change the Template in Target
- Issue:The date file "C:\ProgramData\Metalogix\EnvironmentSettings.xml" contains invalid data.Values in that file reset to defaults
- Get List Field Type using ECMA Script in SP Hosted Apps
- SharePoint Interview Questions and Answers..
- Issues:SharePoint Foundation 2013 Prerequisites installation(off line) on Windows Server2012 R2
- Get Choice Field Type Values using ECMA Script
- Something Went wrong error message while deploying App in SharePoint 2013
- The Server was unable to save the form at this time. Please try again.
- Get the Items inside the Folder in document Library in SharePoint 2013 using REST API
- SharePoint Client Browser for SharePoint Online and SharePoint On-Premises
- What is Managed meta data service in SharePoint 2010, 2013 and How to Configure it?
- Deprecated features in SharePoint 2013
ECMA Script
- Get List Field Type using ECMA Script in SP Hosted Apps
- Get Choice Field Type Values using ECMA Script
- Executing Search Queries using ECMA Script in SharePoint 2013
- Create Content Type using ECMA Script
- Get List Content Types using ECMA Script
- Get Lookup Column Value using ECMA Script
- Create Folder in document Library using ECMA Script in SharePoint Hosted Apps
- Delete List Item Collection using ECMA Script
- Get List Item attachments using ECMA Script
- How to get List Items Count from SP List Using Visual webpart and ECMA script in SharePoint 2010
- How to get the List Items Count of SP List using ECMA Script in SharePoint 2010 ,2013
- SharePoint Client Object Model using ECMAScript in 2013
0 comments:
Post a Comment