3

I have a Sharepoint Hosted app in which I have some javascript (permission related) that only should run once. It should run only when the app is trusted by the admin that installs the app and when the app home page is loading the first time.

Found something on google that proposes to set some "_AppInit" property bag value, but I can't find that property bag anywhere...

Thanks!

2 Answers 2

1

I had to something similar with one of my SP-Hosted apps. This how I accomplished it:

function getInitProp() {
    var oDataUrl = appweburl + "/_api/web/AllProperties/?$select=_AppInit";
    $.ajax({
        url: oDataUrl,
        type: 'GET',
        headers: { "accept": "application/json;odata=verbose" },
        success: getInitPropSuccess,
        error: errorHandler
    });
}

function getInitPropSuccess(data) {
    if(data.d.OData__x005f_AppInit) {
        $("#message").text("Already Initialized");
    }else {
        getFormDigest();
    }
}

function setInitProp() {
    var ctx = SP.ClientContext.get_current();
    var web = ctx.get_web();
    var properties = web.get_allProperties();
    properties.set_item("_AppInit", true);
    web.update();
    ctx.load(web);
    ctx.executeQueryAsync(function() {
        console.log("Successfully configured app permissions");
        $("#message").text("Successfully configured app");
    }, function() {
        console.log("Error configuring app permissions");
    });    
}

If I can find the link I used to come up with this, I'll post it. It was quite awhile ago.

2
  • that's what I wanted to do, but somehow this doesn't work since the property *_AppInit is not listed... Sep 30, 2014 at 18:20
  • YOu have to set it first. If it's not set, it does not exist, I believe.
    – wjervis
    Sep 30, 2014 at 18:29
0

OK figured it out by using a hidden list :). What I did was: - create a standard list with 2 columns "ConfigProcess" and "Boolean". - added added a row to the list in the Elements.xml of the list instance with values {"Installation", True } - added the following JS:

$("document").ready(function () {
      retrieveConfigParam("Installation"); //calls fxSetPermissions through     fxSuccess if CamlQuery returns TRUE, skips if FALSE.  
});

function retrieveConfigparameter, calls function CamlSuccess on success:

function retrieveConfigParam(ConfigProcess) {

    var ctx = SP.ClientContext.get_current();
    var oList = ctx.get_web().get_lists().getByTitle('lstConfigParameters');

    var camlQuery = new SP.CamlQuery();
    camlQuery.set_viewXml(
        '<Query><Where><Eq><FieldRef Name=' + ConfigProcess + ' /><Value  Type=”Text”>Installation</Value>' +
        '</Eq></Where></Query>');
    this.collListItem = oList.getItems(camlQuery);

    ctx.load(collListItem);
    ctx.executeQueryAsync(Function.createDelegate(this, this.CamlSuccess),     Function.createDelegate(this, this.fxFail));

}

function CamlSuccess, runs my JS script (fxSetPermissions) that has to run once if Boolean = True. Then runs fxUpdateListItem to set Boolean=false for "ConfigProcess=Installation:

function CamlSuccess () {

    var listItemInfo = '';
    var listItemEnumerator = collListItem.getEnumerator();

    while (listItemEnumerator.moveNext()) {
        var oListItem = listItemEnumerator.get_current();
        listItemInfo += oListItem.get_item('Boolean');
        var xID = oListItem.get_id();
        alert("xID == " + xID);

    }

    alert(listItemInfo.toString());
    if (listItemInfo == "true") {
        alert("set permissions...");
        fxSetPermissions();
        fxUpdateListItem(xID);
    }
}

And finally fxUpdateListItem to set Boolean=false:

function fxUpdateListItem(xID) {
    var ctx = SP.ClientContext.get_current();
    var oList = ctx.get_web().get_lists().getByTitle('lstConfigParameters');
    this.oListItem = oList.getItemById(xID);
    oListItem.set_item('Boolean', false);
    oListItem.update();
    ctx.executeQueryAsync(
        Function.createDelegate(this, this.fxUpdated),
        Function.createDelegate(this, this.fxFailed)
    );
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.