3

I have a very simple scenario. I am using SharePoint Online I have 2 site collections SC1 and SC2. Now i am executing a JavaScript function in SC1 and am trying to create a list in SC2 in a sub-site using CSOM. First question is, Is it possible? second question is How?

Please note that I have tried using the REST approach which works but that has a drawback that you have to make multiple ajax calls to create columns.

3

I've not provided whole code but you can call cross site connection using JavaScript. Take a reference from below code:

var crossSiteContext = new SP.ClientContext("http://url-of-another-siteCollection/");
var crossSiteWeb = crossSiteContext.get_web();
crossSiteContext.load(crossSiteWeb);
crossSiteContext.executeQueryAsync(function(){

    var listCreationInfo = new SP.ListCreationInformation();  
    listCreationInfo.set_title('ListName');  
    listCreationInfo.set_templateType(SP.ListTemplateType.announcements);  
    this.oList = crossSiteWeb.get_lists().add(listCreationInfo);  
    crossSiteContext.load(oList);   

},function(sender,args){ alert(args.get_message()); });

You can get reference from this link for JavaScript code in SharePoint.

  • Its giving me an error : SP.ClientContext is not a constructor anything i am missing? – Sree Kuttan Jun 23 '16 at 12:55
  • Thank you it worked. The error was because of something else. – Sree Kuttan Jun 23 '16 at 14:47
1

For SharePoint Online / Office 365, you can use JSOM to make cross-site collection calls as long as the site collections are all in the same tenant.


There are only two scenarios where you can make cross-site collection calls using the JavaScript Object Model in a SharePoint on-premises farm:

  1. Both site collections are located in the same web application and none of them is a host-named site collection. Basically the domain of both site collections needs to be the same.

  2. You make the calls from within a SharePoint-Hosted Add-in (aka App) and use the web proxy to access data from any site collections, regardless in which web application they are hosted. With this you can actually make calls to non-SharePoint resources (e.g. public web services). In SP-hosted Add-ins you can also use the cross-domain library to achieve the same results I described above at point 1.


Regarding how to do this, see Dikesh's answer. The important bit is new SP.ClientContext("http://url-of-another-siteCollection/"); where you specify the URL of the site located in the site collection where you're trying to create the list.

  • so in Sharepoint Online terms it should be in the same tenant or under the same account? – Sree Kuttan Jun 23 '16 at 14:46
  • A tenant in SharePoint online is basically equivalent to a web application in SharePoint on-premise farm, as far as JSOM is concerned. I will update my answer. – Mihail-Florin Popa Jun 23 '16 at 14:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.