通过 SharePoint 2013 App 的应用程序页面获取 FormDigest

Get FormDigest via application page for SharePoint 2013 App

启动新的 SharePoint 应用程序时创建的 default.aspx 页面不需要我的需求,因为它继承自主机 Web 母版页,而且我发现使用我自己的 [=15] 更容易获得响应式设计=] 复杂性较低的页面。使用这篇文章 JSOM in HTML5 Apps,我试图检索 FormDigest,但在检查代码时它不起作用。有任何想法吗?这是我当前的代码:

<!DOCTYPE html>
<%--Declare the Page Language and obtain the SharePoint namespace required for retrieving the FormDigest header--%> 
<%@ Page language="C#" %>
<%@ Register TagPrefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Import Namespace="Microsoft.SharePoint" %>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />

<title></title>
<link href="../Content/bootstrap.min.css" rel="stylesheet" />

<link href="../Content/App.css" rel="stylesheet" />

<script src="../Scripts/jquery-1.9.1.min.js"></script>
<script src="/_layouts/1033/init.js"></script>
<script src="/_layouts/MicrosoftAjax.js"></script>
<script src="/_layouts/sp.core.js"></script>
<script src="/_layouts/15/sp.runtime.js"></script>
<script src="/_layouts/15/sp.js"></script>
<script src="/_layouts/15/SP.RequestExecutor.js"></script>
<script src="/_layouts/15/SP.UI.Controls.js"></script>

<script src="../Scripts/angular.min.js"></script>

<script src="../Scripts/ui-bootstrap-tpls-0.12.0.min.js"></script>
<script src="../Scripts/angular-route.min.js"></script>
<script src="../Scripts/angular-resource.min.js"></script>

<script src="../Scripts/dirPagination.js"></script>

<script src="../Services/services.js"></script>
<script src="../Controllers/controllers.js"></script>
<script src="../App.js"></script>

</head>
<body>
<SharePoint:FormDigest ID="FormDigest1" runat="server"></SharePoint:FormDigest>
 <div class="container" data-ng-app="app">
      <div id="chrome_ctrl_container"></div>
    <div>
        <p id="message">

            initializing...
        </p>
    </div>
    <div data-ng-view></div>
    <hr>
    <footer></footer>
</div>
</body>
</html>

我发现我需要一个表单实例来执行调用。它运行完美:

index.aspx

<!DOCTYPE html>
<%--Declare the Page Language and obtain the SharePoint namespace required for retrieving the FormDigest header--%> 
<%@ Page language="C#" %>
<%@ Register TagPrefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Import Namespace="Microsoft.SharePoint" %>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />

<title></title>
<link href="../Content/bootstrap.min.css" rel="stylesheet" />

<link href="../Content/App.css" rel="stylesheet" />

<script src="../Scripts/jquery-1.9.1.min.js"></script>
<script src="/_layouts/1033/init.js"></script>
<script src="/_layouts/MicrosoftAjax.js"></script>
<script src="/_layouts/sp.core.js"></script>
<script src="/_layouts/15/sp.runtime.js"></script>
<script src="/_layouts/15/sp.js"></script>
<script src="/_layouts/15/SP.RequestExecutor.js"></script>
<script src="/_layouts/15/SP.UI.Controls.js"></script>

<script src="../Scripts/angular.min.js"></script>

<script src="../Scripts/ui-bootstrap-tpls-0.12.0.min.js"></script>
<script src="../Scripts/angular-route.min.js"></script>
<script src="../Scripts/angular-resource.min.js"></script>

<script src="../Scripts/dirPagination.js"></script>

<script src="../Services/services.js"></script>
<script src="../Controllers/controllers.js"></script>
<script src="../App.js"></script>

</head>
<body>
<%--    Get the form digest to use for POST/Update/Delete operations via Web services--%>
<form id="Form1" method="post" runat="server">
<SharePoint:FormDigest ID="FormDigest1" runat="server"></SharePoint:FormDigest>
    </form>
 <div class="container" data-ng-app="app">
      <div id="chrome_ctrl_container"></div>
    <div>
        <p id="message">

            initializing...
        </p>
    </div>
    <div data-ng-view></div>
    <hr>
    <footer></footer>
</div>

</body>
</html>

我们在 ngResource 调用中的例子 AngularJS:

appServices.factory('appTypes', ['$resource', function ($resource) {
    return $resource("/_api/web/lists/getbytitle('Todo Types List')/Items", {Id: "@Id"},
    {
        'query': { method: "GET", isArray: false, headers: { 'Accept': 'application/json;odata=nometadata' } },
        'update': { method: 'PATCH', headers: { 'Accept': 'application/json;odata=nometadata' } },
        'save': { method: 'POST', headers: { 'Accept': 'application/json;odata=nometadata', 'content-type': 'application/json;odata=nometadata', 'X-RequestDigest': $("#__REQUESTDIGEST").val() } }
    }
    );
}]);