Pregunta

Estoy tratando de ejecutar la misma función 3 veces seguidas, pero obtenga errores al hacerlo.Aparentemente, los scripts se ejecutan más rápido que la ejecución.Código:

$("document").ready(function () {
    WebInherited();
    CreateGroup("Group1", "Description group 1");
    CreateGroup("Group2", "Description group 2");
    CreateGroup("Group3", "Description group 3");
});

function WebInherited() {
    var c = new SP.ClientContext();
    this.w = c.get_web();
    c.load(w, "HasUniqueRoleAssignments");
    c.executeQueryAsync(
        Function.createDelegate(this, this.fxLoad),
        Function.createDelegate(this, this.fxFail)
        );
}

function fxLoad() {
    console.log("hasUniqueRoleAssignments == " +   w.get_hasUniqueRoleAssignments());
    if (!w.get_hasUniqueRoleAssignments()) {
        console.log("Obtaining web's permission properties...");
        var ctx = new SP.ClientContext();
        web = ctx.get_web();
        ctx.load(web, "RoleAssignments", "RoleDefinitions");
        ctx.executeQueryAsync(
            Function.createDelegate(this, this.fxBreak(web)),
            Function.createDelegate(this, this.fxFail)
            );
    }
}

function fxBreak(web) {
    console.log("Breaking Inheritance...");
    web.breakRoleInheritance(false, true);

}

function fxFail() {
    console.log("faal");
}

function CreateGroup(sName, sDescr) {
    console.log("Loading site group data and checking if " + sName + " should be created...");
    groupName = sName;
    groupDescr = sDescr;
    var ctx = new SP.ClientContext();
    web = ctx.get_web();

    //group collection
    groups = web.get_siteGroups();

    //checken of group al bestaat
    ctx.load(groups);
    ctx.executeQueryAsync(
        Function.createDelegate(this, this.fxCheckIfExists),
        Function.createDelegate(this, this.fxFail)
    );    
}

function fxCheckIfExists() {
    console.log("groups query succeeded");
    var grpEnum = groups.getEnumerator();
    var cnt = 0;
    while (grpEnum.moveNext()) {
        var grpItem = grpEnum.get_current();
        console.log(grpItem.get_title());
        if (grpItem.get_title() == groupName) {
            cnt += 1;
            console.log("Group " + groupName + " already exists.")
            break;
        }
    }
    if (cnt == 0) {
        console.log("Group " + groupName + " does not exist")
        fxCreate(groupName, groupDescr);
    }
}

function fxCreate(groupName, groupDescr) {
    console.log("Creating group " + groupName + "...");
    var ctx = new SP.ClientContext();
    web = ctx.get_web();
    var groups = web.get_siteGroups();

    //info nieuwe group
    var infoGroup = new SP.GroupCreationInformation;
    infoGroup.set_title(groupName);
    infoGroup.set_description(groupDescr);

    //nwe group toevoegen aan group collection
    var oGroup = groups.add(infoGroup);

    //rol definieren, 1073741827=Contributor
    var rdContribute = web.get_roleDefinitions().getById(1073741827);

    //role collection ophalen en nwe rol toevoegen
    var collRoleDefBindings =   SP.RoleDefinitionBindingCollection.newObject(ctx);
    collRoleDefBindings.add(rdContribute);
    var assignments = web.get_roleAssignments();
    var raContribute = assignments.add(oGroup, collRoleDefBindings);

    //group creeren in ctx
    ctx.load(oGroup);
    ctx.executeQueryAsync(
        function () {
            console.log("group " + groupName + " gecreerd!")
        },
        function () {
            console.log("group " + groupName + " creeren mislukt...");
        });
}

básicamente lo que quiero es: 1) Compruebe si las persmisiones únicas, si no, entonces, la ruptura 2) Crea 3 grupos nuevos si aún no existen.

Sin embargo, ejecutar la función CreateGroup-Function tres veces, así, no funciona.¿Cómo puedo esperar con la ejecución de una función hasta que la función anterior está totalmente terminada?

¡Toda ayuda será muy apreciada !!!!

¿Fue útil?

Solución

Usa jquery promesas.Implementación básica:

var deferred = $.Deferred();

deferred.then(function() {
//do callback to check unique permissions
}).then(function(){
//create groups
});

Licenciado bajo: CC-BY-SA con atribución
scroll top