Search Returns Feature Layers name but not Layer name

1294
3
Jump to solution
12-16-2020 08:23 AM
Clive_S
New Contributor III

Greetings,

I have been battling trying to get each associated Layer name returned, for a Feature Layer. 

I have approx. 300 Feature Layers.

Looking at one Feature layer (Temporary Works) it has five layers (work extent, Roads Poly, Roads Line, Temp Roads, label Points). I want to return the Feature layer (Temporary Works) which has five layers (work extent, Roads Poly, Roads Line, Temp Roads, label Points)

I want to Feature layer (Temporary Works) has five layers (work extent, Roads Poly, Roads Line, Temp Roads, label Points)

I want to return Feature Layer (id, url, Feature Layer name, layer url, layer name)

I get the (id, url, Feature Layer name) the Feature Layer name is returned five times, but I cannot get not the layer name. 

This is my code, I cannot get it to loop on FeatureName[0].

 

all_users = portal.users.search(query='owner:*',  max_users = 700)  
groups = portal.content.search("*",  item_type="Feature*")

## output file
outFile = (r"C:\Users\user\Data Catalogue\Portal_TEST.csv")
with open(outFile, 'w', newline='') as csvfile:
    filewriter = csv.writer(csvfile, delimiter=',', quotechar='|', quoting=csv.QUOTE_MINIMAL)
    filewriter.writerow(['group_id', 'group_title', 'group_owner', 'item_id', 'item_type', 'item_url', 'item_title', 'item_name',  'item_owner' ])
    for group in groups:
        #    ### Prints the Group Name + Owner
        #    #print(group.title, group.owner)
        for user in all_users:
            #print(group.id, group.title, group.owner)
            #### List the Items in Portal
            content_item = user.items()
            for item in content_item:
                #item = content_item.item.name[0]
                #print(item)
                #print(item.url)
                #print(item)
                #print(item)
                print(group.id, group.title, group.owner, item.id, item.title, item.type, item.owner, item.url)
                rowitem = (group.id, group.title, group.owner, item.id, item.type, item.url, item.title, item.name, item.owner)
                filewriter.writerow(rowitem )

print("END")

 

 

I get as an output:

 

 

1ab23ab12345670a12345678d1234567	Feature Service	https://my.com/server/rest/services/Temporary_Works/FeatureServer  Temporary_Works 	Temporary_Works
1ab23ab12345670a12345678d1234567	Feature Service	https://my.com/server/rest/services/Temporary_Works/FeatureServer  Temporary_Works 	Temporary_Works
1ab23ab12345670a12345678d1234567	Feature Service	https://my.com/server/rest/services/Temporary_Works/FeatureServer  Temporary_Works 	Temporary_Works
1ab23ab12345670a12345678d1234567	Feature Service	https://my.com/server/rest/services/Temporary_Works/FeatureServer  Temporary_Works 	Temporary_Works
1ab23ab12345670a12345678d1234567	Feature Service	https://my.com/server/rest/services/Temporary_Works/FeatureServer  Temporary_Works 	Temporary_Works

 

 

 

I would appreciate any pointers to return the Layer id and name.

Regards,

Clive 

0 Kudos
1 Solution

Accepted Solutions
JoshuaSharp-Heward
Occasional Contributor III

Hi Clive,

I'll admit I'm a little confused about your code, as I don't see any reference to FeatureName[0] and your variable "groups" seems to be returning your feature layers? Also your output doesn't match what's in your "rowitem" at all? 

Regardless, to access that information you need to access the FeatureLayer object properties, which gives you access to the name and id as a dictionary.

# this prints the dictionary with all of the extra info, flayer being a Feature Layer object
flayer.properties

# to print id and name respectively
flayer.properties["id"]
flayer.properties["name"]

If your intention is to iterate over every feature layer and print out that information, I think the following code does it a bit more efficiently.

serviceItems = gis.content.search("*",  item_type="Feature*", max_items=1000)
for item in serviceItems:
    layers = item.layers
    for lyr in layers:
        try:
            print(item.id, item.url, lyr.properties["id"], lyr.properties["name"])
        except Exception as e:
            print(e)

View solution in original post

3 Replies
JoshuaSharp-Heward
Occasional Contributor III

Hi Clive,

I'll admit I'm a little confused about your code, as I don't see any reference to FeatureName[0] and your variable "groups" seems to be returning your feature layers? Also your output doesn't match what's in your "rowitem" at all? 

Regardless, to access that information you need to access the FeatureLayer object properties, which gives you access to the name and id as a dictionary.

# this prints the dictionary with all of the extra info, flayer being a Feature Layer object
flayer.properties

# to print id and name respectively
flayer.properties["id"]
flayer.properties["name"]

If your intention is to iterate over every feature layer and print out that information, I think the following code does it a bit more efficiently.

serviceItems = gis.content.search("*",  item_type="Feature*", max_items=1000)
for item in serviceItems:
    layers = item.layers
    for lyr in layers:
        try:
            print(item.id, item.url, lyr.properties["id"], lyr.properties["name"])
        except Exception as e:
            print(e)
Clive_S
New Contributor III

Hi Joshua,

I spent ages, trying to get it to loop through the layers..

Thanks for your advice, much appreciated,  that provides the loop that I couldn't get working.

Regards,

Clive

0 Kudos
JoshuaSharp-Heward
Occasional Contributor III

No worries Clive, Glad I could help out!

Regards,

Josh

0 Kudos