How to Get SharePoint Client Context in SharePoint Apps (Provider Hosted / SharePoint Access ) in CSOM (Client Side Object Model)
http://www.codeproject.com/Articles/581060/HowplustoplusGetplusSharePointplusClientplusContex
Introduction
The first step of app development is to correctly get access to SharePoint client context. I have struggled to develop a simple model to initialize the SharePoint Client context. Most of the App development includes ASP Master pages. So I need to figure out a working model for app development.
First, you need to know how SharePoint offers the contextString
. contextString
offers when SharePoint gets redirected from appredirect.aspx URL.
At that point, we need to capture the contextString
and generate either AccessToken or RefreshToken and save it for accessing the SharePoint Client Context at a later time. Otherwise, it will result in generating the error The parameter 'token' cannot be a null or empty string (This is a nasty error which drove me crazy.).
Normally AccessToken
is valid for 12 hours and RefreshToken
is valid for 6 months.
In my approach, I used another key which is called as CacheKey
to identify the user uniquely. Therefore I use this value to maintain a cookie based on the user.
Reference: Tips and FAQs: OAuth and remote apps for SharePoint 2013
The following shows my approach to do this. If you have a master page, you need to put the code in the onInit()
rather than page load.
Collapse | Copy Code
protected void Page_Load(object sender, EventArgs e)
{
var key = Session["CashKey"]; // sharepoint url (app hosted url)
var hostWeb = Page.Request["SPHostUrl"]; // Sharepoint url with app Title (app deployed sp url)
Uri SharePointUri = new Uri(hostWeb + "/SharePointApp1/"); // This is first time the app is running
if (key == null)
{
// get the TokenString
var contextTokenString = TokenHelper.GetContextTokenFromRequest(Page.Request);
// Get the contexttoken by passing the token string
var contextToken = TokenHelper.ReadAndValidateContextToken
(contextTokenString, Request.Url.Authority); //Since some browsers does not support cookie name more than 40 chars
// Im taking first 40 chars
var cookieName = contextToken.CacheKey.Substring(0, 40); //Add User specific cookie name to the Session
Session.Add("CashKey", cookieName); // Get the Refresh Token
var refreshToken = contextToken.RefreshToken;
// Add the cookie value (refresh Token)
Response.Cookies.Add(new HttpCookie(cookieName, refreshToken)); }
else
{
// USER already in the application, means it is not getting redirect from the appRedirect
// So contextstring is null
}
}
This is code for button click.
Collapse | Copy Code
protected void Button1_Click(object sender, EventArgs e)
{
// sharepoint url (app hosted url)
var hostWeb = Page.Request["SPHostUrl"]; // Sharepoint url with app name (app deployed sp url)
Uri SharePointUri = new Uri(hostWeb + "/SharePointApp1/"); // Get the cookie name from the session
var key = Session["CashKey"] as string;
// Get the refresh token from the cookie
var refreshToken = Request.Cookies[key].Value; //Get the access Token By passing refreshToken
// 00000003-0000-0ff1-ce00-000000000000 is principal name for SP2013 it is unique
var accessToken = TokenHelper.GetAccessToken(refreshToken,
"00000003-0000-0ff1-ce00-000000000000",
SharePointUri.Authority, TokenHelper.GetRealmFromTargetUrl(SharePointUri)); // Access the Sharepoint Do your work
using (var clientContext = TokenHelper.GetClientContextWithAccessToken
("https://rajee.sharepoint.com/SharepointApp1", accessToken.AccessToken))
{
clientContext.Load(clientContext.Web, web => web.Title);
clientContext.ExecuteQuery();
Response.Write(clientContext.Web.Title);
}
}
Note: In the middle of the app, if the context is broken due to expiration or some other case you need to initialize the app from the AppRedirect, therefore you can:
Collapse | Copy Code
var hostWeb = Page.Request["SPHostUrl"];
var val = TokenHelper.GetAppContextTokenRequestUrl(hostWeb, Server.UrlEncode(Request.Url.ToString()));
How to Get SharePoint Client Context in SharePoint Apps (Provider Hosted / SharePoint Access ) in CSOM (Client Side Object Model)的更多相关文章
- 在C#开发中如何使用Client Object Model客户端代码获得SharePoint 网站、列表的权限情况
自从人类学会了使用火,烤制的方式替代了人类的消化系统部分功能,从此人类的消化系统更加简单,加速了人脑的进化:自从SharePoint 2010开始有了Client Side Object Model ...
- SharePoint Client Object Model API 介绍以及工作原理解析
CSOM和ServerAPI 的对比 SharePoint从2010开始引入了Client Object Model的API(后文中用CSOM来代替),从名字来看,我们可以简单的看出,该API是面向客 ...
- SharePoint 2010 匿名用户调用Client Object Model访问列表项
最近有个小需求,在门户首页上加个通知公告的版块,新闻来源是列表项,需要有垂直滚动的效果. 第一个想法就是通过SharePoint的Client Object Model获取列表数据再加上JQuery来 ...
- c# sharepoint client object model 客户端如何创建中英文站点
c# sharepoint client object model 客户端如何创建中英文站点 ClientContext ClientValidate = tools.GetContext(Onlin ...
- 关于SharePoint 的Client object model该何时load和execut query的一点自己的看法
很多人在用client object model的时候,不知道何时或者该不该load,今天看到一个观点描述这个问题,觉得很有道理,和大家分享.那就是写client object model就像写sql ...
- SharePoint 2013中Office Web Apps的一次排错
转自http://www.cnblogs.com/awpatp/archive/2013/06/06/3121420.html, 仅供自己查看 笔者尝试在自己的测试环境中为SharePoint 201 ...
- SharePoint安全 - 在Goolge和Bing中查找SharePoint相关内容
博客地址 http://blog.csdn.net/foxdave 本篇提供两个查询串字典,分别对应Google和Bing的搜索,用来查询SharePoint网站的相关内容 Google ShareP ...
- SharePoint 2010 最佳实践学习总结------第1章 SharePoint Foundation开发基础
----前言 这段时间项目出在验收阶段,不是很忙,就潜心把SharePoint学一下,不求有多深刻,初衷只是先入门再说.后续会发布一系列的学习总结.主要学习的书籍为<SharePoint2010 ...
- 【Office Web Apps】在 SharePoint 中使用 Office Web Apps
在 SharePoint 中使用 Office Web Apps 在安装并配置了 Microsoft Office Web Apps 的 SharePoint 网站上,通过 Office Web Ap ...
随机推荐
- FLEX自定义事件
有时候我们需要让两个组件之间实现联动,并且在其中传递数据,自定义事件机制可以帮助我们比较优雅的实现这种需要. 下面的例子,是打算实现一个列表和一个编辑框的联动. 编辑框代码 <?xml vers ...
- vmware workstation11+centos7+lnmp一键安装包 环境搭建
vmware workstation11 1.下载:http://pan.baidu.com/s/1gecipOJ 2.安装:直接下一步. centos7 1.下载:网易镜像 http://mirro ...
- 轻量型ORM框架Dapper的使用
在真实的项目开发中,可能有些人比较喜欢写SQL语句,但是对于EF这种ORM框架比较排斥,那么轻量型的Dapper就是一个不错的选择,即让你写sql语句了,有进行了关系对象映射.其实对于EF吧,我说下我 ...
- 【转载】shell中 dd 命令
转载自:http://blog.chinaunix.net/uid-24958038-id-3416169.html dd if=/dev/zero of=的含义是什么? 一.dd命令的解释 dd:用 ...
- 受限玻尔兹曼机(RBM)学习笔记(五)梯度计算公式
去年 6 月份写的博文<Yusuke Sugomori 的 C 语言 Deep Learning 程序解读>是囫囵吞枣地读完一个关于 DBN 算法的开源代码后的笔记,当时对其中涉及的算 ...
- Network - Tcpdump
Tcpdump homepage - tcpdump wiki - tcpdump 常用格式 tcpdump -i eth<网卡号> port <端口号> -s0 -w 示例: ...
- http与websocket(基于SignalR)两种协议下的跨域基于ASP.NET MVC--竹子整理
这段时间,项目涉及到移动端,这就不可避免的涉及到了跨域的问题.这是本人第一次接触跨域,有些地方的配置是有点麻烦,导致一开始的不顺. 至于websocket具体是什么意义,用途如何:请百度. 简单说就是 ...
- 12 19 spring3 项目总结
项目截图: 林集团 https://home.cnblogs.com/u/linjituan/ 团队guihub: https:///github.com/MoiGi 李鹏飞 https://www. ...
- Linux - root初始密码设置
Ubuntu刚安装后,不能在terminal中运行su命令,因为root没有默认密码,需要手动设定. 以安装ubuntu时输入的用户名登陆,该用户在admin组中,有权限给root设定密码. 给roo ...
- 给文本框添加模糊搜索功能(“我记录”MVC框架下实现)
步骤: 1.在文本框中输入内容时,触发keyup事件: 2.在keyup事件的处理方法中,通过Ajax调用控制器的方法: 3.在控制器方法中,搜索满足条件的数据,这里分页获取数据,且只取第一页的数据, ...