Quantcast
Channel: SharePoint 2013 - Development and Programming forum
Viewing all articles
Browse latest Browse all 7589

Getting Client Context in Class Library for Sharepoint 2013 provider hosted app

$
0
0

I have created an MVC Provider hosted app.

From my controllers I can easily get the client context via:

var spContext = SharePointContextProvider.Current.GetSharePointContext(HttpContext);


But in my layout page, I have a call that gets the topnavbar and replicates it:

                               
@foreach (var nav in HostHelper.GetNav(this))
 {<li class='static'><a class='static menu-item ms-core-listMenu-item ms-displayInline ms-navedit-linkNode' href='@nav.Url'><span class='addition-background ms-navedit flyoutArrow'>                                                        <span class='menu-item-text'>
@nav.Title                                                </span>                                                   </span>                                               </a>                                           </li>                                       
}

This causes an error because I am not sure how to get the clientcontext from my class. I have tried:

      
public static NavigationNodeCollection GetNav()       
 {
var spContext = SharePointContextProvider.Current.GetSharePointContext(System.Web.HttpContext.Current);

using (var clientContext = spContext.CreateUserClientContextForSPHost())
{
if (clientContext != null)
{                                    
NavigationNodeCollection topNav = clientContext.Web.Navigation.TopNavigationBar;            


clientContext.Load(topNav);                   

clientContext.ExecuteQuery();                   

return topNav;                                       }           
}                       
return null;       
}

put it doesnt get the client context


Viewing all articles
Browse latest Browse all 7589

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>