SharePoint 2013 APP开发示例 (二)获取用户信息 - 面向对象网,web开发,Sharepoint,学习,对象 - 面向对象技术开发

面向对象技术开发

会员投稿 投稿指南 站长资讯通告:
您的位置: 首页 > Web开发 > SharePoint > 正文

SharePoint 2013 APP开发示例 (二)获取用户信息

来源: www.bianceng.cn 阅读:

这个示例里,我们将演示如何获取用户信息:

1. 打开 Visual Studio 2012.

2. 创建一个新的  SharePoint 2013 app: UserProfileTest.

3. 选择SharePoint-hosted, 点Finish.

4. 打开Default.aspx :

加入knockoutjs和sp.userprofiles.debug.js(包含user profile的信息):

<script type="text/javascript" src="../Scripts/knockout-3.0.0.js"></script>

   <script type="text/javascript" src="/_layouts/15/sp.runtime.debug.js"></script>

   <script type="text/javascript" src="/_layouts/15/sp.debug.js"></script>

   <script type="text/javascript" src="/_layouts/15/sp.userprofiles.debug.js"></script>

修改title:

<asp:Content ContentPlaceHolderID="PlaceHolderPageTitleInTitleArea" runat="server">

   User Information

</asp:Content>

加入用户显示:

<asp:Content ContentPlaceHolderID="PlaceHolderMain" runat="server">
   
    <h2>Current User Properties</h2>
   
    <table data-bind="with: currentUser">
        <tr>
            <td>title</td>
            <td data-bind="text: get_title()"></td>
        </tr>
        <tr>
            <td>Id</td>
            <td data-bind="text: get_id()"></td>
        </tr>
        <tr>
            <td>loginName</td>
            <td data-bind="text: get_loginName()"></td>
        </tr>
        <tr>
            <td>email</td>
            <td data-bind="text: get_email()"></td>
        </tr>
        <tr>
            <td>isSiteAdmin</td>
            <td data-bind="text: get_isSiteAdmin()"></td>
        </tr>
    </table>
   
    </asp:Content>

5. 打开App.js 修改如下:

前二行引用的js提供了智能感知的功能

/// <reference path="knockout-3.0.0.debug.js"  />
/// <reference path="~/_layouts/15/sp.userprofiles.debug.js"  />
   
$(function () {
    ko.applyBindings(new userProfileProps());
});
   
function userProfileProps() {
    var self = this;
    self._currentUser = null;
    self.currentUser = ko.observable();
      
   
    self.load = function () {
        var context = SP.ClientContext.get_current();
        self._currentUser = context.get_web().get_currentUser();
        context.load(self._currentUser);
   
        var pm = new SP.UserProfiles.PeopleManager(context);
        self._props = pm.getMyProperties();
        context.load(self._props);
   
        context.executeQueryAsync(
            Function.createDelegate(self, self.onSuccess),
            Function.createDelegate(self, self.onFail)
            );
    }
   
    self.onSuccess = function () {
        self.currentUser(self._currentUser);
    }
   
    self.onFail = function (sender, args) {
        alert("Unable to access user information: " + args.get_message());
    }
   
    self.load();
}

Tags:
相关文章列表: