1

For one of the Requirements I am working on, I have a choice drop-down in a document library. Basically when a user uploads a document in the library they have to populate the Content type from the drop-down, please see the below screenshot.

enter image description here

What I want to do is the following: 1. I want to get the value from the Content type drop-down and then get its id. IF the user changes the content type selection before clicking on Save, it should account for that and give me the right ID for content type selection.

  1. Then I would like to get the list of content types associated with that Library called "Finance" and get the relevant IDs.

  2. Lastly, I would want to match ID I get from (1) with the ID I Get from (2), if it matches, my script will do something.

I would like to achieve this using JSOM, so if someone knows please help with the code.

2

You can enhance below code as per your need: Below code shows you how to get the content type associated with a list and get id of a particular content type:

<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("Finance");  // 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>   
  • thanks, this is one portion, how to get the id of content type selection from the choice drop-down in the library itself? – mdevm May 17 '18 at 12:46
  • call something like below on dropdown change event: var web = clientContext.get_web(); this.contentTypeCollection = web.get_contentTypes(); console.log(contentTypeCollection); this.contentType = contentTypeCollection.getByName("CTName"); clientContext.load(this.contentType); – Deepmala May 17 '18 at 12:54
  • thanks much, your code was very helpful. I have a question though, I am able to get the ID for Project Financing content type using the Code you provided and also I am able to get the ID of a Content type drop-down selection that user selects. The Issue is I would want to access the ID returned by getListContentTypes outside the function and match this id with the ID of content type drop-down selection id, any help will be appreciated. – mdevm May 17 '18 at 15:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.