Domanda

Sto cercando di eseguire la stessa funzione 3 volte di seguito, ma ottenere errori mentre lo fai.Apparentemente gli script vengono eseguiti più velocemente dell'esecuzione.Codice:

$("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...");
        });
}
.

Fondamentalmente quello che voglio è: 1) Controlla se perseguit univoche, se non allora break -heritance 2) Crea 3 nuovi gruppi se non esistono ancora.

Tuttavia, eseguendo la funzione CreateGroup tre volte come questo non funziona.Come posso aspettare con l'esecuzione di una funzionale fino alla funzione precedente è totalmente finita ??

Tutto il Guida sarà molto apprezzato !!!!

È stato utile?

Soluzione

Usa promesse jQuery.Implementazione di base:

var deferred = $.Deferred();

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

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a sharepoint.stackexchange
scroll top