-
Notifications
You must be signed in to change notification settings - Fork 0
/
pocSHPOnline.html
169 lines (143 loc) · 4.7 KB
/
pocSHPOnline.html
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Page Title</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" type="text/css" media="screen" href="main.css" />
<script src="main.js"></script>
<script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
</head>
<body>
<script>
/* ************************************************************
This script stores access logs in Sharepoint 2010 list.
Requirements:
- Sharepoint list named "Audiencia" with text columns: "Usuario" and "URL".
How to use:
- Add a <script/> reference to this file at Sharepoint Page or Master Page.
************************************************************ */
$(document).ready(function(){
setTimeout(function(){
RegistrarAcesso();
}, 2000);
});
function GetCurrentDateTime()
{
var today = new Date();
var date = today.getFullYear()+'-'+(today.getMonth()+1)+'-'+today.getDate();
var time = today.getHours() + ":" + today.getMinutes() + ":" + today.getSeconds();
var dateTime = date+' '+time;
return dateTime;
}
async function GetCurrentUser()
{
console.log("Iniciou busca pelo usuário atual.");
try{
var userid= _spPageContextInfo.userId;
var requestUri = _spPageContextInfo.webAbsoluteUrl + "/_api/web/getuserbyid(" + userid + ")";
var requestHeaders = { "accept" : "application/json;odata=verbose" };
$.ajax({
url : requestUri,
contentType : "application/json;odata=verbose",
async: false,
headers : requestHeaders,
success : onSuccess,
error : onError
});
function onSuccess(data, request){
var loginName = data.d.Title;
console.log("Usuário encotrado: " + loginName);
return loginName;
}
function onError(error) {
console.log("Erro ao buscar usuário: " + error);
return "Não encontrado";
}
}
catch{
console.log("Executado fora do Sharepoint.");
return "Usuário de exemplo";
}
}
async function RegistrarAcesso()
{
console.log("Iniciou a inclusão do item");
var listName = "Audiencia";
var currentUserLoginName;
var urlAtual = window.location.href;
var dtAcesso = GetCurrentDateTime();
var urlPrefix;
try{
urlPrefix = _spPageContextInfo.webAbsoluteUrl;
}
catch(){
console.log("Não foi possível encontrar o site.");
}
/// Sharepoint Online
var urlAPI = urlPrefix + "/_api/web/lists/GetByTitle('" + listName + "')/items";
console.log("Iniciou busca pelo usuário atual.");
try{
var userid = _spPageContextInfo.userId;
var requestUri = _spPageContextInfo.webAbsoluteUrl + "/_api/web/getuserbyid(" + userid + ")";
var requestHeaders = { "accept" : "application/json;odata=verbose" };
$.ajax({
url : requestUri,
contentType : "application/json;odata=verbose",
async: false,
headers : requestHeaders,
success : onSuccess,
error : onError
});
function onSuccess(data, request){
var loginName = data.d.Title;
console.log("Usuário encotrado: " + loginName);
currentUserLoginName = loginName;
}
function onError(error) {
console.log("Erro ao buscar usuário: " + error);
currentUserLoginName = "Não encontrado";
}
}
catch{
console.log("Executado fora do Sharepoint.");
currentUserLoginName = "Usuário de exemplo";
}
console.log("Valor de currentUserLoginName: " + currentUserLoginName);
CreateListItem(urlAPI, listName, "Teste", currentUserLoginName, urlAtual);
}
function CreateListItem(url, listName, title, usuario, urlAtual){
console.log("CreateListItem");
$.ajax({
async: true, // Async by default is set to “true” load the script asynchronously
// URL to post data into sharepoint list
url: url,
method: "POST", //Specifies the operation to create the list item
data: JSON.stringify({
'__metadata': {
'type': 'SP.Data.' + listName + 'ListItem' // it defines the ListEnitityTypeName
},
//Pass the parameters
'Title': 'Teste inclusão',
'Usuario': usuario,
'URL': urlAtual
}),
headers: {
"accept": "application/json;odata=verbose", //It defines the Data format
"content-type": "application/json;odata=verbose", //It defines the content type as JSON
"X-RequestDigest": $("#__REQUESTDIGEST").val() //It gets the digest value
},
success: function(data) {
console.log("Sucesso");
},
error: function(error) {
console.log(JSON.stringify(error));
}
});
}
</script>
<button id="nmSalvarVisita" onclick="RegistrarAcesso()" type="button">Registrar acesso manual</button>
<div id="ResultDiv"></div>
</body>
</html>