This example demonstrates how to set hierarchy, measure adn member level security in a data model programmatically.
The example uses API authentication driven from JavaScript. See Authentication APIs for alternatives.
var pyramidURL = "http://mysite.com/api2/";
let token = callApi("auth/authenticateUser",{
"data":{
"userName":"adminUser",
"password":"abc123!"
}
},false);
log("got token "+token);
let server= callApi("dataSources/findServerByName ",{
"searchCriteria": {
"searchValue":"Pyramid IMDB"
},
"auth": token
});
let serverId = server.data[0].itemId
let database= callApi("dataSources/findDatabaseByName ",{
"searchCriteria":{
"dataContainerId":serverId,
searchCriteria:{
"searchValue":"PyramidDemo"
}
},
"auth": token
});
let databaseId = database.data[0].itemId
let model= callApi("dataSources/findModelByName ",{
"searchCriteria":{
"dataContainerId":databaseId,
searchCriteria:{
"searchValue":"PyramidDemo"
}
},
"auth": token
});
let modelId = model.data[0].itemId
let role= callApi("access/getRolesByName ",{
searchCriteria:{
"searchValue":"role 1"
},
"auth": token
});
let roleId = role.data[0].roleId
let addMembersSecurityToModel= callApi("dataSources/addMembersSecurityToModel ",{
"hierarchyMeasureSecurityApiObject":{
"modelId":modelId,
"roleId":roleId,
"enableMode": true,
"hierarchyUniqueName": "[Date].[year]",
"members":["2009", "2010"]
},
"auth": token
});
let addMembersSecurityToModel= callApi("dataSources/addMembersSecurityToModel ",{
"hierarchyMeasureSecurityApiObject":{
"modelId":modelId,
"roleId":roleId,
"enableMode": true,
"hierarchyUniqueName": "[Geography].[Country]",
"script":"NonEmpty([Geography].[Country].AllMembers, {StrToMember([Security].[User],UserName())})"
},
"auth": token
});
let addMembersSecurityToModel= callApi("dataSources/addMembersSecurityToModel ",{
"hierarchyMeasureSecurityApiObject":{
"modelId":modelId,
"roleId":roleId,
"enableMode": true,
"hierarchyUniqueName": "[Security].[User]",
"script":"StrToMember([Org].[UserIds],UserName())"
},
"auth": token
});
let addHierarchySecurityToModel= callApi("dataSources/addHierarchySecurityToModel ",{
"hierarchyMeasureSecurityApiObject":{
"modelId":modelId,
"roleId":roleId,
"enableMode": false,
"uniqueNames": ["[Customer].[State]"]
},
"auth": token
});
let addMeasureSecurityToModel= callApi("dataSources/addMeasureSecurityToModel ",{
"hierarchyMeasureSecurityApiObject":{
"modelId":modelId,
"roleId":roleId,
"enableMode": true,
"uniqueNames": ["[measures].[Cost]"]
},
"auth": token
});
function log(msg){
document.write(msg);
console.log(msg);
}
function callApi(path,data,parseResult=true){
var xhttp = new XMLHttpRequest();
xhttp.open("POST", pyramidURL+path, false);
xhttp.send(JSON.stringify(data));
if(parseResult){
return JSON.parse(xhttp.responseText);
}else{
return xhttp.responseText;
}
}