VSS.getService(VSS.ServiceIds.ExtensionData)
// the callback on the next line returns a promise, which the JavaScript engine will follow, so you don't need to nest the next `then`
.then((dataService) => dataService.getDocuments('MyCollection2'))
.then((docs) => { ...
This is how we access data storage in VSTS extension.
MyCollection2
is a name of the storage that we are using.
However, this is not unique to the project. When I try to access the hub extension from another project within the same organization, I can still see the data.
I tried to dynamically name the collection based on the project that I access, but there is no clear way to get the project name on the extension side.
How can I make the data storage unique to the project within the same organization??
You could get project name from getWebContext(). Then set the DocumentId value to the ProjectName:
// Get data service
VSS.getService(VSS.ServiceIds.ExtensionData).then(function(dataService) {
// Set value (default is project collection scope)
dataService.setValue("ProjectName", "DocumentId").then(function(value) {
console.log("Key value is " + value);
});
});
After that, retrieve the setting value:
// Get data service
VSS.getService(VSS.ServiceIds.ExtensionData).then(function(dataService) {
// Get value in user scope
dataService.getValue("ProjectName").then(function(value) {
console.log("User scoped key value is " + value);
});
});
Last, when you get the document from the DocumentId, it will get from a project:
// Get data service
VSS.getService(VSS.ServiceIds.ExtensionData).then(function(dataService) {
// Get document by id
dataService.getDocument("MyCollection", "DocumentId").then(function(doc) {
// Assuming document has a property named foo
console.log("Doc foo: " + doc.foo);
});
});
I actually fixed this by using a Promise.
// Sets unique data storage name for each project
function setDBName() {
return new Promise(function(resolve, reject) {
VSS.ready(function(){
let web = VSS.getWebContext();
resolve(web);
})
})
}
then
setDBName()
.then(function(res){
console.log('res', res)
}
.catch(function(err){
console.log('err', err)
}
This will return the web context whenever VSS is ready to get fetched.