<script type=”text/javascript”>
window.onload = function() {
getListContentTypes();
};
var contentTypeCollection;
var listCollection;
var list;
function getListContentTypes() {
var clientContext = new SP.ClientContext.get_current();
if (clientContext != undefined && clientContext != null) {
var web = clientContext.get_web();
this.listCollection = web.get_lists();
this.list = listCollection.getByTitle(“ContactList”); // Provide the needed List or Document Library Name
this.contentTypeCollection = list.get_contentTypes();
clientContext.load(this.contentTypeCollection);
clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
}
}
function onQuerySucceeded() {
var contentType = ‘Associated Content Types:\n ‘
var contentTypeEnumerator = this.contentTypeCollection.getEnumerator();
while (contentTypeEnumerator.moveNext()) {
var content = contentTypeEnumerator.get_current();
if(content.get_name()==”Employee”){ // provide the Name of content Type
alert(“id=”+content.get_id()); // Displays the ID of Employee Content Type
}
contentType += content.get_name() + ‘\n’;
}
alert(contentType); // Displays all the content type names that are associated with ContactList
}
function onQueryFailed(sender, args) {
alert(‘Request failed. ‘ + args.get_message() + ‘\n’ + args.get_stackTrace());
}
</script>
Description: ( I guess the script is self-explanatory)
- Get the current Client Context using SP.ClientContext.get_current
- Load the List for which need to find the associated Content Types
- In success function iterate through the result of ContentTypeCollection.
- Check for each item whether the Name matches and obtain the ID if it matches.
- Display all the Content Type name that are associated with this List
