Improve SiteExistsAnywhere in Tenant Extensions.
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.
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:
Method Summary: SiteExistsAnywhere
-
Initial Connection Attempt:
- Uses a
ClientContextto clone the tenant context with the specified site URI. - Loads the
Siteobject and executes a query. - If successful, returns
SiteExistence.Yes.
- Uses a
-
Error Handling:
- If an exception occurs:
-
Unauthorized Access: If the exception indicates unauthorized access, the site collection is considered to exist, returning
SiteExistence.Yes.
-
Unauthorized Access: If the exception indicates unauthorized access, the site collection is considered to exist, returning
- If an exception occurs:
-
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.
-
404 FILE NOT FOUND: Returns
- Sends an HTTP GET request to the site URL using an
-
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.
- If the original exception indicates that the site was not found, returns
To ensure the correctness and reliability of the SiteExistsAnywhere method, a unit test has been created.