0

I previously built an AngularJS application in SharePoint 2013 and that is great, now I am building a similar application in SharePoint 2010. I have two questions specific to SharePoint 2010:

  1. How do I refresh the Request Digest from JavaScript?
  2. How do I perform CRUD operations on a document library from JavaScript using the SP2010 REST Interface?
0

You can leverage REST interface available in URL

http://siteurl/_vti_bin/listdata.svc

Create

$.ajax({
    url: "http://siteurl/_vti_bin/listdata.svc/listName",
    type: "POST",
    processData: false,
    contentType: "application/json;odata=verbose",
    data: JSON.stringify(data),
    headers: {
        "Accept": "application/json;odata=verbose"
    },
    success: function (data) {
        alert("Created");
    },
    error: function (data) {
        alert("Failed";
    }
});

Update

$.ajax({
     type: 'POST',
     url: "http://siteurl/_vti_bin/listdata.svc/listName(1)", //1 is item id to update
     contentType: 'application/json',
     processData: false,
     headers: {
        "Accept": "application/json;odata=verbose",
        "X-HTTP-Method": "MERGE"
     },
     data: JSON.stringify(data),
     success: function (data) {
        alert("Created");
     },
     error: function (data) {
        failure(data);
     }
});

Read

$.ajax({
    url: "http://siteurl/_vti_bin/listdata/listName(1)",
    method: "GET",
    headers: { "Accept": "application/json; odata=verbose" },
    success: function (data) {
        alert(data.d);
    },
    error: function (data) {
        alert('Failed');
    }
});
  • This is a good start but I also need to know how to upload a file to said document library from javascript. As a bonus copying a file would be useful too. – gamelover42 Jun 4 '15 at 18:11
0

Using my angular module SennitShareAngularJs.

For get request digest you need only writte this code:

sennitRestApi.requestDigest

For mor details and download see the link below

SennitShareAngularJs github

  • That library appears to be for SharePoint 2013 – gamelover42 Oct 8 '15 at 22:58
0

What I ended up doing was to make an asp.net page named index.aspx. Inside that page I added the FormDigest server control:

<%@ Page Language="C#" %>
<%@ Register TagPrefix="SharePoint"
  Namespace="Microsoft.SharePoint.WebControls"
  Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Import Namespace="Microsoft.SharePoint" %>
<!DOCTYPE html>
<html lang="en" ng-app="app" ng-strict-di>
<head>
  <script src="/_layouts/1033/init.js"></script>
  <script src="/_layouts/MicrosoftAjax.js"></script>
  <script src="/_layouts/sp.core.js"></script>
  <script src="/_layouts/sp.runtime.js"></script>
  <script src="/_layouts/sp.js"></script>
</head>
 <body>
<SharePoint:FormDigest ID="FormDigest1" runat="server"></SharePoint:FormDigest>
...
</body>
</html>

I then created an angular service that calls $interval to periodically post a soap message to '/_vti_bin/sites.asmx':

$interval(refreshFormDigest, 1440000);
function refreshFormDigest() {
  var config = {
    headers: {
      'Accept': 'text/xml',
      'Content-Type': 'text/xml; charset=utf-8',
      'SOAPAction': 'http://schemas.microsoft.com/sharepoint/soap/GetUpdatedFormDigest'
    }
  };
  var soapMsg = '<?xml version="1.0" encoding="utf-8"?>' +
    '<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ' +
    'xmlns:xsd="http://www.w3.org/2001/XMLSchema" ' +
    'xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">' +
    '<soap:Body>' +
    '<GetUpdatedFormDigest xmlns="http://schemas.microsoft.com/sharepoint/soap/" />' +
    '</soap:Body>' +
    '</soap:Envelope>';  
  $http.post(appWebUrl + '/_vti_bin/sites.asmx', soapMsg, config)
  .then(function (response) {
    //TODO: parse the response xml here and update the hidden input value named __REQUESTDIGEST
  });

}

The response comes back in XML which I parse using a regular expression to get the request digest value. The whole thing works very well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.