Search code examples
azure-devopsazure-devops-extensions

How to make data storage unique to project in VSTS extension?


    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??


Solution

  • 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.