共享

ArcGIS 项目管理器:获取当前活动门户

1
2
var active_portal = ArcGISPortalManager.Current.GetActivePortal();
string uri = active_portal.PortalUri.ToString();

ArcGIS 项目管理器:获取所有门户的列表

1
2
3
var portals = ArcGISPortalManager.Current.GetPortals();
//Make a list of all the Uris
var portalUris = portals.Select(p => p.PortalUri.ToString()).ToList();

ArcGIS 项目管理器:将门户添加到门户列表

1
2
var portalUri = new Uri("http://myportal.esri.com/portal/", UriKind.Absolute);
ArcGISPortalManager.Current.AddPortal(portalUri);

ArcGIS 项目管理器:获取门户并登录,将其设置为活动状态

1
2
3
4
5
6
7
8
9
10
11
12
//Find the portal to sign in with using its Uri...
var portal = ArcGISPortalManager.Current.GetPortal(new Uri(uri, UriKind.Absolute));
if (!portal.IsSignedOn())
{
//Calling "SignIn" will trigger the OAuth popup if your credentials are
//not cached (eg from a previous sign in in the session)
if (portal.SignIn().success)
{
//Set this portal as my active portal
ArcGISPortalManager.Current.SetActivePortal(portal);
}
}

ArcGIS 程序管理器:侦听门户事件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
ArcGIS.Desktop.Core.Events.ActivePortalChangedEvent.Subscribe((args) =>
{

var active_uri = args.ActivePortal?.PortalUri.ToString();
//etc
});

ArcGIS.Desktop.Core.Events.ArcGISPortalAddedEvent.Subscribe((args) =>
{
var added_portal = args.Portal;
//etc
});

ArcGIS.Desktop.Core.Events.ArcGISPortalRemovedEvent.Subscribe((args) =>
{
var old_uri = args.RemovedPortalUri;
//etc
});

ArcGIS.Desktop.Core.Events.PortalSignOnChangedEvent.Subscribe((args) =>
{
var portal = args.Portal;
var isSignedOn = args.IsSignedOn;
//etc
});

门户:从活动门户获取当前登录用户

1
2
3
4
5
6
7
var portal = ArcGISPortalManager.Current.GetActivePortal();
//Force login
if (!portal.IsSignedOn())
{
portal.SignIn();
}
var user = portal.GetSignOnUsername();

门户:获取当前用户的“联机”门户视图

1
2
3
4
//If no-one is signed in, this will be the default view for
//the anonymous user.
var online = ArcGISPortalManager.Current.GetPortal(new Uri("http://www.arcgis.com"));
var portalInfo = await online.GetPortalInfoAsync();

门户:获取当前用户的组织 ID

1
2
3
var portal = ArcGISPortalManager.Current.GetPortal(new Uri(portalUri));
var portalInfo = await portal.GetPortalInfoAsync();
var orgid = portalInfo.OrganizationId;

门户:从活动门户获取活动用户的用户内容

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
var portal = ArcGISPortalManager.Current.GetActivePortal();
var owner = portal.GetSignOnUsername();
var userContent = await portal.GetUserContentAsync(owner);
//Get content for a specific folder (identified by its folder id)
//var userContent = await portal.GetUserContentAsync(owner, folderId);

//Get all the folders
foreach (var pf in userContent.PortalFolders)
{
//Do something with the folders

}

//Get all the content items
foreach (var pi in userContent.PortalItems)
{
//Do something with the portal items
}

门户:下载用户内容中的任何包项

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//user content previously from...
//var userContent = await portal.GetUserContentAsync(owner);

var packages = new List<PortalItemType>
{
PortalItemType.BasemapPackage,
PortalItemType.GeoprocessingPackage,
PortalItemType.LayerPackage,
PortalItemType.LocatorPackage,
PortalItemType.MapPackage,
PortalItemType.ProjectPackage,
PortalItemType.ScenePackage,
PortalItemType.RulePackage,
PortalItemType.VectorTilePackage
};
var folder = @"E:\Temp\PortalAPITest\";
foreach (var di in userContent.PortalItems.Where(pi => packages.Contains(pi.PortalItemType)))
{
var path = System.IO.Path.Combine(folder, di.Name);
await di.GetItemDataAsync(path);
}

门户:获取指定用户的组

1
2
3
4
5
6
7
//elsewhere...
//var owner = portal.GetSignOnUsername();
var groups = await portal.GetGroupsFromUserAsync(owner);
foreach (var group in groups)
{
//Do something with the portal groups
}

门户:执行门户搜索

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
var portal = ArcGISPortalManager.Current.GetPortal(portalUri);
var owner = portal.GetSignOnUsername();
var portalInfo = await portal.GetPortalInfoAsync();

//1. Get all web maps
var query1 = PortalQueryParameters.CreateForItemsOfType(PortalItemType.WebMap);

//2. Get all web maps and map services - include user, organization
// and "usa" in the title
var query2 = PortalQueryParameters.CreateForItemsOfTypes(new List<PortalItemType>() {
PortalItemType.WebMap, PortalItemType.MapService}, owner, "", "title:usa");
query2.OrganizationId = portalInfo.OrganizationId;

//retrieve in batches of up to a 100 each time
query2.Limit = 100;

//Loop until done
var portalItems = new List<PortalItem>();
while (query2 != null)
{
//run the search
PortalQueryResultSet<PortalItem> results = await portal.SearchForContentAsync(query2);
portalItems.AddRange(results.Results);
query2 = results.NextQueryParameters;
}

//process results
foreach (var pi in portalItems)
{
//Do something with the portal items
}

EsriHttpClient:获取当前登录用户

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//Reference Newtonsoft - Json.Net
//Reference System.Net.Http
UriBuilder selfURL = new UriBuilder(ArcGISPortalManager.Current.GetActivePortal().PortalUri)
{
Path = "sharing/rest/portals/self",
Query = "f=json"
};
EsriHttpResponseMessage response = new EsriHttpClient().Get(selfURL.Uri.ToString());

dynamic portalSelf = JObject.Parse(await response.Content.ReadAsStringAsync());
// if the response doesn't contain the user information then it is essentially
// an anonymous request against the portal
if (portalSelf.user == null)
return;
string userName = portalSelf.user.username;

获取当前登录用户的组

1
2
3
4
5
6
7
8
9
10
//Assume that you have executed the "Get the Current signed on User" snippet and have 'userName'
UriBuilder groupsURL = new UriBuilder(ArcGISPortalManager.Current.GetActivePortal().PortalUri)
{
Path = String.Format("sharing/rest/community/users/{0}", userName),
Query = "f=json"
};
var groupResponse = new EsriHttpClient().Get(groupsURL.Uri.ToString());
dynamic portalGroups = JObject.Parse(await groupResponse.Content.ReadAsStringAsync());

string groups = portalGroups.groups.ToString();

EsriHttpClient:查询活动门户上的 Esri 内容

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
//http://www.arcgis.com/sharing/search?q=owner:esri&f=json

UriBuilder searchURL = new UriBuilder(ArcGISPortalManager.Current.GetActivePortal().PortalUri)
{
Path = "sharing/rest/search",
Query = "q=owner:esri&f=json"
};
EsriHttpClient httpClient = new EsriHttpClient();
var searchResponse = httpClient.Get(searchURL.Uri.ToString());
dynamic resultItems = JObject.Parse(await searchResponse.Content.ReadAsStringAsync());

long numberOfTotalItems = resultItems.total.Value;
long currentCount = 0;

List<dynamic> resultItemList = new List<dynamic>();
// store the first results in the list
resultItemList.AddRange(resultItems.results);
currentCount = currentCount + resultItems.num.Value;
//Up to 50
while (currentCount < numberOfTotalItems && currentCount <= 50)
{
searchURL.Query = String.Format("q=owner:esri&start={0}&f=json", resultItems.nextStart.Value);
searchResponse = httpClient.Get(searchURL.Uri.ToString());
resultItems = JObject.Parse(await searchResponse.Content.ReadAsStringAsync());
resultItemList.AddRange(resultItems.results);
currentCount = currentCount + resultItems.num.Value;
}

EsriHttpClient:获取当前用户的 web 地图并将其添加到专业版

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
UriBuilder searchURL = new UriBuilder(ArcGISPortalManager.Current.GetActivePortal().PortalUri)
{
Path = "sharing/rest/portals/self",
Query = "f=json"
};
EsriHttpClient httpClient = new EsriHttpClient();
EsriHttpResponseMessage response = httpClient.Get(searchURL.Uri.ToString());

dynamic portalSelf = JObject.Parse(await response.Content.ReadAsStringAsync());
// if the response doesn't contain the user information then it is essentially
// an anonymous request against the portal
if (portalSelf.user == null)
return;
string userName = portalSelf.user.username;

searchURL.Path = "sharing/rest/search";
string webMaps = "(type:\"Web Map\" OR type:\"Explorer Map\" OR type:\"Web Mapping Application\" OR type:\"Online Map\")";
searchURL.Query = string.Format("q=owner:{0} {1}&f=json", userName, webMaps);

var searchResponse = httpClient.Get(searchURL.Uri.ToString());
dynamic resultItems = JObject.Parse(await searchResponse.Content.ReadAsStringAsync());

long numberOfTotalItems = resultItems.total.Value;
if (numberOfTotalItems == 0)
return;

List<dynamic> resultItemList = new List<dynamic>();
resultItemList.AddRange(resultItems.results);
//get the first result
dynamic item = resultItemList[0];

string itemID = item.id;
Item currentItem = ItemFactory.Instance.Create(itemID, ItemFactory.ItemType.PortalItem);

if (MapFactory.Instance.CanCreateMapFrom(currentItem))
{
Map newMap = MapFactory.Instance.CreateMapFromItem(currentItem);
await ProApp.Panes.CreateMapPaneAsync(newMap);
}

EsriHttpClient:获取服务图层并将其添加到专业版

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
UriBuilder searchURL = new UriBuilder(ArcGISPortalManager.Current.GetActivePortal().PortalUri)
{
Path = "sharing/rest/search"
};
string layers = "(type:\"Map Service\" OR type:\"Image Service\" OR type:\"Feature Service\" OR type:\"WMS\" OR type:\"KML\")";
//any public layer content
searchURL.Query = string.Format("q={0}&f=json", layers);

EsriHttpClient httpClient = new EsriHttpClient();

var searchResponse = httpClient.Get(searchURL.Uri.ToString());
dynamic resultItems = JObject.Parse(await searchResponse.Content.ReadAsStringAsync());

long numberOfTotalItems = resultItems.total.Value;
if (numberOfTotalItems == 0)
return;

List<dynamic> resultItemList = new List<dynamic>();
resultItemList.AddRange(resultItems.results);
//get the first result
dynamic item = resultItemList[0];

string itemID = item.id;
Item currentItem = ItemFactory.Instance.Create(itemID, ItemFactory.ItemType.PortalItem);

await QueuedTask.Run(() =>
{
//Create a LayerCreationParam
var layerParam = new LayerCreationParams(currentItem);
// if we have an item that can be turned into a layer
// add it to the map
if (LayerFactory.Instance.CanCreateLayerFrom(currentItem))
LayerFactory.Instance.CreateLayer<FeatureLayer>(layerParam, MapView.Active.Map);
});

;