Wednesday, May 24, 2017

IManage create workspace with folder structure

/////////////////////////////////////////////////////////////////////////////////////////
$.ajaxSetup({async: false});
var cache = {};
cache[ "group_id" ] ="";//"blah"
cache["baseurl"] = 'https://blah/api/v1/';
cache["user_id"] = "ws_creator";
cache["pwd"] = "test";
cache["FolderIds"] =[];

function getToken( ){
console.log("getToken");
//debugger;
    return $.when(
        $.ajax({
url: cache["baseurl"] +'session/login',
type: 'put',
data: JSON.stringify({
user_id:cache["user_id"],
password:cache["pwd"]
}),
headers: {
'Content-Type': 'application/json'
},
dataType: 'json',
            success: function( resp ){
                cache[ "Token"  ] = resp["X-Auth-Token"];
console.log("getToken:success");
            },
failure: function( resp ){
console.log("getToken:failure");
console.log(resp);
            }
        })
    );
}
/////////////////////////////////////////////////////////////////////////////////////////
function CreateWorkspace(ClientID , EntityID, wsName, groupId,accessType){
console.log("CreateWorkspace");
//debugger;
    return $.when(
        $.ajax({
url: cache["baseurl"] + 'workspaces',
type: 'post',
data: JSON.stringify({
database: "Blah",
custom1:ClientID ,
custom2:EntityID ,
default_security: "private", //change back to private
name: wsName
}),
headers: {
'X-Auth-Token':cache[ "Token"  ],
'Content-Type': 'application/json'
},
dataType: 'json',
            success: function( resp ){
//debugger;
cache[ "group_id"  ] = groupId;
addGroup(resp.data.id, "workspaces", accessType);
                cache[ "workspace_id"  ] = resp.data.id;
console.log("CreateWorkspace:success");
            },
failure: function( resp ){
console.log("CreateWorkspace:failure");
console.log(resp);
            }
        })
    );
}
/////////////////////////////////////////////////////////////////////////////////////////
function AddFolderToWorkspace(fname,accessType,documentGroup ){
console.log("AddFolderToWorkspace");
//debugger;
    return $.when(
        $.ajax({
url: cache["baseurl"] +'workspaces/' + cache[ "workspace_id"  ]  + '/folders',
type: 'post',
data: JSON.stringify({
 database:"Blah",
 name:fname,
 default_security:"inherit",
 profile:
  {
 author:cache["user_id"],
 class:"DOC",
     comment:"",
 custom29:documentGroup
        }
}),
headers: {
'X-Auth-Token':cache[ "Token"  ],
'Content-Type': 'application/json'
},
dataType: 'json',
            success: function( resp ){
//debugger;
//cache[ "group_id"  ] = ""; //remove
addGroup(resp.data.id, "folders", accessType);
cache[ "FolderIds"].push({"root":"","name":fname,id :resp.data.id});
console.log("AddFolderToWorkspace:success");
            },
failure: function( resp ){
console.log("AddFolderToWorkspace:failure");
console.log(fname);
console.log(resp);
            }
        })
    );
}
/////////////////////////////////////////////////////////////////////////////////////////////
/*
access levels
"no_access", "read", "read_write", "full_access"

what are the possible values for user types?
"user" or "group"
*/
function addGroup(objectID, type, accessType){
//type documents, workspaces, folders
//debugger;
console.log("cache[ 'group_id'] " + cache[ "group_id" ]  );
if(cache[ "group_id" ] !==""){
 return $.when(
   $.ajax({
url: cache["baseurl"] + type +  '/'  + objectID + "/security",
type: 'post',
headers: {
'X-Auth-Token':cache[ "Token"  ],
'Content-Type': 'application/json'
},
dataType: 'json',
data: JSON.stringify({
include:
[{access_level: accessType,
id: cache[ "group_id" ],
type: "group"
}]
}),
            success: function( resp ){
//debugger;
//user_type
               //  cache[ "doc_id"  ] = resp.data[0].id;
console.log("addGroup:success");
            },
failure: function( resp ){
//debugger;
console.log("addGroup:failure");
console.log(resp);
            }
        })
    );
}
}
/////////////////////////////////////////////////////////////////////////////////////////
function AddFolderToFolder(root, father, fname ,accessType,taxYear){
console.log("AddFolderToFolder");
console.log(father + "-" + fname);
//debugger;
    return $.when(
        $.ajax({
url: cache["baseurl"] +'workspaces/' + cache["FolderIds"].filter(function (el) {
return el.name == father && el.root == root;
})[0].id + '/folders',
type: 'post',
data: JSON.stringify({
 database:"Blah",
 name:fname,
 default_security:"inherit",
 profile:
  {
 author:cache["user_id"],
 class:"DOC",
     comment:"",
 custom4:taxYear,
 custom29:father
        }
}),
headers: {
'X-Auth-Token':cache[ "Token"  ],
'Content-Type': 'application/json'
},
dataType: 'json',
            success: function( resp ){
addGroup(resp.data.id, "folders", accessType);
cache[ "FolderIds"].push({"root" : father, name:fname,id :resp.data.id});
console.log("AddFolderToFolder:success");
            },
failure: function( resp ){
console.log("AddFolderToFolder:failure");
console.log(fname);
console.log(resp);
            }
        })
    );
}

/////////////////////////////////////////////////////////////////////////////////////////
getToken()//ClientID , EntityID, wsName, groupId,accessType
    .then(CreateWorkspace("11274","438917","test 2","Blah","read"), function(err) {console.log( err );})
.then(AddFolderToWorkspace("Billing","read","Billing"), function(err) {console.log( err );})
//root, father, fname ,accessType,taxYear
.then(AddFolderToFolder("","Billing","31-Dec-2017","full_access","2017"), function(err) {console.log( err );})
.then(AddFolderToFolder("","Billing","31-Dec-2016","full_access","2016"), function(err) {console.log( err );})

1 comment:

  1. isn't this supported by iManage that you link a template with Workspace (at the time of creation or afterwards) and iManage read that template and create folders hierarchy automatically?

    ReplyDelete