Office365-REST-Python-Client icon indicating copy to clipboard operation
Office365-REST-Python-Client copied to clipboard

Getting a list with columns that are from external lists

Open GushPanda opened this issue 2 years ago • 0 comments

Hello,

I have been trying to get lists using the previous topic here that helped immensely: https://github.com/vgrem/Office365-REST-Python-Client/issues/474

This has provided me the possibility to connect and eventually get the list into a dataframe, but I encountered a problem when the list contained columns that do not really exist in the specific list, but in external lists (using Lookup in the columns settings).

Is there a way to take the list as is into a dataframe? or to get the list including these columns?

Current code: `list_title = "FCR" view_title = "All Items"

ctx = ClientContext(site_url).with_credentials(client_credentials) list_object = ctx.web.lists.get_by_title(list_title)

view_fields = list_object.views.get_by_title(view_title).view_fields.get().execute_query()

Get Column names

fields = [list_object.fields.get_by_internal_name_or_title(field_name).get() for field_name in view_fields] ctx.execute_batch()

Print Column names

fields_names = {f.internal_name: f.title for f in fields} print(json.dumps(fields_names)) In Specific cases, when you have to change LinkTitle because the name changes, I use this: Change LinkTitle to Title because it is the actual name of the column "Name " fields_names["Title"] = fields_names.pop("LinkTitle") but unfortunately this is not sufficient in external list sourced columns

list_view = ctx.web.lists.get_by_title(list_title).views.get_by_title(view_title) list_items = list_view.get_items().execute_query()

Loop to transfer the info received into a DataFrame

rows = [] for i in list_items: cells = [] for f in fields_names.keys(): cells.append(i.properties[f]) rows.append(cells) df = pd.DataFrame(rows, columns=fields_names.values()) print(df)`

I have seen the columns defined in settings as coming from a different list as a source, using a Lookup type.

Is there a solution to get this list including these columns?

Thank you for you help.

GushPanda avatar Apr 04 '23 08:04 GushPanda