Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

Can a sharepoint hosted app get access to data in another site collection?

I'm getting a windows login dialog when accessing data in another site collection (other than the host webs site collection). Just want to access the items of another list. I'm accessing it via Rest Api.

code:

    var url = "https://urlOftheOtherSiteCollection";
    appweburl = decodeURIComponent(getQueryStringParameter("SPAppWebUrl"));
    var scriptbase = url + "/_layouts/15";
    $.getScript(scriptbase + "SP.RequestExecutor.js", function () {

        var url = appweburl + "/_api/SP.AppContextSite(@target)" +
                "/web/lists/getbytitle('Announcements')/items?" +
                "@target='" + url + "'";

        var executor = new SP.RequestExecutor(appweburl);
        executor.executeAsync(
        {
            url: url,
            method: "GET",
            headers: { "Accept": "application/json; odata=verbose" },
            success: function (data) {}
        }
share|improve this question

1 Answer 1

You should be able to access that site via the rest interface. It should use your browser session to do the windows authentication. You just need to validate that the site you are accessing is accessible by the user running your code and they need the client api permissions.

 Private Function ISharePointCreatePerson(ByVal firstName As String, ByVal lastName As String) As String Implements IService1.ISharePointCreatePerson
    Try
        Dim srv As New MiddleMan.SPTraining.TrainingDataContext(New Uri("http://1dev4000/sites/SPTraining/_vti_bin/ListData.svc"))
        srv.Credentials = New System.Net.NetworkCredential("username", "password", "domain")
        Dim newP As New MiddleMan.SPTraining.PersonsItem With {
            .FirstName = firstName, _
            .LastName = lastName, .Title = "Sent From QAD"}
        srv.AddToPersons(newP)
        Dim resp = srv.SaveChanges()
        Return resp.BatchStatusCode.ToString
    Catch ex As Exception
        Return ex.Message.ToString
    End Try

End Function

Private Function ISharePointGetPersons() As Person() Implements IService1.ISharePointGetPersons
    Dim srv As New MiddleMan.SPTraining.TrainingDataContext(New Uri("http://1dev4000/sites/SPTraining/_vti_bin/ListData.svc"))
    srv.Credentials = New System.Net.NetworkCredential("username", "password", "domain")
    Dim personCollection As System.Collections.ObjectModel.ObservableCollection(Of Person) = New ObjectModel.ObservableCollection(Of Person)
    For Each rec In srv.Persons.Execute
        Dim newP As New Person With {
                           .FirstName = rec.FirstName, _
                               .LastName = rec.LastName}
        personCollection.Add(newP)
    Next
    Return personCollection.ToArray
End Function
share|improve this answer
    
Sorry no i'm getting a 401 Unauthorized. I'm trying it with an account which has access to all site collections. code see above –  Steffen Schindler Jul 2 '13 at 13:43
    
ok if you getting a 401 Unauthorized error you need to perform an authentication request with the rest service. –  Joseph D Surgeon Jul 2 '13 at 21:22
    
wictorwilen.se/Post/… –  Joseph D Surgeon Jul 2 '13 at 21:23
    
I did it in .NET from WCF look at my revised answer –  Joseph D Surgeon Jul 2 '13 at 21:23
    
Ok, when you're using .Net and calling the webservice it is no problem. But when using a sharepoint hosted app you can't use .Net. –  Steffen Schindler Jul 4 '13 at 11:32

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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