getOAuthToken : async function() {
let realm = <the realm obtained in step 1>;
let principal = "00000003-0000-0ff1-ce00-000000000000";
let spClientId = <the client Id obtained in section 1> + “@” + realm;
let clientSecret = < the client secret obtained in section 1>
// find the domain name: if my SharePoint access url is
// https://mycompany.sharepoint.com/teams/MyProjectTeam/Shared%20Documents
// then the domain name would be “mycompany.sharepoint.com”
let domainName = " mycompany.sharepoint.com ";
let resource = principal + "/" + domainName + "@" + realm;
let url = "https://accounts.accesscontrol.windows.net/" + realm + "/tokens/OAuth/2";
let data =
"client_id=" + encodeURIComponent(spClientId) + "&" +
"client_secret=" + encodeURIComponent(client_secret) + "&" +
"resource=" + encodeURIComponent(resource) + "&" +
"grant_type=" + "client_credentials";
return new Promise( async (resolve, reject) => {
try {
let response = await axios.post(url, data);
resolve(response.data.access_token)
}
catch (error)
{
reject(error)
}
});
}
createFile : async function(accessToken, fileName, content) {
let folderName = 'Documents/' + <rootfolder/myfolder/mysubfolder…>;
//Notice the single quotes in the URL below. They should be part of the URL
let spUrl = <Base URL>/_api/web/GetFolderByServerRelativeUrl('<folderName>');
spUrl += "/Files/add(url='" + fileName + "',overwrite=true)";
return new Promise( async (resolve, reject) => {
try {
let result = await axios({method:"POST", url:spUrl, data:content,
headers:{Authorization:'Bearer ' + accessToken, "Accept":"application/json"}});
resolve("Success");
}
catch(error) {
reject(error);
}
});
}
deleteFile: async function(accessToken, fileName) {
let folderName = 'Documents/' + <rootfolder/myfolder/mysubfolder…>;
//notice the single quote in the URL below.
let spUrl = <Base URL>/_api/web/GetFolderByServerRelativeUrl('<folderName/fileName>');
return new Promise( async(resolve, reject) =>{
try {
let processResult = await axios({method:"DELETE", url: spUrl, data:{},
headers:{Authorization:'Bearer ' + accessToken, "Accept":"application/json"}});
resolve("Succeed.");
}
catch(error) {
reject(error);
}
});
}
getFilesList : async function(accessToken) {
let folderName = 'Documents/' + <rootfolder/myfolder/mysubfolder…>;
//notice the single quote in the URL below.
let url = <Base URL>/_api/web/GetFolderByServerRelativeUrl('<folderName>')/files?&$format=json;
return new Promise((resolve, reject)=>{
try {
axios({method:"GET", url:url, data:{}, headers:{Authorization:'Bearer ' + accessToken, "Accept":"application/json"}}).then(async (dataResp) => {
let filesArray = [];
for( let i=0; i< dataResp.data.value.length; i++) {
filesArray.push({Name:dataResp.data.value[i].Name, Timestamp: dataResp.data.value[i].TimeLastModified});
}
})
resolve (filesArray);
});
}
catch(error) {
reject(error);
}
});
}
getFileContent : async function(fileName, accessToken) {
let folderName = 'Documents/' + <rootfolder/myfolder/mysubfolder…>;
//notice the single quote in the URL below.
let spUrl = <Base URL>/_api/web/GetFolderByServerRelativeUrl('<folderName>')/files('<fileName>')/$value
return new Promise( async(resolve, reject) => {
try {
let response = await axios({method:"GET", url:spUrl, data:{},
headers:{Authorization:'Bearer ' + accessToken, "Accept":"application/json"}});
resolve(response.data);
}
catch(error) {
reject(error);
}
});
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
28 | |
13 | |
12 | |
11 | |
9 | |
9 | |
7 | |
6 | |
5 | |
5 |