none
Using REST retrieve list of Content Type Groups

    Question

  • when looking at content types in site settings they are grouped in categories like 'Business Intelligence', etc is there a way to extract this listing via REST?

    I know the group that the individual content type belongs to is shown on the group property of each CT.

    Thanks

    MrP

    Wednesday, August 27, 2014 3:24 PM

Answers

  • Hi,

    According to your post, my understanding is that you want to get the content type groups.

    We can use the following REST endpoint to get the all the content types and the groups of a site.

    http://YourSite/_api/web/contenttypes

    Then we can extract the group via REST, I have made a simple code demo below to get the groups, you can have a look at it.

    <script src="http://code.jquery.com/jquery-1.10.2.min.js" type="text/javascript"></script>
    
    <script type="text/javascript">
    
    $(function(){
    
                    $("#Button1").click(function(){
    
                                    //alert(1);
    
                                    ReadAllGroup();
    
                    });
    
    });
    
    function ReadAllGroup() {
    
                    //alert(2);
    
        var url = _spPageContextInfo.webAbsoluteUrl;
    
        //alert(url);
    
        getContentType(url, success_CT, failure_CT);
    
        
    
    }              
    
    function getContentType(siteurl, success, failure) {
    
        $.ajax({
    
            url: siteurl + "/_api/web/contenttypes",
    
            method: "GET",
    
            headers: { "Accept": "application/json; odata=verbose" },
    
            success: function (data) {
    
                success(data);
    
            },
    
            error: function (data) {
    
                failure(data);
    
            }
    
        });
    
    }
    
    function success_CT(data){
    
                    var items = data.d.results;
    
                   //alert(items);   
    
        // Add all the new items
    
        for (var i = 0; i < items.length; i++){
    
              alert(items[i].Group+"|"+items[i].Name);
    
        }
    
    }
    
    function failure_CT(data){
    
                    alert("Ooops, an error occured. Please try again");
    
    }
    
    </script>
    
    <input id="Button1" type="button" value="Get Content Type and Group"/>
    

    Thanks,

    Jason

    Forum Support

    Please remember to mark the replies as answers if they help and unmark them if they provide no help. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.


    Jason Guo
    TechNet Community Support

    • Marked as answer by mrP Friday, August 29, 2014 5:54 AM
    Thursday, August 28, 2014 3:39 AM
    Moderator

All replies

  • Hi,

    According to your post, my understanding is that you want to get the content type groups.

    We can use the following REST endpoint to get the all the content types and the groups of a site.

    http://YourSite/_api/web/contenttypes

    Then we can extract the group via REST, I have made a simple code demo below to get the groups, you can have a look at it.

    <script src="http://code.jquery.com/jquery-1.10.2.min.js" type="text/javascript"></script>
    
    <script type="text/javascript">
    
    $(function(){
    
                    $("#Button1").click(function(){
    
                                    //alert(1);
    
                                    ReadAllGroup();
    
                    });
    
    });
    
    function ReadAllGroup() {
    
                    //alert(2);
    
        var url = _spPageContextInfo.webAbsoluteUrl;
    
        //alert(url);
    
        getContentType(url, success_CT, failure_CT);
    
        
    
    }              
    
    function getContentType(siteurl, success, failure) {
    
        $.ajax({
    
            url: siteurl + "/_api/web/contenttypes",
    
            method: "GET",
    
            headers: { "Accept": "application/json; odata=verbose" },
    
            success: function (data) {
    
                success(data);
    
            },
    
            error: function (data) {
    
                failure(data);
    
            }
    
        });
    
    }
    
    function success_CT(data){
    
                    var items = data.d.results;
    
                   //alert(items);   
    
        // Add all the new items
    
        for (var i = 0; i < items.length; i++){
    
              alert(items[i].Group+"|"+items[i].Name);
    
        }
    
    }
    
    function failure_CT(data){
    
                    alert("Ooops, an error occured. Please try again");
    
    }
    
    </script>
    
    <input id="Button1" type="button" value="Get Content Type and Group"/>
    

    Thanks,

    Jason

    Forum Support

    Please remember to mark the replies as answers if they help and unmark them if they provide no help. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.


    Jason Guo
    TechNet Community Support

    • Marked as answer by mrP Friday, August 29, 2014 5:54 AM
    Thursday, August 28, 2014 3:39 AM
    Moderator
  • thanks - so you have to populate the list yourself - no direct api for accessing them.

    I modified your code above to populate a distinct list - for use in my app.

    var contentTypeGroups=[];
        exec.executeAsync({
                url: appweburl + "/_api/SP.AppContextSite(@target)/web/ContentTypes?$select=ID,Name,Group&@target='"
                    + hostweburl + "'",
                method: "GET",
                headers: { "Accept": "application/json; odata=verbose" },
                success: function (data) {
                    var items = JSON.parse(data.body);
                    for (var i = 0; i < items.d.results.length; i++) {
                        if (contentTypeGroups.indexOf(items.d.results[i].Group)<0)
                            contentTypeGroups.push(items.d.results[i].Group);
                    }
                },
                error: function (error) {
                    console.log(JSON.stringify(error));
                }
            });
    


    Thursday, August 28, 2014 3:36 PM
  • Hi,

    Yes, you are right, there is no directly way to get the list of content type groups using REST API.

    As a workaround, we can populate the content type groups list as the above code snippets.

    Thanks,

    Jason

    Forum Support

    Please remember to mark the replies as answers if they help and unmark them if they provide no help. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.


    Jason Guo
    TechNet Community Support

    Friday, August 29, 2014 1:25 AM
    Moderator