Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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?

share|improve this question
    
Need to understand the context.. are you planning to do something one time only? using PowerShell? –  Ashish Patel Dec 5 '11 at 20:36
    
@Ashish No, it's for a web part that has to access a bunch of SPWebs. Currently I only have a List<Guid> with the SPWeb.ID, but now I need to do this cross-Site collection and wonder if I can avoid changing the data structure to also have the Site Collection ID. –  Michael Stum Dec 5 '11 at 21:32
    
My current algorithm goes through all Site Collections and Webs to find it, which is obviously slow, so I want to optimize it, preferably without changing the underlying data. –  Michael Stum Dec 5 '11 at 21:33
add comment

4 Answers 4

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
   }
share|improve this answer
add comment
up vote 1 down vote accepted

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

share|improve this answer
4  
I wouldn't call this the "answer" to your problem. If anything I would've edited and/or closed your question. –  Nick Larter Dec 20 '11 at 1:35
1  
well, it is actually good enough answer but from architecture perspective instead of development one :) I would vote up just for thinking ootb –  Ivan Padabed Sep 8 '12 at 7:15
add comment

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

share|improve this answer
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.