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

Seach for metadata ==> No result when column containing space

Open natenjo opened this issue 2 years ago • 1 comments

I am new to this library and believe this really has potential for my use case!

I came across a situation where I need some guidance:

I am searching for files in SharePoint via metadata. According to KQL Specification I am trying to search by metadata with the following query:

Serial number:"T-EST-JD1"

I have files with this Metadata (screenshot from Edge, please note that in Chrome the space in "Serial number" is replaced with "x200", see this screenshot from Chrome): image

I am using the following code:

from office365.sharepoint.client_context import ClientContext

username = "[email protected]"
password = "xxx"
sp_site = 'https://xxx.sharepoint.com/sites/Test/'
ctx = ClientContext(sp_site).with_user_credentials(username, password)

search_string = "Serial number:\"T-EST-JD1\""

result = ctx.search.query(search_string, row_limit=100).execute_query()
results = result.value.PrimaryQueryResult.RelevantResults

for row in results.Table.Rows:
   print("Path     : ", row.Cells["Path"])

There are no results.

I also tried the following search terms (without success):

search_string = "Serial number:\"T-EST-JD1\""
search_string = "\"Serial number\":\"T-EST-JD1\""

when just searching ( search_string = "T-EST-JD1" )for the string, the results show up, but this is unreliable/not sufficient for my use case.

When querying the metadata of the files explicitly, I can get the results:

from office365.sharepoint.client_context import ClientContext

username = "[email protected]"
password = "xxx"
sp_site = 'https://xxx.sharepoint.com/sites/Test/'
relative_url = "/sites/Test/Sort_Inbox/Dest"
ctx = ClientContext(sp_site).with_user_credentials(username, password)
libraryRoot = ctx.web.get_folder_by_server_relative_path(relative_url)
ctx.load(libraryRoot)
ctx.execute_query()

#List files
files = libraryRoot.files
ctx.load(files)
ctx.execute_query()

for myfile in files:
    if  myfile:
        
        file_url = myfile.properties["ServerRelativeUrl"]
        
        file = ctx.web.get_file_by_server_relative_url(file_url).expand(["ModifiedBy", "listItemAllFields"]).get().execute_query()
                      
        MetaData_sn = file.listItemAllFields.get_property('Serial_x0020_number') 

        if MetaData_sn:
            print("File link: {0}".format(myfile.properties["ServerRelativeUrl"]))
            print("MetaData_sn ", MetaData_sn)
        print("\n\n")

Output:

python3 temp_gutgub.py

File link: /sites/Test/Sort_Inbox/Dest/Dokument.docx
MetaData_sn  T-EST-JD1

when using MetaData_sn = file.listItemAllFields.get_property('Serial number') I don't get any results, thats why I beleve I have to escape spaces with _x0020_ in the query.

Do you have any hints on this? I will create another two columns (one with a space and one without) to see if this is alsoways (and only) an issue when there is a space.

.. Or am I overseeing something else? I have added the files > 7 days ago, so the indexing shouldn't be a problem.

natenjo avatar Jun 22 '23 15:06 natenjo

I am experiencing a similar issue. I am trying to extract the data in a column, but I cannot see the column name from the listItemAllFields properties.

I have two columns I am interested in collecting. The one column is of date time format, the second is of Lookup type. When I use listItemAllFields, I can get the data time column, but trying to do the same for the Lookup type column doesn't work. I have also tested this, with and without spaces in the column name.

Any way forward for this? Is there a specific property which needs to be accessed, or a different method for collecting the file listItem or using something else entirely? I have used the same above logic for reference. My working hypothesis is that metadata columns of lookup type aren't exposed to the listItem object.

Any help would be greatly appreciated!

EBIMASTER117 avatar Nov 14 '23 16:11 EBIMASTER117