Skip to content

ProSnippets Sharing

arcgisprosdk edited this page Sep 4, 2015 · 21 revisions
Language:      C#
Subject:       Sharing
Contributor:   ArcGIS Pro SDK Team <[email protected]>
Organization:  Esri, http://www.esri.com
Date:          7/13/2015
ArcGIS Pro:    ArcGIS Pro 1.1
Visual Studio: 2013, 2015

##EsriHttpClient: Get the Current signed on User

//Reference Newtonsoft - Json.Net
  //Reference System.Net.Http
UriBuilder selfURL = new UriBuilder(PortalManager.GetActivePortal());
selfURL.Path = "sharing/rest/portals/self";
selfURL.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;

##Get the Groups for the Current Signed on User

//Assume that you have executed the "Get the Current signed on User" snippet and have 'userName'
UriBuilder groupsURL = new UriBuilder(PortalManager.GetActivePortal());
groupsURL.Path = String.Format("sharing/rest/community/users/{0}", userName);
groupsURL.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: Query for esri content on the active Portal

//http://www.arcgis.com/sharing/search?q=owner:esri&f=json

UriBuilder searchURL = new UriBuilder(PortalManager.GetActivePortal());
searchURL.Path = "sharing/rest/search";
searchURL.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: Get a Web Map for the Current User and Add it to Pro

UriBuilder searchURL = new UriBuilder(PortalManager.GetActivePortal());
searchURL.Path = "sharing/rest/portals/self";
searchURL.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.Create(itemID, ItemFactory.ItemType.PortalItem);

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

##EsriHttpClient: Get a Service Layer and Add it to Pro

UriBuilder searchURL = new UriBuilder(PortalManager.GetActivePortal());
searchURL.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.Create(itemID, ItemFactory.ItemType.PortalItem);

await QueuedTask.Run(async () => {
    // if we have an item that can be turned into a layer
    // add it to the map
    if (LayerFactory.CanCreateLayerFrom(currentItem))
        LayerFactory.CreateLayer(currentItem, MapView.Active.Map);
});
Clone this wiki locally