0

I am using client code using JQuery and rest API where I have a requirement to bind a drop down control with the content type names associated with a particular list.

I am working on SharePoint 2016 on-premise.

Any help on this would be highly appreciated

2 Answers 2

3

Try using below endpoint:

http://<sitecollection>/<site>/_api/web/lists/getbytitle('Display name of List')/contenttypes

OR

http://<sitecollection>/<site>/_api/web/lists(listid)/contenttypes

official documentation: SP.ContentTypeCollection object - REST resource endpoint.

Update:

Try using something like below to fetch the names of content type. This is like pseudo code for your reference. You can check the exact data you are getting from REST call by adding break-point inside success function:

$.ajax({
    url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/Lists/getbytitle('Display name of List')/contenttypes?$select=Name,Id",
    type: "GET",
    headers: {
        "Content-Type": "application/json;odata=verbose"
        "Accept": "application/json;odata=verbose"
    },
    success: function(data) {
        var optionsArray = [];
        if(data.d && data.d.results) {
            for(var i = 0; i < data.d.results.length; i++) {
                console.log(data.d.results[i].Name);
                // Push the content type name in array
                optionsArray.push(data.d.results[i].Name);
            }
        }
    },
    error: function(error) {
        console.log(error);
    }
});
4
  • http://<sitecollection>/<site>/_api/web/lists(listid)/contenttypes , this gives me a list of content type. Can you please suggest me how to get all those values in an array please so that I can bind them in the dropdown control
    – Muskan
    Commented Dec 5, 2019 at 8:44
  • 1
    try to run the loop on data.d.results in success function of GET call Commented Dec 5, 2019 at 8:53
  • Try using the code given in update section in my answer and let me know if it works for you or if you face any issues. Commented Dec 5, 2019 at 11:15
  • 1
    Thank you so much Ganesh. It worked perfectly ! Much Appreciated, Thank once again :)
    – Muskan
    Commented Dec 6, 2019 at 13:23
0

With GET request you can get all the content types of your specified list

.../_api/web/lists(ID)/contenttypes

I recommend to use extension in your Browser Called SharePoint Insider, it will help you a lot to figure out the endpoints that will help you to build your REST request

Best regards.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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