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

https://code.msdn.microsoft.com/SharePoint-2013-Use-the-6b3e4c1e

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

http://devmachine:1010/sites/AppCatalog/_layouts/15/appredirect.aspx?client_id=i%3A0i%2Et%7Cms%2Esp%2Eint%7C51a99a6e%2Df3eb%2D4708%2Db586%2D8cfb32360f6e%4039134774%2D6e9d%2D45b2%2D9b2c%2Dae59c3421caa&redirect_uri=%7EappWebUrl%2FPages%2FReadTitle%2Easpx%3F%7BStandardTokens%7D

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 127.0.0.1 is localhost IP.

127.0.0.1    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.