I am developing a Farm Solution (full trust) with some custom WebParts. For one WebPart i want some ajax behaviour, so i thought: Why not use the JSOM API? My web part just want to retrieve the new Announcements, so i added a new WebPart (Farm Solution), some HTML and the following javascript
<script src="//ajax.aspnetcdn.com/ajax/4.0/1/MicrosoftAjax.js" type="text/javascript"></script>
<script type="text/javascript">
function retrieveListItems() {
SP.SOD.executeFunc('sp.js', 'SP.ClientContext', function () {
var ctx = SP.ClientContext.get_current();
var web = ctx.get_web();
var list = web.get_lists().getByTitle("Announcements");
ctx.load(list);
ctx.executeQueryAsync(
function () {
console.log(list.get_title());
},
function (sender, args) {
console.log(args.get_message());
}
);
});
}
in my html i have a link to call the js function:
<a onclick="retrieveListItems(); return false;">Call</a>
When i call the function i receive an 403 FORBIDDEN error, the google chrome console shows me the following
/_vti_bin/client.svc/ProcessQuery 403 (FORBIDDEN)
executeRequest @ MicrosoftAjax.js:5
executeRequest @ MicrosoftAjax.js:5
invoke @ MicrosoftAjax.js:5
$31_0 @ sp.runtime.js:2
executeClientRequestAsync @ sp.runtime.js:2
executeQueryAsync @ sp.runtime.js:2
retrieveListItems @ default.aspx:1091
onclick @ default.aspx:1118
And in the NETWORK tab of the Google Chrome console, in the PREVIEW (response) i see the following
[{SchemaVersion: "15.0.0.0", LibraryVersion: "15.0.4420.1017", ErrorInfo: {,…},…}]
0:{SchemaVersion: "15.0.0.0", LibraryVersion: "15.0.4420.1017", ErrorInfo: {,…},…}
ErrorInfo:{,…}
ErrorCode:-2130575251
ErrorMessage:"A validação de segurança para esta página não é válida e pode estar corrompida. Use o botão Voltar do navegador da Web para tentar realizar a operação novamente."
TRANSLATING: The security validation for this page is invalid and might be corrupted. Please use your web browser's Back button to try your operation again.
ErrorTypeName:"Microsoft.SharePoint.SPException"
ErrorValue:null
TraceCorrelationId:"78edb09d-fb1d-7012-0000-026d47d0154a"
LibraryVersion:"15.0.4420.1017"
SchemaVersion:"15.0.0.0"
TraceCorrelationId:"78edb09d-fb1d-7012-0000-026d47d0154a"
I do some research i found something related with FormDigest, so i searched my HTML markup for the __REQUESTDIGEST and copied its value, called the function and realized that the digest sent is different. It could be the source of the problem?
I do more research and found this js function that cheats the RequestDigest:
function CustomUpdateFormDigest() {
if (window._spPageContextInfo != null) {
var $v_2 = window._spPageContextInfo;
var $v_3 = $v_2.webServerRelativeUrl;
var $v_4 = window._spFormDigestRefreshInterval;
UpdateFormDigest($v_3, $v_4);
}
}
Called it before call my retrieveListItems() function but it not worked. Anyone can point me to the right direction? it is really a digest problem?
EDIT: I realised that if my page has my web part i get the error when i click in the link AND if i execute the script in the google chrome console. If i remove my webpart from page, i can execute the script in Google Chrome Console succesfully.