Quantcast
Channel: Developing Apps for SharePoint 2013 forum
Viewing all articles
Browse latest Browse all 8089

Best way to get your tokens for multiple follow-up calls?

$
0
0

Hi All,

I'm currently using AUTOHOSTED apps.

I've got an issue when making further calls back into SharePoint AFTER the initial page_load since any PostBacks from my ASP.NET form (or additional calls back in) generate the following error :

The parameter 'token' cannot be a null or empty string

This is coming back from the TokenHelper.cs class.  I've tried googling and the error seems to be common for some people when the app first tries to instantiate from the appredirect.aspx page.

I've got a "simple" web form with some dropdown lists being populated on page-load from items in my sharepoint lists in one of my site collections (note - thedata is NOT in the HostWebfrom which the app is being launched).

Everything works fine here; it's all done via CSOM and works nicely.

I did note, however, that when the page is submitted, I received the error I indicated above.  To prevent this, I wrapped my page load code within an

if (!isPostBack)
{
}

block ... and I thought all was well......

I'm now trying to make an additional call back to SharePoint inside an Ajax UpdatePanel on my form - eseentially, I'm wanting to check to see if an entry being made in one of my fields already exists BEFORE I ultimately submit my form (think - signing up for an online service and picking a username .... the username is checked to see if it is available).

What I'm doing, is wrapping my clientContext in a function ... and all is ok for calls in the page load .....but is failing on the next call.

Here's what's in my username lostfocus handler :-

ClientContext clientContext = ClientOnlyContext(siteurl);
                using (clientContext)
            {
                Web web = clientContext.Web;
                List list = web.Lists.GetByTitle("_Registrations");
 // DO SOME STUFF WITH THE LISTS
}

It is this call to my "ClientOnlyContext()" function that generates errors on subsequent calls :-

Here's the function : (note - siteurl is known / stored and accessible and resolves to the url where my lists are contained).

protected ClientContext ClientOnlyContext(String siteurl)
        {
            string contextTokenString = TokenHelper.GetContextTokenFromRequest(Request);
            //Get context token.
            SharePointContextToken contextToken = TokenHelper.ReadAndValidateContextToken(contextTokenString, Request.Url.Authority);
            Uri sharepointUrl = new Uri(siteurl);
            //Get access token.
            string accessToken = TokenHelper.GetAccessToken(contextToken, sharepointUrl.Authority).AccessToken;
            ClientContext clientContext = TokenHelper.GetClientContextWithAccessToken(sharepointUrl.ToString(), accessToken);
            return clientContext;
        }

Is there a better way to do this sort of thing??  Once I've cracked this, I'm going to need to WRITE my record into the list back on the site so the issue is going to crop up again.

I've looked at this http://msdn.microsoft.com/en-us/library/office/fp179932(v=office.15).aspx where it talks about refreshtokens etc / storing in cookies / ... but I'm ideally looking for a good reusable code example that shows how to get my handle back to SharePoint for any call I want to make (and preferably within CSOM since my form submit will be handled behind my button_click() event).

Any help would be hugely appreciated.



Viewing all articles
Browse latest Browse all 8089

Latest Images

Trending Articles



Latest Images

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