2015-09-07 24 views
7

Sto cercando di creare un semplice elemento di una lista con l'API REST su Sharepoint 2013. Il mio codice:Sharepoint 2013 tramite REST API: Errore 403 Forbidden quando si cerca di creare l'elemento

$.ajax({ 
    url: siteUrl + "/_api/web/lists/getByTitle('internal_Listname')/items", 
    type: "POST", 
    contentType: "application/json;odata=verbose", 
    data: JSON.stringify({ 
     '__metadata': { 
      'type': 'SP.Data.internal_ListnameListItem', 
     }, 
     'K1F1': k1f1Result, 
    }), 
    headers: { 
     "accept": "application/json;odata=verbose", 
     "X-RequestDigest": $("#__REQUESTDIGEST").val(), 
    }, 
    success: function (data) { 
     console.log("done"); 
    }, 
    error: function (err) { 
     console.log(JSON.stringify(err)); 
    } 
}); 

Quando si cerca di inviare i dati ottengono l'errore 403 "Proibito".

"error":{ 
    "code":"-2130575251, Microsoft.SharePoint.SPException", 
    "message":{ 
     "lang":"en-US", 
     "value":"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." 
    } 
} 
  • devo privilegi di amministratore completo su questo sito e la lista.

risposta

1

trovato la soluzione di qualche giorno fa: Ho dimenticato di aggiungere la richiesta digerire modulo per il corpo. Dovrebbe avere la seguente struttura;

<form runat="server"> 
    <SharePoint:FormDigest ID="FormDigest1" runat="server"></SharePoint:FormDigest> 
</form> 
10

Molto probabilmente questo errore si verifica dal momento che il digest è stato scaduto nella pagina.

In tal caso, è possibile acquisire un nuovo valore di digest del modulo effettuando una richiesta POST all'endpoint /_api/contextinfo.

Esempio

function getFormDigest(webUrl) { 
    return $.ajax({ 
     url: webUrl + "/_api/contextinfo", 
     method: "POST", 
     headers: { "Accept": "application/json; odata=verbose" } 
    }); 
} 


function createListItem(webUrl, listName, itemProperties) { 
    return getFormDigest(webUrl).then(function (data) { 

     return $.ajax({ 
      url: webUrl + "/_api/web/lists/getbytitle('" + listName + "')/items", 
      type: "POST", 
      processData: false, 
      contentType: "application/json;odata=verbose", 
      data: JSON.stringify(itemProperties), 
      headers: { 
       "Accept": "application/json;odata=verbose", 
       "X-RequestDigest": data.d.GetContextWebInformation.FormDigestValue 
      } 
     }); 
    }); 
} 

Uso

//Create a Task item 
var taskProperties = { 
    '__metadata': { 'type': 'SP.Data.WorkflowTasksItem' }, 
    'Title': 'Order approval' 
}; 

createListItem(_spPageContextInfo.webAbsoluteUrl, 'Workflow Tasks', taskProperties) 
.done(function (data) { 
    console.log('Task has been created successfully'); 
}) 
.fail(function (error) { 
    console.log(JSON.stringify(error)); 
}); 
0

La mia soluzione per lo stesso problema:

<form id="form1" runat="server"> <!-- this make SP 2013 take it legit --> 
<div class="style1"> <!-- dont know what, but SP need it --> 
---your page usually a divs--- 
</div> 
</form>