AGSPortalItem.serviceURL Is Blank

815
7
06-25-2019 08:48 AM
NickG
by
New Contributor II

Hi,

Trying to access the info of a feature service portal item via itemID.  I'm able to access other properties like 

title but not the serviceURL.  I've tried in both online and on premise but to no avail.  The items are shared with my user via a group.  I've even tried loading the item (portalItem.load) and still the url is empty. Not sure if a bug or I am missing something obvious.  Any help would be greatly appreciated.   Example code below.  Thank you. 

func getPortalFeatureServiceItem(){

        guard let portal = self.portal else {

            return

        }

        

        let portalItem = AGSPortalItem(portal: portal, itemID: "ID")

        

        if let itemURL = portalItem.serviceURL{

            print(itemURL.absoluteString) -> ""  //itemUrl gets set but is empty

        }

        print("item title \(portalItem.title)") -> "Title"

 

}

0 Kudos
7 Replies
Nicholas-Furness
Esri Regular Contributor

You will need to load the portal item. See the Loadable Resources guide.

Try something like this:

portalItem.load { (error) in
    if let error = error {
        print("Error loading portal item: \(error.localizedDescription)")
        return
    }
    
    print("Service URL: \(portalItem.serviceURL)")
}

Are you sure you can read the title? In my testing I am unable to read the title until it's loaded.

portalItem.load { (error) in
    if let error = error {
        print("Error loading portal item: \(error.localizedDescription)")
        return
    }
    
    print("Title (loaded): \(portalItem.title)")
}

print("Title (unloaded): \(portalItem.title)")

outputs this…

Title (unloaded): 

Title (loaded): Rockingham County Parcels

Notice how the unloaded output prints first. The loaded call requires a round-trip to load the metadata from the portal item.

Hope this helps.

Nick

0 Kudos
NickG
by
New Contributor II

I've tried loading the item.  Still no url.  Sorry shouldn't have included the title in the "unloaded" example.  

portalItem.load { (error) in         

           guard error == nil else { return }

            

            print("item title \(portalItem.title)") -> "Title"

            

            if let itemURL = portalItem.serviceURL{

                print(itemURL.absoluteString) -> ""

            }

        }

0 Kudos
Nicholas-Furness
Esri Regular Contributor

And it's a service item? E.g. a Feature Service? You won't get a serviceURL if it's, say, a web map. What do you see if you print the typeName?

0 Kudos
NickG
by
New Contributor II

As far as I know.  It's got the same title and I've tried the following code in the load block and it reaches the print statements.  

portalItem.load { (error) in         

           guard error == nil else { return }

            

            print("item title \(portalItem.title)") -> "Title"

            if (portalItem.type == .featureService){

                     

               guard let url = portalItem.serviceURL?.appendingPathComponent("0", isDirectory: false) else {

                         return

                }

                print("feature service") -> "feature service"

                print(url) -> 0

            }

}

0 Kudos
Nicholas-Furness
Esri Regular Contributor

Hmm. Looks like you're doing the right thing. Can you share the service with us? And which version of the Runtime are you using? Please DM me if you like.

It seems we have a bug where if there isn't a serviceURL (e.g. it's a Web Map) then we still return an empty URL, but we should be returning nil. This may be masking something else that's going on.

0 Kudos
NickG
by
New Contributor II

I used the code you provided on GitHub and it worked!  There was something clearly wrong with my code.  Sorry for sending you on a wild goose chase.  Your input was very helpful.  Thank you!  

Nicholas-Furness
Esri Regular Contributor

For reference, here's the code I provided:

import UIKit
import ArcGIS

class ViewController: UIViewController {
    
    @IBOutlet weak var mapView: AGSMapView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        let p = AGSPortal.arcGISOnline(withLoginRequired: false)
        
        let portalItem = AGSPortalItem(portal: p, itemID: "<feature service item id>")
        
        portalItem.load { (error) in
            if let error = error {
                print("Error loading portal item: \(error.localizedDescription)")
                return
            }

            print("Service URL: \(portalItem.serviceURL)")
            if let url = portalItem.serviceURL {
                print("URL: \(url.absoluteString)")
            }
            print("Title (loaded): \(portalItem.title)")
            print("Type: \(portalItem.typeName)")
        }
        
        print("Title (unloaded): \(portalItem.title)")
    }
}
0 Kudos