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