+ Start a Discussion
ngOnengOne 

Downloading a Document from the Library using IOS and rest API

Hi I have tried a few option here but none seem to work, I dont know if it's because the sdk will only work with JSON data and not sure if i should go for SOAP Calls. I can list my libraries, and documents contained but cant download a document.

 

//# Option 1: Using the api factory method for the request. Of course it expects JSON return.
SFRestRequest *request = [[SFRestAPI sharedInstance] requestForRetrieveWithObjectType:@"ContentVersion" objectId:documentId fieldList: @"VersionData"];
    
     //# Option 2: Method executes but again it's expecting json return
    SFRestRequest *request = [[SFRestRequest alloc] init];
    [request setPath: @"/v23.0/sobjects/ContentVersion/[object id]/VersionData"];
    [request setMethod: SFRestMethodGET];
    [[SFRestAPI sharedInstance] send:request delegate:self];
    
    //#Option 3: get html error saying that the page doesnt exist,  could be related to the new url that needs to be set after login process, dont know how tho get it from the framework.

    NSURL *myURL = [NSURL URLWithString:@"https://test.salesforce.com/v23.0/sobjects/ContentVersion/[objectID]/VersionData"];
    NSMutableURLRequest* request = [[[NSMutableURLRequest alloc] initWithURL:myURL] autorelease];
    
    [request addValue:@"OAuth token" forHTTPHeaderField:@"Authorization"];
    [NSURLConnection connectionWithRequest:request delegate:self];

 

tstellanova-sfdctstellanova-sfdc

I plugged your request into the RestExplorer (under native/SampleApps/RestExplorer) and what I can see is that with option #1, VersionData is returned as the url to the actual content

{
    Id = 06830000001PSx4AAG;
    VersionData = "/services/data/v23.0/sobjects/ContentVersion/06830000001PSx4AAG/VersionData";
    attributes =     {
        type = ContentVersion;
        url = "/services/data/v23.0/sobjects/ContentVersion/06830000001PSx4AAG";
    };
}

So I guess what you actually want is option 3, using the "url" attribute from the VersionData response. The missing bit for option #3 is the instanceUrl for the server. That can be obtained from:

[[[[SFRestAPI sharedInstance] coordinator] credentials] instanceUrl];

(FWIW you can see in SFRestAPI.rkClient that this is the same way we setup the RestKit client:

_rkClient = [[RKClient alloc] initWithBaseURL:[_coordinator.credentials.instanceUrl absoluteString]];

If you obtain the server instance URL in this way, and avoid hardcoding eg test.salesforce.com, your code should work regardless of the actual server instance the user's org lives on.

ngOnengOne

Thanks a lot, that put on the right path. I issued a raw http request, and said my session had expired, probably i was missing some header, cookie, so I went back to #2

I modified the SDK to have the option to return the raw NSData object, and copied the new library.

 

So my code looks like.

 

 

 //# Option 2
    SFRestRequest *request = [[SFRestRequest alloc] init];
    [request setPath: @"/v23.0/sobjects/ContentVersion/068Q0000000FURRIA4/VersionData"];
    request.returnRawData = TRUE; [modified  SFRestRequest to add this property];
    [request setMethod: SFRestMethodGET];
    [[SFRestAPI sharedInstance] send:request delegate:self];

 

 

added a new method in the delegate that receives an NSData *, and modified RKRequestDelegateWrapper to include the following

 

 if(!_request.returnRawData){
        ...... original code goes here, will return json formatted data.
        
        if ([self.request.delegate respondsToSelector:@selector(request:didLoadResponse:)]) {
            [self.request.delegate request:_request didLoadResponse:jsonResponse];
        }
    }else{
        [self.request.delegate request:_request didLoadResponseRaw:response.body];
        
    }

 

Not sure how this solution will work with large files, probably will need to think of a more streaming solution.

 

brumbrum

Hi ngOne,

 

I am developing a mobile iOS app using SalesforceMobileSDK-iOS and I need to download a document (text file, images) from Salesforce. I posted this message : http://boards.developerforce.com/t5/Mobile/How-to-download-text-file-and-images-from-Salesforce-using-ios/td-p/440487

 

Could you help me to solve this problem?

 

Thanks,

brum