Question

I'm working with Skydrive APIs, and I would like my user tobe able to open a view about a file where you can edit it (the same view as we can have about a file when you're on the skydrive web page).

There may be a WL function for it but I can't find it. An other solution would be for me to get the URL of the view page and open it in a new window with javascript.

Was it helpful?

Solution

I have implemented this solution using SkyDrive and its API. You can try this script out within Microsoft's Interactive Live SDK online tool as well. The key is to obtain SkyDrive's redirect link for the file you are looking to open. This redirect link is returned for each file in the Get api's json result.

Processing Steps:

  1. Initializes Windows Live jscript api client
  2. Authenticate with Windows Live (skydrive) OAuth
  3. GetFiles: Get a list of all files within your SkyDrive account. This could be adjusted for your needs and focused to just get a list for a specific folder with your SkyDrive account
  4. onFilesComplete: iterate through json response looking for an item with a type='file' and file name you are looking to open. In this, case a file name 'robots.txt'
    • display details about the found file
    • using the found file's "link" attribute, open url in a new window browser window. This will open the file using SkyDrive default action. For known file types such as code files, this will open them in SkyDrive's online file editor. Otherwise, the default action will be to download the found file

Example Code:

WL.init({ client_id: clientId, redirect_uri: redirectUri });

WL.login({ "scope": "wl.skydrive" }).then(
    function(response) {
        getFiles();
    },
    function(response) {
        log("Could not connect, status = " + response.status);
    }
);

function getFiles() {
    var files_path = "/me/skydrive/files";
    WL.api({ path: files_path, method: "GET" }).then(
        onGetFilesComplete,
        function(response) {
            log("Cannot get files and folders: " +
                JSON.stringify(response.error).replace(/,/g, ",\n"));
        }
    );
}

function onGetFilesComplete(response) {
    var items = response.data;
    var foundFolder = 0;
    for (var i = 0; i < items.length; i++) {
        if (items[i].type === "file" &&
            items[i].name === "robots.txt") {
            log("Found a file with the following information: " +
                JSON.stringify(items[i]).replace(/,/g, ",\n"));
            foundFolder = 1;
            //open file in a new browser window
            window.open(items[i].link);
            break;
        }
    }

    if (foundFolder == 0) {
        log("Unable to find any file(s)");
    }
}

function log(message) {
    var child = document.createTextNode(message);
    var parent = document.getElementById('JsOutputDiv') || document.body;
    parent.appendChild(child);
    parent.appendChild(document.createElement("br"));
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top