Sitecore Mobile SDK: how to read the value from a linkedItem in a DropLink

313 views Asked by At

I am creating a native IOS app by using the Sitecore Mobile SDK. So far I am able to read the items I need but I got stuck on reading the fieldvalue from a linked item in a Droplink field.

I use this code:

SCApiContext* context = [SCApiContext contextWithHost: @"http://<myhost>/-/item"];
SCItemsReaderRequest* request = [ SCItemsReaderRequest new ];
request.requestType = SCItemReaderRequestQuery;
request.request = @"/sitecore/content/Home/descendant::*[@@templatename='Content item']";
request.flags = SCItemReaderRequestReadFieldsValues;
request.fieldNames = [ NSSet setWithObjects: @"Content title", @"Content author", @"Content introduction", @"Content date", @"Content body" , nil ];

    [context itemsReaderWithRequest: request]( ^(id result, NSError* error)
    {
        NSArray* items = result;

        for (SCItem* item in result)
        {
            // get the author
            __block NSString *author = @"empty";
            SCField *dropLinkField = [item fieldWithName: @"Content author"];

            [dropLinkField fieldValueReader]( ^(id result, NSError *error)
            {
                if (!error)
                {
                    SCItem *linkedItem = result;

                    // TODO: author is not yet filled
                    NSSet *fieldsSet = [NSSet setWithObjects:@"Firstname", nil];
                    // this method seems to be skipped
                    [linkedItem fieldsReaderForFieldsNames:fieldsSet]( ^(id result2, NSError *error2)
                       {
                           if (!error2)
                           {
                               NSDictionary *fields = result2;
                               SCField *field_ = [fields objectForKey: @"Firstname"];
                               author = field_.rawValue;
                           }
                       });
                }
            });


        }

    }

The original item is read and I can read the field values of the droplink field. It also seems that I can read the linked Item, because I can write it's itempath to the log. But when I try to read a field from the linked item, it fails and the "fieldsReaderForFieldsNames" method seems to be skipped.

I'm obviously doing something wrong here, but seem to overlook the issue...

EDIT:

I forgot to mention that I use Sitecore 7, not sure if it makes a difference. I have added the lines above that creates the SCApiContext and SCItemReaderRequest.

I use anonymous access and in the "site settings" I use

itemwebapi.mode="StandardSecurity" 
itemwebapi.access="ReadOnly" 
itemwebapi.allowanonymousaccess="true"

I just thought that I found the issue, because I did not set the Field Remote Read rights on several fields. However, setting that permission did not resolve it and other fields without the Field Remote Read set, did return in the API.

1

There are 1 answers

0
Alexander Dodatko On BEST ANSWER

Sitecore iOS SDK operations (from the list below) are executed asynchronously on the background operation queue.
* fieldValueReader
* fieldsReaderForFieldsNames

This does not guarantee that author data is downloaded at the moment you are accessing it.

Please use downloaded items and fields in the completion callback block to ensure they exist on your iPhone.

[linkedItem fieldsReaderForFieldsNames:fieldsSet]( ^(id result2, NSError *error2)
{
    NSLog(@"Read author field");
    if (!error2)
    {
    NSLog(@"No error");
    NSDictionary *fields = result2;
    SCField *field_ = [fields objectForKey: @"Firstname"];
    author = field_.rawValue;

    // Now all required fields will 
    // definitely be downloaded by the time you create a blog item


    NSLog(@"voornaam: %@", author);

    ParTechBlogItem *blogItem;
    blogItem = [[ParTechBlogItem alloc] initWithTitle:[item fieldValueWithName:@"Content title"] 
                                                 date:[item fieldValueWithName:@"Content date"] 
                                                intro:[item fieldValueWithName:@"Content introduction"] 
                                               author:author 
                                                 text:[item fieldValueWithName:@"Content body" ]];
        [weakSelf addBlogItem:blogItem];
}