{"id":1921,"date":"2020-04-23T13:38:52","date_gmt":"2020-04-23T12:38:52","guid":{"rendered":"http:\/\/www.stuartroberts.net\/?p=1921"},"modified":"2020-04-23T13:41:01","modified_gmt":"2020-04-23T12:41:01","slug":"client-context-in-addin","status":"publish","type":"post","link":"http:\/\/www.stuartroberts.net\/index.php\/2020\/04\/23\/client-context-in-addin\/","title":{"rendered":"Client Context in AddIn"},"content":{"rendered":"\n<p>How to get the client context for code in a provider-hosted add-in that is called outside of the SharePoint context.<\/p>\n\n\n\n<p>For example, you want a page in your solution to be called directly and not from SharePoint. Reasons for this may be that you have a page in the add-in that you want to share with users and don&#8217;t want to give the full add-in url. Instead of:<\/p>\n\n\n\n<p>http:\/\/myaddindomain.com\/pages\/custompage.aspx?SPHostUrl={etc}<\/p>\n\n\n\n<p>You want something like this, without all the parameters:<\/p>\n\n\n\n<p>http:\/\/myaddindomain.com\/pages\/custompage.aspx<\/p>\n\n\n\n<!--more-->\n\n\n\n<p>With the former, you&#8217;ll be able to get the context with:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\nvar context = SharePointContextProvider.Current.GetSharePointContext(Context);\n<\/pre><\/div>\n\n\n<p>Trying this without all the parameters will obviously fail and you&#8217;ll get a 401 unauthorised error.<\/p>\n\n\n\n<p>To get a valid context this way, you&#8217;ll need to have the SharePoint host&#8217;s URL stored somewhere in the remote website. In the web.config for example.<\/p>\n\n\n\n<p>To obtain the context, use this instead:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\nvar hostUri = new Uri(ConfigurationManager.AppSettings&#91;&quot;HostUrl&quot;]);\nusing (var context = TokenHelper.GetS2SClientContextWithWindowsIdentity(hostUri, Request.LogonUserIdentity){\n\/\/ CSOM code\n}\n<\/pre><\/div>\n\n\n<p>This will create the context for the current user and you should now be able to query the SharePoint site&#8217;s content.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>How to get the client context for code in a provider-hosted add-in that is called outside of the SharePoint context. For example, you want a page in your solution to be called directly and not from SharePoint. Reasons for this &hellip; <a href=\"http:\/\/www.stuartroberts.net\/index.php\/2020\/04\/23\/client-context-in-addin\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false,"jetpack_post_was_ever_published":false,"jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":[]},"categories":[3,44],"tags":[45],"jetpack_publicize_connections":[],"aioseo_notices":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/plx2I-uZ","_links":{"self":[{"href":"http:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/posts\/1921"}],"collection":[{"href":"http:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/comments?post=1921"}],"version-history":[{"count":5,"href":"http:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/posts\/1921\/revisions"}],"predecessor-version":[{"id":1926,"href":"http:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/posts\/1921\/revisions\/1926"}],"wp:attachment":[{"href":"http:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/media?parent=1921"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/categories?post=1921"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/tags?post=1921"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}