Copying Items Between Portals Using Python | Getech

文章推薦指數: 80 %
投票人數:10人

Sometimes it's extremely useful to be able to manage your Portal via scripting and handily Esri provides the ArcGIS API for Python for doing ... Skiptocontent TwitterpageopensinnewwindowLinkedinpageopensinnewwindowYouTubepageopensinnewwindowinfo@getech.com+44(0)1133222200 Search: Search HomeLocateIndustriesIndustriesCarbonCapture&StorageCriticalMineralsGeothermalHealth,Safety,SecurityandEnvironmentHydrogenPetroleumPipelineRenewablesProductsCapabilitiesBasinEvolutionCrustalStructureDepositionalSystemsGIS&MappingPalaeoClimatePlateModellingPlayBasedExplorationUnconventionalDevelopmentsTechnologyDataAssistantExplorationAnalystGlobeHeatSeekerMaptiumUnconventionalsAnalystContentGravityDataMagneticDataDepthtoBasementSearchOurDataLibrarySolutionsSolutionsArcGISforRenewablesArcGISPipelineReferencingEmergencyResponseExplorationRiskManagementProductionOptimisationSiteAnalysis&PlanningServices&SupportServices&SupportG&MServicesG&MTrainingGISServicesGISTrainingCountryFocusCountryFocusAfricaAmericasEuropeMiddleEastAsia/AustralasiaDevelop&OperateGreenHydrogenGeothermalResponsibilityCorporateGovernanceSustainabilityInvestorsDirectorsandManagementBusinessModelStockExchangeAnnouncementsFinancialReportsPresentationsDocuments&CircularsAIMRule26AdvisersMedia&EventsMediaNewsBlogWebinarsUpcomingEventsContact CopyingItemsBetweenPortalsUsingPython Sometimesit’sextremelyusefultobeabletomanageyourPortalviascriptingandhandilyEsriprovidestheArcGISAPIforPythonfordoingjustthis. ThereasonfordevelopingthescriptwediscussinthisBlogwasaneedforacustomertoviewourdatawithinit’scorporateon-siteArcGISEnterprisePortal.SomeofourdatalayersarepublishedwithinArcGISOnline(AGOL),anditcanbetimeconsumingtosharelargevolumesofmapservicesbetweenPortals.Inthisinstancewewantedtoshareroughly1500items,soagoodoptionwastousetheArcGISAPIforPython.   Whatdoesthescriptdo? Thisscriptallowsyoutocopyitemsofinterest(inthiscaseMapandImageServices)betweenanAGOLPortalandanEnterprisePortal.Thescriptdoesnotexplicitlycopytheactualitem,insteaditcopiesthekey‘ingredients’thatmakeupthatiteminAGOLe.g.,thetitle,URL,andtypeofitem. HowwouldIgetstarted? YouwillneedtohaveArcGISProinstalledandlicensesonthemachineyouaredevelopingsoyoucanimporttherelevantEsriPythonlibraries,aswellasaPythonIDErunningtheArcGISProPythonenvironment.   Thecode Thesectionbelowshowsthestartofthescript.Notethatthevariablesneedtobeconfiguredtoyourspecificsetup.TheAGOLPortalwillbethesource,whileyourlocalArcGISEnterprisewillbethetargetPortal. Fromarcgis.gisimportGIS #localArcGISEnterpriseserviceinformation localUsername=“ENTERPRISEUSERNAME” localPassword=“ENTERPRISEPASSWORD” localPortalServer=“myserver.yourcompanyname.local” localPortalServerPath=“portal” localPortalServerPort=“443” #AGOLserviceinformation agolUsername=“AGOLUSERNAME” agolPassword=“AGOLPASSWORD” ThescriptsectionbelowconnectstothelocalEnterprisePortal.InthiscodeexampleIhaveincluded“verify_cert=False”.Thismaynotbenecessaryforyoursetupasmostlive/productioninstallationswillhaveproperlyconfiguredsecuritycertification–howeverIhadanunsecuredtestEnterprisePortalinstalledandforthecodetoworkIhadtopassthisargument. #connecttothedestinationportalinstance localgis=GIS(“https://”+localPortalServer+“:”+localPortalServerPort+“/”+localPortalServerPath,localUsername,localPassword,verify_cert=False) WhencopyingitemsintothetargetPortal,bydefaulttheitemswillbecopiedintotherootdirectory.Inmostcasesthisisprobablynotideal,soIdecidedtocreateafolderforallcontenttobecopiedto.Ifafolderofsamenamealreadyexiststhennoerroristhrown.Thismeansthatthereisnoneedtocheckforanexistingfolderwiththesamename,andalsothatthecontentsofthefolderwillnotbedeleted. However,thesamecannotbesaidofitemsthatyoucreate(e.g.mapimageorfeaturelayers)–thenamesforwhichshouldbeunique.Later,youwillseethatthecodeskipsitemsiftheyalreadyexistinthetargetdestination. #createfoldertoputcontentinto folder_name=“upload_folder” localgis.content.create_folder(folder_name) NowwearereadytoconnecttothesourcewhichisanAGOLPortal.Onceconnectedwemustgetasessiontoken. #connecttosourceportal(ArcGISOnlineinthiscase) gis=GIS(“https://arcgis.com”,agolUsername,agolPassword) token=gis._con.token Wearegoingtosearchforitemsinthesourceportalwhichhaveatag“Load21”.Youmayuseothersearchcriteriae.g.,searchbytitleorevenURL.Max_itemsisanimportantargumenthere.Thedefaultis1000,whichmaybesufficientforyourneeds.FurtherinformationonsearchoptionscanbefoundintheEsrionlineHelpforContentManager. #searchforitemsinsourceportalthatmatchthetag“Load21″ search_result=gis.content.search(query=”tags:”+‘Load21’,max_items=5000) ThelastpartofthescriptloopsthroughthesearchresultsfromtheAGOLPortalandcheckstoseeiftheitemalreadyexistswithintheEnterprise/targetPortal.Ifthesearchresultcountiszero,thentheitemisaddedusingdataPropsvariable.Thishasbeenpopulatedwiththeitemdetailsharvestedfromthesearchresult.Youneedthisinformationalongwiththetokeninordertoaddtheitem: Title Type URL Asanoptionalargumentwealsopassthefoldernametoplacethenewlycopieditemsinto. #loopthrougheachiteminsearch_resultlistandaddtodestinationPortal foriteminsearch_result: dataProps={‘title’:item.title,‘type’:item.type,‘url’:item.url,‘serviceUsername’:agolUsername,‘servicePassword’:agolPassword,‘token’:token} #checkifitemisalreadyinlocalGISbeforetryingtoadd. local_result=localgis.content.search(query=”title:”+item.title,max_items=1) iflen(local_result)==0: localgis.content.add(item_properties=dataProps,folder=folder_name) print(“Finished”) Conclusion Theprocessforcopyinganitemviasuchscriptingisveryquick–justafewseconds.YoushouldfindthatusingtheArcGISAPIforPythoninthiswaywillsaveyoualotoflaboriousandtediouswork–especiallyifyouhavehundredsorthousandsofitemstocopy.   PostedbySimonEarnshaw,GISDeveloper Category: Blog9thNovember2021 RelatedPostsDiagenesis:Akeydiscipline,ThenandNow27thJune2022GetechCommentaryonIEAWorldEnergyInvestmentReport202222ndJune2022MyTop5Featuresinv2.2ofUnconventionalsAnalystforArcGISPro15thJune2022ProbingtheFrontierinNamibia10thMay2022Maptium–Yourgatewaytogeoenergydataandinsight5thMay2022GeologicHistoryinBavariaUncoversPathwaystotheEnergyFuture31stJanuary2022 GotoTop Thiswebsiteusescookiestoimproveyouronlineexperience.PleasereadourupdatedPrivacyPolicytolearnhowwemanageandprotectyourdatainlinewiththenewGDPR.ReadMoreCookiesettingsACCEPTGetechGroupUpdatedPrivacy&CookiesPolicy Close PrivacyOverview Thiswebsiteusescookiestoimproveyourexperiencewhileyounavigatethroughthewebsite.Outofthese,thecookiesthatarecategorizedasnecessaryarestoredonyourbrowserastheyareessentialfortheworkingofbasicfunctionalitiesofthewebsite.Wealsousethird-partycookiesthathelpusanalyzeandunderstandhowyouusethiswebsite.Thesecookieswillbestoredinyourbrowseronlywithyourconsent.Youalsohavetheoptiontoopt-outofthesecookies.Butoptingoutofsomeofthesecookiesmayaffectyourbrowsingexperience. Necessary Necessary AlwaysEnabled Necessarycookiesareabsolutelyessentialforthewebsitetofunctionproperly.Thiscategoryonlyincludescookiesthatensuresbasicfunctionalitiesandsecurityfeaturesofthewebsite.Thesecookiesdonotstoreanypersonalinformation. Non-necessary Non-necessary Anycookiesthatmaynotbeparticularlynecessaryforthewebsitetofunctionandisusedspecificallytocollectuserpersonaldataviaanalytics,ads,otherembeddedcontentsaretermedasnon-necessarycookies.Itismandatorytoprocureuserconsentpriortorunningthesecookiesonyourwebsite. SAVE&ACCEPT



請為這篇文章評分?