-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLoop through object items (customfieldProjects).js
More file actions
47 lines (41 loc) · 1.91 KB
/
Loop through object items (customfieldProjects).js
File metadata and controls
47 lines (41 loc) · 1.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// This code sample will get all projects custom field data > Loop through each item in the customfieldProjects object > Printout the customfieldId, customFieldValueId and the value of the custom field if a value exists.
// Params included:
// - includeCompletedProjects=true
// - includeCustomFields=true
// - optional parameter to filter by customfield Value `&projectCustomField[4875][eq]=Needs attention`
// Endpoint documentation: https://apidocs.teamwork.com/docs/teamwork/v3/projects/get-projects-api-v3-projects-json
const myHeaders = new Headers();
const userName = "email address or API KEY here";
const password = "password";
const siteName = "yourSiteName";
const filterCustomField = "";//&projectCustomField[4875][eq]=Needs attention
let loop = true
let page = 1
myHeaders.append("Authorization", "Basic " + btoa(userName + ":" + password));
const requestOptions = {
method: "GET",
headers: myHeaders,
redirect: "follow"
};
async function fetchProjects() {
do {
let v3ProjectsUrl = `https://${siteName}.teamwork.com/projects/api/v3/projects.json?includeCompletedTasks=true&includeCustomFields=true&page=${page}${filterCustomField}`
const response = await fetch(v3ProjectsUrl, requestOptions)
let data = await response.json()
//console.log(data)
var customfieldProjects = data["included"]["customfieldProjects"];
var hasMore = data["meta"]["page"]["hasMore"];
for (const key in customfieldProjects) {
if (customfieldProjects.hasOwnProperty(key)) {
const field = customfieldProjects[key];
if (field.value != null) {
console.log(`customfieldId: ${field.customfieldId}`);
console.log(`customFieldValueId: ${field.id}`);
console.log(`value: ${field.value} \n`);
}
}
}
page++
} while (hasMore);
}
fetchProjects();