title: How to run Sharepoint Rest API from server side with elevated privileges?
body: <p>The Sharepoint Rest API uses a simple URL of the type <code>http://mysite/_api/search/query?querytext='search_key'</code> to return search results as an XML. When I run this directly in a browser, I see a valid XML response:</p>
  
  <p><img src="https://i.stack.imgur.com/Uu9iP.png" alt="Rest API Output"></p>
  
  <p>(1) Am I right in assuming the above response is generated using the current user's authorization?</p>
  
  <p>(2) Can this URL be invoked from server side? I tried it in a web method (WCF web service), but received a <code>401 - Unauthorized</code>:</p>
  
  <pre><code>public string GetSearchResults(string searchKey)
  {
      string webURL = SPContext.Current.Web.Url;
      string searchURL = webURL + "/_api/search/query?querytext='" + searchKey + "'";
      WebClient client = new WebClient();
      string xmlResponse = client.DownloadString(searchURL); // throws 401
  
      // parse xmlResponse and return appropriately
  }
  </code></pre>
  
  <p>(3) What I really need is to be able to <strong>get the search results irrespective of the current user's access rights</strong> (the requirement is that users will see all search results, with an option to "request access" when needed). </p>
  
  <p>I tried this in the above web method, but it still throws the same <code>401</code>:</p>
  
  <pre><code>public string GetSearchResults(string searchKey)
  {
      string webURL = SPContext.Current.Web.Url;
      string searchURL = webURL + "/_api/search/query?querytext='" + searchKey + "'";
      string xmlResponse;
      SPSecurity.RunWithElevatedPrivileges(delegate()
      {
          WebClient client = new WebClient();
          xmlResponse = client.DownloadString(searchURL); // still 401
      });
  
      // parse xmlResponse and return appropriately
  }
  </code></pre>
  
  <p><br/> </p>
  
  <p><strong>What is the right way to invoke the Rest URL from server side? Specifically, from a web method? And how can it be run as super user?</strong></p>