0

I have created a console application by using this tutorial:

http://blogs.msdn.com/b/kaevans/archive/2014/03/02/building-a-sharepoint-app-as-a-timer-job.aspx

With some differences of course, in my console app, I want to make a rest call to the search api.

Read this: Giving the App Principal Permissions Instead of using tenant manage as in the example I used this:

  <AppPermissionRequests>
    <AppPermissionRequest Scope="https://sharepoint/search" Right="QueryAsUserIgnoreAppPrincipal" />
  </AppPermissionRequests>

Then I went to “_layouts/AppInv.aspx”. And registered the app using the client id and the xml above.

Then I clicked on Trust it.

My app.config is like this:

<xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="Sites"
             type="System.Configuration.NameValueSectionHandler"/>
  </configSections>
  <appSettings>
    <add key="ClientId" value="xx-1c6c-45e7-8a2a-7364a705c836"/>
    <add key="ClientSecret" value="+xx="/>
  </appSettings>
  <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    </startup>
  <Sites>
    <add key="site1" value="https://xx.sharepoint.com/sites/developersitecollection"/>
  </Sites>
</configuration>

Then my console app fails with Unauthorized Exception on the getresponde

 static void Main(string[] args)
        {
            var config = (NameValueCollection)ConfigurationManager.GetSection("Sites");


            foreach (var key in config.Keys)
            {
                Uri siteUri = new Uri(config.GetValues(key as string)[0]);

                using (ClientContext clientContext = new ClientContext(siteUri))
                {
                    string realm = TokenHelper.GetRealmFromTargetUrl(siteUri);
                    string accessToken =
                        TokenHelper.GetAppOnlyAccessToken(TokenHelper.SharePointPrincipal,
                        siteUri.Authority, realm).AccessToken;

                    HttpWebRequest endpointRequest = (HttpWebRequest)HttpWebRequest.Create(siteUri +"/_api/search/query?querytext=%27*.pptx%27");
                    endpointRequest.Method = "GET";
                    endpointRequest.Accept = "application/json;odata=verbose";
                    endpointRequest.Headers.Add("Authorization", "Bearer " + accessToken);
                    HttpWebResponse endpointResponse = (HttpWebResponse)endpointRequest.GetResponse();
                    StreamReader reader = new StreamReader(endpointResponse.GetResponseStream());
                    var searchXml = new XmlDocument();
                    searchXml.LoadXml(reader.ReadToEnd()); 

                }
            }
        }
-1

I have added this into the xml AllowAppOnlyPolicy="true"

and now the code shows me a different error, already asked a question about that error here:

https://sharepoint.stackexchange.com/questions/96102/remote-server-returned-an-error-500-internal-server-error-when-using-rest-sea

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.