How to access list data through rest api from different SharePoint farms

There is a scenario where we have Project Server hosted on different farm but we need to fetch projects data through rest API from a SharePoint site collection hosted in another farm. We have tried this from console application and it was working fine. But when we tried through Sandbox Solution it is throwing security exception, we have also tried to access through jQuery ajax but not able to fetch as we are getting the below error

Failed to load resource: the server responded with a status of 401 (Unauthorized)

XMLHttpRequest cannot load https://<sitename>/PWA/_api/ProjectData/Projects/. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'https://<siteurl>' is therefore not allowed access. The response had HTTP status code 401.</siteurl></sitename>

<sitename><siteurl></siteurl></sitename>

<sitename><siteurl></siteurl></sitename>

code for console application was

Uri uri = new Uri("https://xx.mysite.com/_api/web/lists/GetByTitle('TeamList')/items"); NetworkCredential cred = new NetworkCredential("xxx", "xxx", "xx"); HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri); request.ContentType = "application/json;odata=verbose"; request.Headers["X-RequestDigest"] = GetFormDigest(cred); request.Accept = "application/json;odata=verbose"; // request.ContentLength = itemXML.Length; request.Credentials = cred; request.Accept = "application/json;odata=verbose"; request.Method = "POST"; string stringData = "{ '__metadata': { 'type': 'SP.ListItem' }, 'Title': 'Hello World' }"; request.ContentLength = stringData.Length; StreamWriter writer = new StreamWriter(request.GetRequestStream()); writer.Write(stringData); writer.Flush(); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); StreamReader reader = new StreamReader(response.GetResponseStream());

It's very urgent so any help would be appreciated.
July 23rd, 2015 8:08am

Let me explain:

  • Sandbox doesn't work because of its limitations. Sandbox doesn't allow you to call external services. And for your information, sandbox solution is deprecated so you should avoid sandbox solution if it's a new development.
  • JS doesn't work because of cross-domain issue known as Cross Origin Resource Scripting(CORS). You can't send request to yourbanksite.com from facebook.com since they are two different domain. if you could then a malicious facebook app could query your bank account while you are logged in to your bank account in another tab in the browser!!!

So you need to try Full-trust/Farm solution (like webpart, layout page) or Provider hosted app.

Free Windows Admin Tool Kit Click here and download it now
July 23rd, 2015 11:42pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics