Skip to content
jimmywim edited this page Dec 16, 2012 · 4 revisions

Here are some notes on using the REST API Wrapper.

First, you'll need to include all the files that start 'SP' from the sample project (under the folder SPREST).

Then, to instantiate your session, make sure you have an import to SPClaimsHelper.h.

#import "SPClaimsHelper.h"

You can then grab your site collection url, username and password and instantiate your session.

SPClaimsHelper *spClaims = [ [SPClaimsHelper alloc] initWithUsernamePasswordSite:username password:password site:site];
[spClaims setDelegate:(id)self];
[spClaims GetTokens];

A delegate method is called by the Helper class that will inform you that the session is instantiated. Handle this to perform your next task.

- (void)tokenDelegate: (SPClaimsToken *)tokenClass didReceiveToken: (int)count
{
    NSLog(@"Tokens received");
    [self performSegueWithIdentifier:@"DoSomethingElse" sender:self];
}

You can then interrogate SharePoint using the standard REST api endpoints.

NSString *siteURL = [ [SPAuthCookies sharedSPAuthCookie] siteUrl];
NSMutableString *listQueryUrl = [ [NSMutableString alloc] initWithString:siteURL];
[listQueryUrl appendString:@"/_api/lists"];

SPRESTQuery *listsQuery = [ [SPRESTQuery alloc] initWithUrlRequestId:listQueryUrl id:@"Lists"];
[listsQuery setDelegate:(id)self];
[listsQuery executeQuery];

Note here that the SPAuthCookies class is used to re-use the site collection url. This is handy as it will prevent you needing to pass the url between view controllers or model classes. You'll need to #import "SPAuthCookies.h" in your class to do this.

A parameter called id is passed in, which is a free-text identifier for your query. You can use this if you are firing multiple queries in your class, as you can use a single return delegate method to find out which query is being handled.

The response from these calls come in the delegate method form:

-(void)SPREST:(id)SPREST didCompleteQueryWithRequestId:(SMXMLDocument *)result requestId:(NSString *)requestId
{
}

The body of the response is in the form of an SMXMLDocument class, which is provided thanks to xmldocument. This is a simple XML Document object parser to help find things you need in the result, for example:

NSMutableArray *listsArray = [NSMutableArray alloc];
    listsArray = [listsArray initWithCapacity:[ [result.root childrenNamed:@"entry"] count]];
    
    for(SMXMLElement *list in [result.root childrenNamed:@"entry"])
    {
        NSString *listName = [ [ [list childNamed:@"content"] childNamed:@"properties"] valueWithPath:@"Title"];
        
        //NSLog(@"List: %@", listName);
        [listsArray addObject:listName];
    }
    self.items = listsArray;

All requests are made with HTTP GET as standard, however you can change this in your SPRESTQuery class instance, using the setRequestMethod method:

[uploadImageQuery setRequestMethod:@"POST"];

If you want to attach a file to your request, call the setAttachedFile method, passing in NSData of your file:

[uploadImageQuery setAttachedFile:imageData];

You can consult the MSDN Documentation for more information specific to the REST API endpoints available to SharePoint 2013:

Using the SharePoint 2013 REST service

How to: Complete basic operations using SharePoint 2013 REST endpoints

Clone this wiki locally