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 ...
随机推荐
- MAC 入门
1.安装java jdk eclipse 后发现运行不了,原因是JAVA_HOME 没有设置,真操蛋 export JAVA_HOME=`/usr/libexec/java_home` 2.安装bre ...
- SpringMVC学习系列-后记 结合SpringMVC和Hibernate-validator,根据后台验证规则自动生成前台的js验证代码
在SpringMVC学习系列(6) 之 数据验证中我们已经学习了如何结合Hibernate-validator进行后台的数据合法性验证,但是通常来说后台验证只是第二道保险,为了更好的用户体验会现在前端 ...
- 比较HTML元素和Native组件的区别
我们开发web应用,会使用到各种Html基本元素,比较<div>,<span>,<img>等. 当我们在开发React Native时,我们不能使用HTML元素,但 ...
- 在腾讯开发应用中心上架apk所遇到的问题
这篇只是为了记录我走过的弯路,和判断错误的方法 首先当我用 keyStore打包apk的时候,程序没有报任何错误,当然也可以运行: 接下来就是上传该apk吧: 等上传完了,就报解析错误.如下.: aa ...
- yousa_team团队项目 兼职平台 完成展示
我们团队的团队项目是一个大学生兼职网站,商家可以在网站上发布信息,学生对相应的岗位进行预约,然后根据信誉度来表示用户的信誉,整个平台由管理员监控, 包括修改错误信息,修改用户信誉度,删除过期信息,接受 ...
- Android SQLite的ORM接口实现(一)---findAll和find的实现
最近在看Android的ORM数据库框架LitePal,就想到可以利用原生的SQLite来实现和LitePal类似的ORM接口实现. LitePal有一个接口是这样的: List<Status& ...
- stl的优先级队列
#include <iostream> #include <vector> #include <queue> using namespace std; class ...
- 【Spark】---- 在Linux集群上安装和配置Spark
1 安装JDK 1) 进入JDK官网 2) 下载JDK安装包 3)配置环境变量,在/etc/profile增加以下代码 JAVA_HOME=/home/hadoop/jdk1.6.0_38 PAT ...
- 微软必应词典客户端的案例分析——个人Week3作业
第一部分 调研,评测 Bug探索 Bug No1.高亮语义匹配错位 环境: windows8,使用必应词典版本PC版:3.5.0 重现步骤: 1. 搜索"funny face"这一 ...
- [C#高级编程]基础知识摘要一
核心C#: 值类型存储在堆栈中,而引用类型存储在托管堆上. object类型可以用于两个目的: 可以使用object引用绑定任何子类型的对象 object类型执行许多一般用途的基本方法,包括Equal ...