Want to become a Vibe Coder? Join Vibe Coding Training here
x
C# Corner
Tech
News
Videos
Forums
Jobs
Books
Events
More
Interviews
Live
Learn
Training
Career
Members
Blogs
Challenges
Certification
Contribute
Article
Blog
Video
Ebook
Interview Question
Collapse
Feed
Dashboard
Wallet
Learn
Achievements
Network
Refer
Rewards
SharpGPT
Premium
Contribute
Article
Blog
Video
Ebook
Interview Question
Register
Login
How to get all the List Content Types using KnockoutJS and CSOM in SharePoint 2013
WhatsApp
Vijai Anand Ramalingam
10y
4.7
k
0
0
25
Blog
Introduction:
In this blog you will see how to get all the Site Content Types using KnockoutJS and CSOM in SharePoint 2013. Please refer
my article
to implement KnockoutJS in a SharePoint page.
Script:
<script src=
"https://c986.sharepoint.com/SiteAssets/jquery-1.8.3.min.js"
></script>
<script src=
"https://c986.sharepoint.com/SiteAssets/knockout-3.1.0.js"
></script>
<script src=
"https://c986.sharepoint.com/SiteAssets/ko.sp-1.0.min.js"
></script>
<script>
ExecuteOrDelayUntilScriptLoaded(MainFunction,
"sp.js"
);
var
completeCTList =
null
;
var
contentTypeCollection=
null
;
// Class used for saving the property values.
function
CTList(name) {
var
self =
this
;
self.Name = name;
}
// View Model - JavaScript that defines the data and behavior of your UI
function
CTListViewModel() {
var
self =
this
;
// observableArray equivalent of a regular array,
self.ContentTypes = ko.observableArray([]);
self.AddContentTypes =
function
(name) {
self.ContentTypes.push(
new
CTList(name));
}
}
function
MainFunction() {
completeCTList =
new
CTListViewModel();
// Retrieve the SharePoint Site Content Types
retrieveContentTypes();
// Activates knockout.js
ko.applyBindings(completeCTList);
}
function
retrieveContentTypes() {
var
clientContext =
new
SP.ClientContext.get_current();
var
list = clientContext.get_web().get_lists().getByTitle(
"Employee Details"
);
this
.contentTypeCollection = list.get_contentTypes();
clientContext.load(contentTypeCollection);
clientContext.executeQueryAsync(Function.createDelegate(
this
,
this
.onQuerySucceeded), Function.createDelegate(
this
,
this
.onQueryFailed));
}
function
onQuerySucceeded(sender, args) {
var
contentTypeEnumerator = contentTypeCollection.getEnumerator();
while
(contentTypeEnumerator.moveNext()) {
var
content = contentTypeEnumerator.get_current();
completeCTList.AddContentTypes(content.get_name());
}
}
function
onQueryFailed(sender, args) {
alert(
'Request failed. '
+ args.get_message() +
'\n'
+ args.get_stackTrace());
}
</script>
<!-- view - HTML markup that defines the appearance of your UI -->
<div id=
"divCTList"
>
<h2>
List Content Types</h2>
<br />
<table id=
"tblEmployeeList"
border=
"1"
>
<thead>
<tr>
<th>
Name
</th>
</tr>
</thead>
<!-- Iterating through every list item using foreach of KO -->
<tbody data-bind=
"foreach: ContentTypes"
>
<tr>
<td data-bind=
"text: Name"
>
</td>
</tr>
</tbody>
</table>
</div>
Output:
Summary:
Thus in this blog you saw how to get all the list Content Types using KnockoutJS and CSOM in SharePoint 2013.
How to get all the List Content Types using KnockoutJS and CSOM in SharePoint 2013
People also reading
Membership not found