Wednesday, October 9, 2013

Access data from other site collection in SharePoint Hosted Apps

When we want to access data from other site collection in same web application in SharePoint-Hosted Apps then there is no option other than cross-domain library(SP.RequestExecutor.js).

Microsoft SharePoint team has provided one of the best code sample for start point. Download solution from below link

After download don't deploy it on your Developer Site, every new SharePoint App developer makes this mistake, even i. 

Steps to deploy and test

  • Just publish this solution from Visual Studio to any location so you will get an App package with extension .app. 
  • Next, Create an App Catalog site collection for testing purpose and  upload this package to "Apps for SharePoint" library on this site.
  • Go to "Site Contents" page of App Catalog site and click on "add an app", select app "CrossDomainApp". You will get installing app message, wait until app get installed.
  • Once installation done, click on the app. 
  • Now you will get page not found message like below

you get this error because your DEV machine couldn't find the DNS mappings for this url so the next step is to add DNS mapping. On most of the DEV machines DNS Manager isn't present then how we can do this? Simple, add domain name host entry in machine hosts file but which domain name to add here.

When you get page not found error your url is like below


You can't add domain name devmachine:1010 as host entry but you need to add app domain host entry which is something like app-22bee6440ed9fc.spapp. How we'll get this entry?

If you have Fiddler then start it and refresh that page not found page, now you will get message like below.

[Fiddler] DNS Lookup for "app-22bee6440ed9fc.spapp" failed. System.Net.Sockets.SocketException The requested name is valid, but no data of the requested type was found 

Copy this app domain DNS entry app-22bee6440ed9fc.spapp (in your case this would be different) and add it to hosts file like below, here is localhost IP.    app-22bee6440ed9fc.spapp
::1    app-22bee6440ed9fc.spapp

Save hosts file and refresh the app page. You will get login prompt, put your credentials and you will get app home page.

One obvious question comes in the mind, When i deploy any app directly from Visual Studio, i don't need to add any such host entry in the hosts file? The reason is, this work is done by the Visual Studio when you deploy an app. For test purpose, just deploy an app using Visual Studio and check host file.

No comments:

Post a Comment