pnpframework icon indicating copy to clipboard operation
pnpframework copied to clipboard

Improve SiteExistsAnywhere in Tenant Extensions.

Open vascoazevedo08 opened this issue 1 year ago • 0 comments

Overview This pull request addresses an issue with validating the existence of a site collection in SharePoint Online. Currently, when a tenant administrator attempts to validate a site collection's existence using its URI, it may result in errors due to reserved characters, specifically periods (.) in the URI.

Background In SharePoint, the following restrictions apply to site collection URIs:

  • Allowed characters: underscores (_), dashes (-), single quotes ('), and periods (.).
  • Restrictions: URIs cannot start or end with a period.

132e9ff6-1caf-46e2-9275-76dad0c84d08

In the Tenant Extensions class within Microsoft.SharePointOnline.CSOM, the method GetSitePropertiesByUrl and GetDeletedSitePropertiesByUrl, currently does not handle URIs with periods correctly, as illustrated below:

a49f45e2-f975-4bd5-ae29-dc69b0943107

Method Summary: SiteExistsAnywhere

  1. Initial Connection Attempt:

    • Uses a ClientContext to clone the tenant context with the specified site URI.
    • Loads the Site object and executes a query.
    • If successful, returns SiteExistence.Yes.
  2. Error Handling:

    • If an exception occurs:
      • Unauthorized Access: If the exception indicates unauthorized access, the site collection is considered to exist, returning SiteExistence.Yes.
  3. HTTP Request for Verification:

    • Sends an HTTP GET request to the site URL using an HttpClient.
    • If the response indicates a 404 status:
      • 404 FILE NOT FOUND: Returns SiteExistence.No, indicating the site does not exist.
      • CONNECTION: CLOSE: Indicates the site is in the recycle bin, returning SiteExistence.Recycled.
  4. Final Checks:

    • If the original exception indicates that the site was not found, returns SiteExistence.No.
    • If no definitive status can be determined, defaults to returning SiteExistence.Yes.

To ensure the correctness and reliability of the SiteExistsAnywhere method, a unit test has been created.

vascoazevedo08 avatar Oct 12 '24 18:10 vascoazevedo08