Can't share item to group using Python API

3213
9
Jump to solution
02-05-2019 05:28 AM
JonathanBailey
Occasional Contributor III

I have a web map that I wish to share with a group in ArcGIS Enterprise. Using the Portal, I can share the map with the group:


However, if I try to share the same map with the same group using the Python API, the map cannot be shared with the group:

cbmt.share(org=True, groups=basemaps_group.title)

{'notSharedWith': ['Basemaps / Cartes de base'],
 'itemId': '21e8f2ab90904f4ba38e44386f03851c'}


Note that I am able to successfully share other maps (e.g., Esri basemaps) with this group within the same script.

Why can't I share the map with the group using the Python API?

1 Solution

Accepted Solutions
JonathanBailey
Occasional Contributor III

It turns out that, in spite of what the API documentation says as of today, that you must share the item using the group's id, and not the group's name.

cbmt.share(org=True, groups=basemaps_group.id)

View solution in original post

9 Replies
JonathanBailey
Occasional Contributor III

It turns out that, in spite of what the API documentation says as of today, that you must share the item using the group's id, and not the group's name.

cbmt.share(org=True, groups=basemaps_group.id)

PattyCharlotteson
New Contributor

HI Jonathan,

I am trying to share content with a user using the method that you recommended, but cannot get it figured out.

Here is a function that I wrote to try to do this. The function completes but doesn't actually show that anything was shared.

def ShareItemToGroup(user1_username1_itemId_groupId_:(
    print("Item: {}".format(itemId_))
    print("Group: {}".format(groupId_))

    usergroups = user1_['groups']
    usercontent = user1_.items()
    
    group = gis.groups.get(groupId_)

    for item in usercontent:
        if(item.id == itemId_):
            try:
                item.share(org=Falsegroups=group)
                print("Successfully shared item: {} with group: {}".format(itemId_, groupId_))
            except:
                print("Something went wrong...")
dcafdg
by
Occasional Contributor II

I had the same issue as Patty Charlotteson‌. The docs say you can pass in a list of group ID strings or a CSV of the same. Neither worked for me. They didn't crash, but nor did they do what I asked. Anyone at ESRI GIS watching the forums for bugs? 

As Patty showed, you can create a group object and pass that in to Item.share as the groups parameter. Note that the docs say you're supposed to pass in a list of group objects, which I did and it worked. I only needed one group, so I only tried a one-element list. 

0 Kudos
SimonGIS
New Contributor III

Thanks @JonathanBailey - this solved my issue as well

0 Kudos
UriGilad_EsriAu
Esri Contributor

The errors above can also occur because items cannot be shared with group having users_update_items=True. 

This is defined when the group is created and cannot be changed later:

ugilad_esriau_0-1618289652846.png

 

 

JustinReynolds
Occasional Contributor II

This was the case for me.  You can share to a group with Update Capabilities, but you need to pass an additional argument to the share method (allow_members_to_edit=True).

E.g.

map_item_id = 'c42004226e264f5389dbaf7d6fea60ae'
group_id = '591520ab3f2f421b83e271e1d40b74f3' # An Update Capable Group

map_item = gis.content.get(map_item_id)
if map_item:
    share_map = map_item.share(groups=group_id, allow_members_to_edit=True)

if share_map:
    print(share_map)

# -------------
{'results': [{'itemId': 'c42004226e264f5389dbaf7d6fea60ae',
   'success': True,
   'notSharedWith': []}]}

 

- Justin Reynolds, PE
0 Kudos
MikeLachance1
New Contributor III

I had issues with this too. Had to pass it the group ID not the group itself.

Also encountered an unintended scenario where trying to share an item to a group was setting that item back to sharing level 'Owner' from 'Org' or 'Public'.

This may be on me for not reading the Python API docs enough, but what I found was that while I was only trying to add an item to a group with this method, because I hadn't specified a value for the everyone and org arguments, it was applying them to the item as well but using the default values. So the item was getting added to the intended group, and having it's everyone and org values set to False, which switched sharing for it back to 'Owner' breaking the apps and maps this item was a part of.

To avoid this, first use the shared with property of the item to gather the current sharing settings, and use those in addition to any sharing additions you are adding when using the share method. Otherwise you may end up programmatically unintentionally changing the share settings for many items in your Org and suddenly have broken many public facing resources without knowing why.

I assumed the share method would allow you to change the share settings you specified, but what I found was that if I didn't specify every argument  (even the ones that weren't changing and were optional) the method changed them to the default anyways. Not great in my opinion and maybe ESRI can remedy that.

0 Kudos
BrianWilson7
Regular Contributor

This is what I get -- I can pass in three different groups, all of which I created, and it will add one of them. Nothing matters, I pass individual IDs or I pass a list of IDs or I pass a list of group objects (as seen here) -- always the same -- it will share with some groups but not others. There has to be some magic "don't share with this one" flag that I can't see. Here's a snippet from a jupyter notebook.

 

service_id = "e5b7345b622040f096fa923a807f55d0"

group_manager = gis.groups
gg = group_manager.search(query='GIS Team')[0]
print("group id", gg.id)
gc = group_manager.search(query='Cartographers')[0]
print("group id", gc.id)
ge = group_manager.search(query='Emergency Management')[0]
print("group id", ge.id)

content_manager = gis.content
service = content_manager.get(service_id)
print("service", service)

#rval = gis.content.share_items([service_id], groups=['Cartographers'])
rval = service.share(groups=[gg,gc,ge])
print("rval =", rval)

print('shared with', service.shared_with)

 

Here is the output. Everything "succeeds" it always returns "success=True" and "notSharedWith" some of the inputs.

 

group id 53378c27d6b34383bc4fc2aa87fc630d
group id 8b6bd8ec1a4142f988119e6290af51d7
group id 0682bb6881ab41b691bedafd2f2740b2
service <Item title:"Surveys_MIL" type:Map Image Layer owner:bwilson@CLATSOP>
rval = {'results': [{'itemId': 'e5b7345b622040f096fa923a807f55d0', 'success': True, 'notSharedWith': ['', '53378c27d6b34383bc4fc2aa87fc630d', '8b6bd8ec1a4142f988119e6290af51d7']}]}
shared with {'everyone': False, 'org': False, 'groups': [<Group title:"Emergency Management" owner:bwilson@CLATSOP>]}

 

 

0 Kudos
BrianWilson7
Regular Contributor

The "magic flag" is the same as above -- I added this and it works now. Just always add this I guess.

, allow_members_to_edit=True

 

0 Kudos