Вопрос

I have a list of SPWeb IDs and would like to open the associated SPWeb objects. However, I do not know in which Site Collection they are (I DO know that they are in the current Web Application though).

Is there a good (fast!) way to do so, or will I need to have the Site Collection ID in any case?

Это было полезно?

Решение 4

Ended up changing my data so that I have both the Web and Site ID.

Другие советы

You can use the full URL to the web. When getting an SPWeb you have two options: 1) us the SPSite and parse through the Webs -or- 2) look it up by the full URL to the web.

I just ran into the same problem and I figured out that you can pass the entire URL into the constructor of SPSite and it will determine which portion of the url is the site collection. Here is my code snippet I used in a method where I am trying to seperate the string into the site collection url and the relative site path.

   try
   {
   //these local variables are created because an out or ref variable cannot be 
   //accessed inside SPSecuirty.RunWithElevatedPrivileges
   string siteCollectionPath = "";
   string relativeSitePath = "";
   SPSecurity.RunWithElevatedPrivileges(delegate()
   {
   using (SPSite spSite = new SPSite(fullPath))
   {
   relativeSitePath = fullPath.Replace(spSite.Url, "").TrimStart('/');
   using (SPWeb spWeb = spSite.AllWebs[relativeSitePath])
   {
   siteCollectionPath = spSite.Url;
   }
   }
   });
   siteCollectionUrl = siteCollectionPath;
   relativeSiteUrl = relativeSitePath;
   }
   catch (FileNotFoundException ex)
   {
   //the path does not point to a site collection
   }
   catch (UriFormatException ex)
   {
   //the path does not point to a site collection
   }
   catch (ArgumentException ex)
   { 
   //the relativeSitePath was invalid
   }

if you are in a web context and you know that your webs are in the current app, all you can is to loop through all site collections and try opening all your IDs on each SPSite object. Although please note that I would avoid holding more than one SPSite and SPWeb object in a time so it make sense to process your Webs and remove them from the list right after that. Not so quick process in any case. Possible workaround is to use LongRunningOperation for it (MS SPLRO sample)

Other possible approach is to use Search but I not sure how to do it that way

Лицензировано под: CC-BY-SA с атрибуция
Не связан с sharepoint.stackexchange
scroll top