Ectropy
Ectropy

Reputation: 1541

Programatically enable Content Approval (Moderation) in SharePoint List

I am writing a app that creates a custom events list.

I'd like to enable content approval (also referred to Moderation on the back end) upon creation of the list.

Here's what my list-creation code looks like.

    function createList(listToCreate)
{
    // Create a SharePoint list with the name that the user specifies.
    var hostUrl = decodeURIComponent(getQueryStringParameter("SPHostUrl"));
    var hostContext = new SP.AppContextSite(currentContext, hostUrl);
    var hostweb = hostContext.get_web();
    var listCreationInfo = new SP.ListCreationInformation();

    //title the list
    listCreationInfo.set_title(listToCreate);

    //set the base type of the list
    listCreationInfo.set_templateType(SP.ListTemplateType.events);

    var lists = hostweb.get_lists();
    //use the creation info to create the list
    var newList = lists.add(listCreationInfo);

    var fieldCollection = newList.get_fields();

    //add extra fields (columns) to the list & any other info needed.
    fieldCollection.addFieldAsXml('<Field Type="User" DisplayName="Requester" Name="Requester" />', true, SP.AddFieldOptions.AddToDefaultContentType);
    fieldCollection.addFieldAsXml('<Field Type="User" DisplayName="Manager" Name="Manager" />', true, SP.AddFieldOptions.AddToDefaultContentType);
    fieldCollection.addFieldAsXml('<Field Type="Boolean" DisplayName="Approved" Name="Approved" />', true, SP.AddFieldOptions.AddToDefaultContentType);

    //Attempting to enable moderation. This doesn't seem to have any effect.
    newList.set_enableModeration(true);

    currentContext.load(fieldCollection);
    currentContext.load(newList);
    currentContext.executeQueryAsync(onListCreationSuccess, onListCreationFail);     
}

function onListCreationSuccess() {
    alert("We've created a list since one didn't exist yet. Look in the site that hosts this app for the list." );
}

function onListCreationFail(sender, args) {
    //alert("We didn't create the list. Here's why: " + args.get_message());
}

Unfortunately it seems that .set_enableModeration(true); has no effect. I don't get any errors, but when I look at the settings for the list I created using this code I see this: enter image description here

So Content Approval clearly was not enabled via the method I'm using.

Upvotes: 1

Views: 1773

Answers (1)

Vadim Gremyachev
Vadim Gremyachev

Reputation: 59348

In order to set Content Approval using SP.List.enableModeration Property, the SP.List.update() Method have to be called as demonstrated below:

list.set_enableModeration(true);
list.update();

Upvotes: 2

Related Questions