Hi
All,
In a
following post we will see, How to get Content Types from the Custom List in
SharePoint using ECMA Script.
Below
snippet I have used in SharePoint Hosted Apps.We can use the same code in
SharePoint 2010 and 2013 normal developments also with minimal changes.
I
have added 2 Custom Content Types to My List “MyList”.
Please
find the below Code snippet.
'use strict';
var SPAppWebUrl;
var spHostUrl;
var contentTypeCollection;
var listCollection;
var list;
(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"));
$('#btnGetContenTypes').on('click',
function () {
GetListContentTypes();
});
});
function
GetListContentTypes() {
var Context = new
SP.ClientContext.get_current();
if (Context !==
undefined && Context !== null) {
var parentContext = new
SP.AppContextSite(Context, spHostUrl);
var web =
parentContext.get_web();
listCollection =
web.get_lists();
list =
listCollection.getByTitle("MyList");
contentTypeCollection = list.get_contentTypes();
Context.load(contentTypeCollection);
Context.executeQueryAsync(onQuerySucceeded, onQueryFailed);
}
function
onQuerySucceeded() {
var contentType = 'MyList Content Types:\n '
var
contentTypeEnumerator = contentTypeCollection.getEnumerator();
while
(contentTypeEnumerator.moveNext()) {
var content =
contentTypeEnumerator.get_current();
contentType +=
content.get_name() + '\n';
}
alert(contentType);
}
function
onQueryFailed(sender, args) {
alert('Error occurred:'
+ args.get_message());
}
}
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]);
}
}
})();
The Output
of the above snippet will as follows,
0 comments:
Post a Comment