python-o365 icon indicating copy to clipboard operation
python-o365 copied to clipboard

SharePoint Site search doesn't seem to work

Open jjr5330 opened this issue 6 years ago • 3 comments

Hi I'm having some difficulty with the SharePoint site search... Example below.

I've already spent many hours cruising through the docs/ drive and SharePoint source code, but can't seem to wrap my head around this.

# initiate share_point account
share_point = account.sharepoint()

# conduct a site search using a '*' to return all sites
# also I am not sure this actually returns all sites as one of my sites wasn't listed here is there a better way?

site_search = share_point.search_site('*')
print(site_search)

# this print statement above proved that "my_sharepoint_site_name" existed in the list
# it was listed as Site: my_sharepoint_site_name

for site in site_search:
    if site == "my_sharepoint_site_name":
        site_id = site.object_id
        print(site_id)

This print statement in the for loop returned nothing. Although I know it exists because I printed it above and it was present in the list. I am looking to get the site object_id.

Additionally is there a way to search for a site object id via the host_name and path_to_site? As I mentioned above using a * to search all sites actually didn't return all of my sites, however I can access the data if I know the exact site_id. Alternatively it would be great to access it from host_name and url_path.

According to the SharePoint source code this should work under the get_site(host_name, path_to_site) function.

def get_site(self, *args):
        """ Returns a sharepoint site
        :param args: It accepts multiple ways of retrieving a site:
         get_site(host_name): the host_name: host_name ej.
         'contoso.sharepoint.com' or 'root'
         get_site(site_id): the site_id: a comma separated string of
         (host_name, site_collection_id, site_id)
         get_site(host_name, path_to_site): host_name ej. 'contoso.
         sharepoint.com', path_to_site: a url path (with a leading slash)
         get_site(host_name, site_collection_id, site_id):
         host_name ej. 'contoso.sharepoint.com'
        :rtype: Site
        """
# initiate share_point account
share_point = account.sharepoint()

# establish SharePoint host name
host_name = 'companyabc.sharepoint.com'

# establish url path for SharePoint site
path_to_site = '/teams/my_sharepoint_site_name/'

# structure get site call with host_name and path_to_site
site_search = share_point.get_site(host_name, path_to_site)

# the above code returns a 404 error url not found
# once again I know it exists because I can access it via a site-id
print(site_search)

site_id = site_search.object_id
print(site_id)

Any help is much appreciated.

jjr5330 avatar Mar 19 '20 14:03 jjr5330

if site == "my_sharepoint_site_name":

This expression is wrong. Correct one:

if site.name =="my_sharepoint_site_name":

As for the other question... I'm not a user of sharepoint for the moment so I can't tell...

You can look here for clues.

alejcas avatar Mar 19 '20 16:03 alejcas

One more point in your second try. Please list path_to_site = '/teams/my_sharepoint_site_name' instead of path_to_site = '/teams/my_sharepoint_site_name/'

vanducng avatar Apr 14 '20 05:04 vanducng

closing as there's no answer

alejcas avatar Oct 27 '20 16:10 alejcas