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

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();
}

6. 保存并发布. 
7. APP页面显示如下:

对于基本的安全检查,这些信息足够了。但为了实现个性化, 我们还要用到 user profile. 
8. 停止debug. 
9. 打开Default.aspx ,加上 user profile:

<br />
<h2>User Profile Properties</h2>
<table data-bind="with: currentProps">
<tr>
<td>AccountName</td>
<td data-bind="text: AccountName"></td>
</tr>
<tr>
<td>UserName</td>
<td data-bind="text: UserName"></td>
</tr>
<tr>
<td>FirstName</td>
<td data-bind="text: FirstName"></td>
</tr>
<tr>
<td>LastName</td>
<td data-bind="text: LastName"></td>
</tr>
<tr>
<td>PreferredName</td>
<td data-bind="text: PreferredName"></td>
</tr>
<tr>
<td>WorkEmail</td>
<td data-bind="text: WorkEmail"></td>
</tr>
<tr>
<td>WorkPhone</td>
<td data-bind="text: WorkPhone"></td>
</tr>
<tr>
<td>PictureURL</td>
<td>
<img src="#" data-bind="attr: { src: PictureURL }" /></td>
</tr>
</table>

10. 打开  App.js ,在这行 var self=this; declaration:后面加上:

self._props = null;
self.userProps = ko.observable();

11. 在这行executeQueryAsync(): 前加上:

var pm = new SP.UserProfiles.PeopleManager(context);
self._props = pm.getMyProperties();
context.load(self._props);

12. 加上这行到 self.onSuccess() function:

self.userProps(self._props.get_userProfileProperties());

13. 打开AppManifest.xml file. 
14. 选择Permissions tab. 
15. scope 选择 User Profiles , permission 选择 Read.

16. 发布. 
17. 你将看到一个要你授权的页面,点 Trust It. 这个页面应该显示如下:

在user profile service有很多属性.你还可以创建自定义的属性, self._props.get_userProfileProperties() 创建了一个对象,包含了所有赋予它的 profile 属性 
, 很容易在debug时查看或者bind它的值到html 上。

来自:

http://www.cnblogs.com/fengwenit/p/3548529.html

SharePoint 2013 APP 开发示例 (二)获取用户信息的更多相关文章

  1. SharePoint 2013 APP 开发示例 系列

    SharePoint 2013 APP 安全: SharePoint 2013 APP 开发示例 (一)List 读写 SharePoint 2013 APP 开发示例 (二)获取用户信息 Share ...

  2. SharePoint 2013 APP 开发示例 (四)JQuery访问REST

    这个示例里,我们将用JQuery AJAX去发送一个 REST请求,并查看返回结果.为了让我们更好地理解REST 接口,我们将添加一个输入框让用户可以指定REST的URL, 这将让我们尝试着用构造的U ...

  3. SharePoint 2013 APP 开发示例 (三)使用远程的web资源

    在这个示例里我们将详细介绍 TokenHelper 类, 我们将看到它是怎么简单地从远程web站点访问SharePoint的.我们还将取到它的一些值.这将帮助我们理解连接是怎么被构造的,同时也方便我们 ...

  4. SharePoint 2013 APP 开发示例 (五)跨域访问 Web Service (REST API)

          虽然 JQuery 也能通过授权header实现跨域, 但SharePoint 提供了更简单的方法,它被实现在SP.RequestExecutor里 .它能访问跨域的服务包括REST AP ...

  5. SharePoint 2013 APP 开发示例 (一)List 读写

    在这个示例里,我们将创建一个页面测试 SharePoint APP的权限.这个页面有二个按钮,一个从documents里读数据,一个往documents里写数据: 1. 打开Visual Studio ...

  6. SharePoint 2013 APP 开发示例 (六)服务端跨域访问 Web Service (REST API)

    上个示例(SharePoint 2013 APP 开发示例 (五)跨域访问 Web Service (REST API))是基于JavaScript,运行在web browser内去访问REST AP ...

  7. SharePoint 2013 App 开发—SharePoint Hosted方式,

    这篇文章会依据简单的Demo,介绍一下SharePoint Hosted 方式开发App 的步骤和说明. 这种方式的环境相比较Office 365 要麻烦一些,如果不可以连接到Internet 或者还 ...

  8. SharePoint 2013 App 开发—Auto Hosted 方式

    Auto Hosted 方式,自动使用Windows Azure来作为host,这种模式将App 发布到Office 365上的SharePoint Developer Site上.这种方式可以不用花 ...

  9. Java微信公众平台开发(十二)--微信用户信息的获取

    转自:http://www.cuiyongzhi.com/post/56.html 前面的文章有讲到微信的一系列开发文章,包括token获取.菜单创建等,在这一篇将讲述在微信公众平台开发中如何获取微信 ...

随机推荐

  1. css的三种引入方式、常用的元素选择器以及css三大特性

    第一.html文件中如何使用css html文件中使用css有3种方式:链接式.嵌入式.行内式,推荐使用程度依次递减:1.链接式:在html文件通过<link />标签引入,rel属性值必 ...

  2. linux下安装LoadRunner LoadGenerator

    root用户登录 关闭防火墙: setenforce 0 /etc/init.d/iptables stop 先安装一个rpm包,compat-libstdc++-33-3.2.3-61.i386.r ...

  3. -webkit-overflow-scrolling

    -webkit-overflow-scrolling 属性 控制元素在移动设备上是否使用滚动回弹效果. 取值 auto    使用普通滚动, 当手指从触摸屏上移开,滚动会立即停止. touch   使 ...

  4. asp.net MVC中使用Autofac小结 (遇到的最傻错误: 没有为该对象定义无参数的构造函数)

    项目使用的MVC4,.net 4.5 Nuget安装最新的autofac,一直提示不支持.net 4.5.没办法了,最后用Nuget控制台安装的老版本.因为我使用的是MVC4,所以直接安装的是auto ...

  5. ITest

    渗透测试入门 我很简单,请不要欺负我 网站综合渗透实验: 真的很简单: 你是会员吗: 2015中国网络安全大赛 一.代码执行: 同DZ漏洞全家桶中的"又见DZ,我能那你怎么办". ...

  6. js中checkbox反选

    <!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content ...

  7. java-成员方法/变量、类方法/变量等区别

    方法 成员方法 成员方法也叫实例方法.必须先有实例即对象,然后才能通过实例调用该实例方法. 类方法 和类变量一样,有关键字static修饰,可以不用实例,直接用类就可以调用类方法. 变量 成员变量 也 ...

  8. Java中ArrayList的使用

    //创建ArrayList ArrayList arr  = new ArrayList(); //ArrayList添加数据 arr.add("123"); arr.add(&q ...

  9. 苹果4S

    港版.4S.白.非翻新机.16G.联通3G移动2G电信2G 1000 美版.4S.白.翻新.16G.联通3G移动2G电信3G 980

  10. POJ 3259 Wormholes(SPFA+邻接表)

    ( ̄▽ ̄)" #include<iostream> #include<cstdio> #include<queue> #include<vector ...