question

VinitJain-3394 avatar image
0 Votes"
VinitJain-3394 asked MichaelHan-MSFT commented

Not able to get folders which are inside folder with name containing special Charaters using sharepoint API

 from office365.runtime.auth.authentication_context import AuthenticationContext
 from office365.sharepoint.client_context import ClientContext
    
 url = "xxxx.com"
 site_url = url + "/xxxx/xxxx/xxxx"
    
 def auth_api(user, pas):                                                        
     ctx_auth = AuthenticationContext(url)                                       
     ctx_auth.acquire_token_for_user(user, pas)                                  
     ctx = ClientContext(siteurl, ctx_auth) # make sure you auth to the siteurl. 
     return ctx                                                                  
                                                                                   
 def folder_check(user, pas, folder_path, folder_to_check):
     try: 
        ctx = auth_api(user, pas)                                                        
        libraryRoot = ctx.web.get_folder_by_server_relative_url(folder_path)           
        ctx.load(libraryRoot)                                                          
        ctx.execute_query()                                                            
        folders = libraryRoot.folders                                                  
        ctx.load(folders)                                                              
        ctx.execute_query() #Breaks here                                                         
        for myfolder in folders:                                                       
            if folder_to_check in myfolder.properties["ServerRelativeUrl"]:                                                               
                return {'status': True,'message':'Folder Found'}                       
            else:                                                                      
                continue                                                               
        return {'status': False,'message':'Folder Not Found'}                          
    except Exception as e:                                                             
        return {'status': False,'message':str(e)}                                      
    
 folder_check('xxxxx', 'xxxx', "user # test", "test")


folder abc#123 contain folder test
but it is not printing test

office-sharepoint-online
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

MichaelHan-MSFT avatar image
0 Votes"
MichaelHan-MSFT answered MichaelHan-MSFT commented

Hi @VinitJain-3394 ,

In C#, we use ResourcePath API to deal with special charaters, then use method Web.GetFolderByServerRelativePath(ResourcePath) instead of Web.GetFolderByServerRelativeUrl(String) to get the folder.

And this method is not available in python : https://github.com/vgrem/Office365-REST-Python-Client/blob/master/office365/sharepoint/webs/web.py

So, i think you may need to use the method get_folder_by_id() to get the folder.


If an Answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.




· 4
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thanks ,Michael
but how can i get id of the same folder ?

0 Votes 0 ·

It may be a bit difficult to get the folder id.

Is this user # test a library?

0 Votes 0 ·

@MichaelHan-MSFT
user # test is a folder in Documents of sharepoint site

0 Votes 0 ·
Show more comments
KellyTaber-3026 avatar image
0 Votes"
KellyTaber-3026 answered KellyTaber-3026 commented

Hi,

This is the problem with spaces and characters in SharePoint. You will have to replace them with the Ascii Hex codes (https://ascii.cl/htmlcodes.htm). In this case you are going to replace the # with %23.
Hope this helps.

· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

I added %23 in folder path folder_=user%23test
but it didn't worked

0 Votes 0 ·

%20 represents spaces. If there are spaces you would need to replace them as well. Try user%20%23%20test

0 Votes 0 ·