1

What's the most efficient way of retrieving ALL SPWeb (root and sub) in a SPWebApplication? Only SPWeb whose url contains a given string as a Url parameter should be retrieved.

For example:

I have 5 sites:

http://server.com:555/                            <---- should not be retrieved
http://server.com:555/sites/getthis               <---- should be retrieved
http://server.com:555/sites/root1/getthis/        <---- should be retrieved
http://server.com:555/sites/root2/dontgetthis/    <---- should not be retrieved
http://server.com:555/sites/root3/getthis/        <---- should be retrieved

I should query all SPWebs who have the url parameter "getthis" and nothing else.

So what is the most efficient way of doing this? (C#)

1

If you need to check if the site / subsite URL contains a specific value , I wrote this code in my side that working properly :

 using (SPSite site = new SPSite("http://Qassasrootsite/"))
            {
                using (SPWeb web = site.OpenWeb())
                {
                        //Get the sites / sub site 
                        string sitelist = "";
                        foreach (SPWeb spWeb in site.AllWebs)
                        {
                        if (spWeb.Url.ToString().Contains("Qassas"))
                          {
                            sitelist += "," + spWeb.Url.ToString();
                          }
                        }    
                }
            } 

The above solution will work as your above request (Contains) , but it will retrieve all the URL's that contains your provided value (not depend on where it find the provided value in the URL).

For example, you need to find the URL with qassas keyword

http://rootsite/qassas  should be retrieved
http://rootsite/qassas/wiki  should be retrieved

Meanwhile, if you need to check the last part in URL to check if it's equal the provided value or not try to split your URL as the following

using (SPSite site = new SPSite("http://Qassasrootsite/"))
            {
                using (SPWeb web = site.OpenWeb())
                {
                        //Get the sites / sub site 
                        string sitelist = "";
                        foreach (SPWeb spWeb in site.AllWebs)
                        {
                            var siteurl = spWeb.Url.ToString().Split('/');
                            // get the last item in site uel array that contains the site name
                            if (siteurl[siteurl.Length - 1].ToString() == "Qassas")
                            {
                                sitelist += "," + spWeb.Url.ToString();
                            }
                        }    
                }
            }

For example, you need to find the URL with qassas keyword

http://rootsite/qassas  should be retrieved
http://rootsite/qassas/wiki  should not be retrieved

Now you have the two code sample, you can select that match your requirements :)

  • I already knew that you could iterate through sites and webs. But I thought that maybe there was a more efficient way as this essentially retrieves all webs before determining whether those webs actually satisfy given parameters. – LeonidasFett Dec 5 '16 at 8:56
  • Hi @LeonidasFett , you didn't mention in your question that you knew this !!!! No problem, I only tried to help you , but I would like to tell you that any comparison criteria based on a specific value will require a looping specifically in your case, you tried to check if all site URL have a specific value or not so you should loop for all sites list, where i introduced a two method with contain function or with last part in your URL.so it should be efficient way because it achieve your target meanwhile it dispose the web and site object , Hope to find another efficient way,Good Luck! – Mohamed El-Qassas MVP Dec 5 '16 at 9:25
  • no problem dude. I wasn't bummed or anything. :) – LeonidasFett Dec 6 '16 at 14:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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