When Visual Studio 2013 came out, it introduced a new class and simplified methods for obtaining a ClientContext to use with the Client Side Object Model (CSOM) to access SharePoint 2013 sites. A new SharePointContext class was added to simplify the programming model, but internally it still called the TokenHelper class that originally shipped with Visual Studio 2012.
Shortly after SharePoint 2013 shipped, I provided an additional class – the ClaimsTokenHelper class – to be used when your SharePoint sites are secured using SAML authentication (http://blogs.technet.com/b/speschka/archive/2013/07/23/3539509.aspx). Neither the original TokenHelper class nor the new SharePointContext class provides a means to properly identify a SAML claims user. I decided to take a fresh look at the ClaimsTokenHelper implementation and see if I could find a way to update things to keep it more closely aligned with the development model used in the new SharePointContext class. What I ended up doing is creating a new SharePointContextExtensions class, and it allows you to use virtually the same exact programming model as you do now with the SharePointContext class.
Here’s an example of the code you use to access a site title using the SharePointContext class:
var spContext = SharePointContextProvider.Current.GetSharePointContext(Context);
using (var clientContext =
spContext.CreateUserClientContextForSPHost())
{
clientContext.Load(clientContext.Web, web => web.Title);
clientContext.ExecuteQuery();
Response.Write(clientContext.Web.Title);
}
Now, here’s an example of using the new SharePointContextExtensions class:
spContext.CreateUserClientContextForSPHost(TokenHelper.IdentityClaimType.SMTP))
As you can see, literally the only difference between the two now is that you need to specify which OAuth identity attribute you want to use as the identity claim – SMTP, SIP, or UPN. Also, there is a corresponding method for getting a ClientContext for an App Only token. Here’s the out of the box syntax:
spContext.CreateAppOnlyClientContextForSPHost();
And here’s the corresponding method when using SAML:
spContext.CreateAppOnlySamlClientContextForSPHost();
In this case I had to actually change the method name, because it would otherwise have the same exact method name and signature as the SharePointContext class. I’ve attached the SharePointContextExtensions and ClaimsTokenHelper classes to this posting. Using them is pretty straightforward:
That’s it – you should be up and running in no time!
Would it be possible to push this code as a library to NuGet?
Hi I am getting access denied error and its still passing the windows identity
Got it working. thanks
Hi Steve, Can I still decorate my methods with [SharePointContextFilter] attribute? Please advise. Thank you so much.
Hi Steve, I'm trying to download the attached file for this post and the original, but they are not working. Could you please help me to get them? Thanks, you content is very helpful!
http://dichvuketoanlongbien.com>dich vu ke toan