I've got https://community.esri.com/thread/219121-find-all-content-for-all-users-in-an-agol-subscription working, but what I'm looking for is some parsed output, hopefully a CSV that shows ITEM NAME, TYPE, OWNER, description,snippet, tags, shared_with, Basically everything from https://esri.github.io/arcgis-python-api/apidoc/html/arcgis.gis.toc.html#item
Hoping this is a pretty common thing and someone has some code snippets they can share.
Thomas Colson
Here you go:
<SPAN class="keyword token">from</SPAN> arcgis<SPAN class="punctuation token">.</SPAN>gis <SPAN class="keyword token">import</SPAN> GIS <SPAN class="keyword token">import</SPAN> csv <SPAN class="keyword token">import</SPAN> getpass portal <SPAN class="operator token">=</SPAN> input<SPAN class="punctuation token">(</SPAN><SPAN class="string token">'Enter the URL of the portal instance'</SPAN><SPAN class="punctuation token">)</SPAN> username <SPAN class="operator token">=</SPAN> input<SPAN class="punctuation token">(</SPAN><SPAN class="string token">'Enter the username'</SPAN><SPAN class="punctuation token">)</SPAN> password <SPAN class="operator token">=</SPAN> getpass<SPAN class="punctuation token">.</SPAN>getpass<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN> gis <SPAN class="operator token">=</SPAN> GIS<SPAN class="punctuation token">(</SPAN>portal<SPAN class="punctuation token">,</SPAN> username<SPAN class="punctuation token">,</SPAN> password<SPAN class="punctuation token">)</SPAN> <SPAN class="comment token">#leave the type param blank to return all types</SPAN> itemsList <SPAN class="operator token">=</SPAN> gis<SPAN class="punctuation token">.</SPAN>content<SPAN class="punctuation token">.</SPAN>search<SPAN class="punctuation token">(</SPAN><SPAN class="string token">''</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">''</SPAN><SPAN class="punctuation token">,</SPAN> max_items <SPAN class="operator token">=</SPAN> <SPAN class="number token">100</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="comment token">#list of fields to populate our header in the CSV</SPAN> fields <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">[</SPAN><SPAN class="string token">'Title'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'Type'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'Size'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'Owner'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'Description'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'Tags'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'Snippet'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'Categories'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'Access'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'URL'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'Shared With'</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="keyword token">def</SPAN> <SPAN class="token function">itemRetrieval</SPAN><SPAN class="punctuation token">(</SPAN>arr<SPAN class="punctuation token">,</SPAN> csvPath<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">try</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">with</SPAN> open<SPAN class="punctuation token">(</SPAN>csvPath<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'w'</SPAN><SPAN class="punctuation token">,</SPAN> encoding<SPAN class="operator token">=</SPAN><SPAN class="string token">'utf-8'</SPAN><SPAN class="punctuation token">,</SPAN> newline <SPAN class="operator token">=</SPAN> <SPAN class="string token">''</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">as</SPAN> outfile<SPAN class="punctuation token">:</SPAN> csvfile <SPAN class="operator token">=</SPAN> csv<SPAN class="punctuation token">.</SPAN>writer<SPAN class="punctuation token">(</SPAN>outfile<SPAN class="punctuation token">)</SPAN> csvfile<SPAN class="punctuation token">.</SPAN>writerow<SPAN class="punctuation token">(</SPAN>arr<SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">for</SPAN> item <SPAN class="keyword token">in</SPAN> itemsList<SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN>item<SPAN class="punctuation token">)</SPAN> row <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">[</SPAN>item<SPAN class="punctuation token">.</SPAN>title<SPAN class="punctuation token">,</SPAN> item<SPAN class="punctuation token">.</SPAN>type<SPAN class="punctuation token">,</SPAN> item<SPAN class="punctuation token">.</SPAN>size<SPAN class="punctuation token">,</SPAN> item<SPAN class="punctuation token">.</SPAN>owner<SPAN class="punctuation token">,</SPAN> item<SPAN class="punctuation token">.</SPAN>description<SPAN class="punctuation token">,</SPAN> item<SPAN class="punctuation token">.</SPAN>tags<SPAN class="punctuation token">,</SPAN> item<SPAN class="punctuation token">.</SPAN>snippet<SPAN class="punctuation token">,</SPAN> item<SPAN class="punctuation token">.</SPAN>categories<SPAN class="punctuation token">,</SPAN> item<SPAN class="punctuation token">.</SPAN>access<SPAN class="punctuation token">,</SPAN> item<SPAN class="punctuation token">.</SPAN>url<SPAN class="punctuation token">,</SPAN> item<SPAN class="punctuation token">.</SPAN>shared_with<SPAN class="punctuation token">]</SPAN> csvfile<SPAN class="punctuation token">.</SPAN>writerow<SPAN class="punctuation token">(</SPAN>row<SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">except</SPAN> Exception <SPAN class="keyword token">as</SPAN> error<SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="string token">'The following error occurred: '</SPAN><SPAN class="punctuation token">,</SPAN> error<SPAN class="punctuation token">)</SPAN> itemRetrieval<SPAN class="punctuation token">(</SPAN>fields<SPAN class="punctuation token">,</SPAN> input<SPAN class="punctuation token">(</SPAN><SPAN class="string token">'Paste Path and Name of Output File : '</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
portal_firsturl = 'url'
portal_firstuser = 'user'
portal_firstpass = 'pwd'
def authenticate_portal(portalurl, portaluser, portalpass):
try:
portal = arcgis.GIS(portalurl, portaluser, portalpass)
if not portal.properties.user.role == 'org_admin':
return False
return portal
except:
raise
def getItemInfo(listsorteditems):
portalrelationshipgroups=dict()
portalrelationshipgroups=[]
for p,item in enumerate(listsorteditems):
grp=[]
for index in range(len(item.shared_with['groups'])):
grp.append(item.shared_with['groups'][index]['title'])
portalrelationshipgroups.append( { "title": item["title"],"id":item.id,"type":item["type"],"owner":item.owner,"shared_with":",".join(grp),"description":item.description,"snippet":item.snippet,"tags":",".join(item.tags)} )
with open('test.csv', 'w') as myfile:
wr = csv.writer(myfile, quoting=csv.QUOTE_ALL,delimiter='\n')
wr.writerow(portalrelationshipgroups)
portalobj = authenticate_portal(portal_firsturl,portal_firstuser,portal_firstpass)
listitems_first = portalobj.content.search('', max_items=10)
listsorteditems_first = sorted(listitems_first, key=lambda k: k['title'])
itemInfo = getRelationshipGroups(listsorteditems_first)
Signed in members can post, follow updates, and more. New here? Register a free account.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.