locked
Cross site collection or cross web application call in CSOM. RRS feed

  • Question

  • Hi All,

    In client object model it was not possible to make a cross site collection or cross web application call. Has this limitation been solved in client side object model or is it still there?


    Amit - Our life is short, so help others to grow.....

    Whenever you see a reply and if you think is helpful, click ♥Vote As Helpful♥ And whenever you see a reply being an answer to the question of the thread, click ♥Mark As Answer♥


    Thursday, June 27, 2013 3:34 PM

Answers

  • Hi Amit,

    It *is* possible to do cross site collection calls using CSOM.  I apologize, I was mistaken about the cross domain library and I gave you an incorrect link above.  You don't actually have to use the cross domain library because you are trying to retrieve data outside of the current site collection context.

    The key for your requirements would be to load a context for both of your site collections.  This could be done utilizing a SharePoint Hosted app (CSOM).  You would need to use the SP.AppContextSite() method to map your site collection context to the target url of your site collection.  Here is some sample code:

    //JSOM Syntax: 
    var ctx = new SP.ClientContext(appWebUrl);
    var appContextSite = new SP.AppContextSite(ctx, targetUrl);
    ctx.Load(appContextSite.get_web());
    
    // Example usage:
    var ctx = new SP.ClientContext(
        "http://contoso-5334ef4b86c8ea.sharepointonline.com/app1"
        ); //Get this from tokens instead of hardcoding it.
    var appContextSite = 
        new SP.AppContextSite(
            ctx, 
            "http://contoso.sharepointonline.com/anothersite"
        ); //Get this from user input or context tokens.
    ctx.Load(appContextSite.get_web());
    
    
    //REST Syntax: 
    appWebUrl/_api/SP.AppContextSite(@t)/web?@t='targetUrl'
    
    // Full URL Example:
    http://contoso-5334ef4b86c8ea.sharepointonline.com/app1/_api/
    SP.AppContextSite(@t)/web?@t='http://contoso.sharepointonline.com/anothersite'

    I pulled that code from this blog post: http://blogs.msdn.com/b/officeapps/archive/2012/11/29/solving-cross-domain-problems-in-apps-for-sharepoint.aspx.  Scroll down to the "Cross-site collection calls" section for a detailed explanation.

    Hope that helps,

    ~ Katie

    • Marked as answer by Qiao Wei Friday, July 5, 2013 2:26 AM
    Friday, June 28, 2013 3:37 PM

All replies

  • Hi Amit,

    Yes, in SharePoint 2013 this is possible.  You can use the JSON cross-domain library to retrieve data.  Please visit this page to find more detailed explanations, scenarios and code samples: How to: Access SharePoint 2013 data from remote apps using the cross-domain library.

    Hope that helps,

    ~ Katie

    Thursday, June 27, 2013 4:44 PM
  • It seems to me that CSOM doesn't support cross site collection operations yet. In sharePoint 2010 i used (list.asmx SharePoint Service + Ajax) to retrive the data from other site collection. it seems to me that JSON cross-domain library is just that only.

    Amit - Our life is short, so help others to grow.....

    Whenever you see a reply and if you think is helpful, click ♥Vote As Helpful♥ And whenever you see a reply being an answer to the question of the thread, click ♥Mark As Answer♥

    Friday, June 28, 2013 2:54 AM
  • Hi Amit,

    It *is* possible to do cross site collection calls using CSOM.  I apologize, I was mistaken about the cross domain library and I gave you an incorrect link above.  You don't actually have to use the cross domain library because you are trying to retrieve data outside of the current site collection context.

    The key for your requirements would be to load a context for both of your site collections.  This could be done utilizing a SharePoint Hosted app (CSOM).  You would need to use the SP.AppContextSite() method to map your site collection context to the target url of your site collection.  Here is some sample code:

    //JSOM Syntax: 
    var ctx = new SP.ClientContext(appWebUrl);
    var appContextSite = new SP.AppContextSite(ctx, targetUrl);
    ctx.Load(appContextSite.get_web());
    
    // Example usage:
    var ctx = new SP.ClientContext(
        "http://contoso-5334ef4b86c8ea.sharepointonline.com/app1"
        ); //Get this from tokens instead of hardcoding it.
    var appContextSite = 
        new SP.AppContextSite(
            ctx, 
            "http://contoso.sharepointonline.com/anothersite"
        ); //Get this from user input or context tokens.
    ctx.Load(appContextSite.get_web());
    
    
    //REST Syntax: 
    appWebUrl/_api/SP.AppContextSite(@t)/web?@t='targetUrl'
    
    // Full URL Example:
    http://contoso-5334ef4b86c8ea.sharepointonline.com/app1/_api/
    SP.AppContextSite(@t)/web?@t='http://contoso.sharepointonline.com/anothersite'

    I pulled that code from this blog post: http://blogs.msdn.com/b/officeapps/archive/2012/11/29/solving-cross-domain-problems-in-apps-for-sharepoint.aspx.  Scroll down to the "Cross-site collection calls" section for a detailed explanation.

    Hope that helps,

    ~ Katie

    • Marked as answer by Qiao Wei Friday, July 5, 2013 2:26 AM
    Friday, June 28, 2013 3:37 PM
  • jquery + REST isn't working?
    Saturday, June 29, 2013 10:23 PM
  • From testing on SharePoint 2013 Online you CAN'T access one site collection from another using WebProxy/REST/CSOM. Unless you are accessing a list in the Development Centre Site from the App Content Url. Please correct me if you think I'm wrong.
    Monday, September 23, 2013 2:41 PM
  • For a while I had the same impression until recently I was able to resolve the cross site collection issue.  Please see my response on this post.

    http://social.msdn.microsoft.com/Forums/office/en-US/434dd593-3790-47ff-a7a6-c8825048526d/cross-domain-and-cross-site-collection-in-sharepoint-hosted-app?forum=appsforsharepoint


    kashif

    Wednesday, October 1, 2014 4:55 PM