Saturday, July 18, 2015

Write CSOM/JSOM/REST call in ASPX page

Nice Blog : http://allthatjs.com/2012/04/03/using-sharepoint-csom-in-html5-apps/
 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
<!DOCTYPE html>
<%@ 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>
<head>
      <title></title>
    <!-- the following 5 js files are required to use CSOM -->
     <script type="text/javascript" src="http://code.jquery.com/jquery-1.7.1.js"></script>
    <script type="text/javascript"src="/_layouts/1033/init.js"></script>
    <script type="text/javascript" src="/_layouts/15/MicrosoftAjax.js"></script>
    <script type="text/javascript" src="/_layouts/15/sp.core.js"></script>
    <script type="text/javascript" src="/_layouts/15/SP.Runtime.js"></script>
    <script type="text/javascript" src="/_layouts/15/SP.js"></script>
 
    <!-- include your app code -->
       <script type="text/javascript">

           var context = SP.ClientContext.get_current();
           var user = context.get_web().get_currentUser();

           // This code runs when the DOM is ready and creates a context object which is needed to use the SharePoint object model
           $(document).ready(function () {
               getUserName();
           });

           // This function prepares, loads, and then executes a SharePoint query to get the current users information
           function getUserName() {
               context.load(user);
               context.executeQueryAsync(onGetUserNameSuccess, onGetUserNameFail);
           }

           // This function is executed if the above call is successful
           // It replaces the contents of the 'message' element with the user name
           function onGetUserNameSuccess() {
               $('#message').text('Hello ' + user.get_title());
           }

           // This function is executed if the above call fails
           function onGetUserNameFail(sender, args) {
               alert('Failed to get user name. Error:' + args.get_message());
           }

    </script>

-->
  
</head>
<body>
    <SharePoint:FormDigest ID="FormDigest1" runat="server"></SharePoint:FormDigest>

     <div>
        <p id="message">
            <!-- The following content will be replaced with the user name when you run the app - see App.js -->
            initializing...
        </p>
    </div>
</body>
</html>

No comments:

Post a Comment

HTML

Script:

JS