Example: Lists.UpdateContentType ;

Jun 2, 2013 at 5:22 PM
Edited Jun 2, 2013 at 5:29 PM
I call the function with List GUID.

The scenario I have, I've modified the Event content type for my needs in a custom content type. Now, I am dynamically creating "Calendar" Lists for users, and I want to have this custom Content Type present without my having to do anything special.

After creating my Event List (templateID: 106), I call the function below to attach my Custom Content Type.

updateContentType(List_GUID);
function updateContentType(List_GUID) {
    //Custom content type GUID from SP Designer, inherits Event
    var customEvents = "0x01020097ED0AB4EBD6A84BA9A44AA73E7C5E3B";
    //Placeholder for org content type. When I initially tried to use 0x0102 I 
    //would get errors
    var orgContentId = "";
    
    // ========= Add custom content type with workflows
    jQuery().SPServices({
        webURL: "/MySiteCol/MyUserSite",
        operation: 'ApplyContentTypeToList',
        listName: List_GUID,
        contentTypeId: customEvents,
        async: false,
        completefunc: function(xData, Status) {
            // error checking/loggin here, if you want
        } // end completedfunc
    }); // end spcall

    // ========= use GetListContentTypes to find ContentTypeID of "Events"
    jQuery().SPServices({
        webURL: "/MySiteCol/MyUserSite",
        operation: 'GetListContentTypes',
        listName: List_GUID,
        async: false,
        completefunc: function(xData, Status) {
            jQuery(xData.responseXML).find("ContentType").each(function() {
                 if (jQuery(this).attr("Name") == "Event") orgContentId = jQuery(this).attr("ID");
            }); // end for each
        } // end completedfunc
    }); // end spcall

    // ====== Remove default content type
    jQuery().SPServices({
        webURL: "/MySiteCol/MyUserSite",
        operation: 'DeleteContentType',
        listName: List_GUID,
        contentTypeId: orgContentId,
        async: false,
        completefunc: function(xData, Status) {
            //perform your error checking/logging here
        } // end completedfunc
    }); // end spcall
} // enc func
Sorry for formatting. Thanks again for great library.