{"id":6293,"date":"2019-04-25T09:36:02","date_gmt":"2019-04-25T09:36:02","guid":{"rendered":"http:\/\/blog.softreeconsulting.com\/?p=1048"},"modified":"2019-04-25T09:36:02","modified_gmt":"2019-04-25T09:36:02","slug":"get-site-collection-from-web-application","status":"publish","type":"post","link":"https:\/\/softreetechnology.com\/blog\/sharepoint\/get-site-collection-from-web-application\/","title":{"rendered":"Get Site Collection from Web Application"},"content":{"rendered":"\n<p>In this blog,\nI will explain how to get all the site Collection under a SharePoint web\napplication by the help of SharePoint Search.<\/p>\n\n\n\n<p>We\ncan get the subsites under the site using CSOM easily but if we need to get all\nthe site collections under a specific web application from a SharePoint farm\nthen we can&#8217;t get it directly. For that, we have to use SharePoint search. By\nusing SharePoint Search we will set the &#8220;KeywordQuery&#8221; to filter and\nget the site collections. Before using the code block, we should ensure the\nSharePoint Search Service Application is running and active.<\/p>\n\n\n\n<p><strong><u>The code block for this is mentioned below.<\/u><\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>using System;  \nusing System.Collections.Generic;  \nusing System.Linq;  \nusing System.Net;  \nusing System.Text;  \nusing System.Threading.Tasks;  \nusing Microsoft.SharePoint.Client;  \nusing Microsoft.SharePoint.Client.Search.Query;  \nnamespace GetSitecollFromWebApplication {  \n    class GetSitecollByUsingSearch {  \n        static void Main(string[] args) {  \n            string webAppUrl = string.Empty;  \n            string username = string.Empty;  \n            string password = string.Empty;  \n            NetworkCredential credentials = null;  \n            webAppUrl = \"webAppUrl\"; \/\/Please provide the Web Application URL  \n            username = \"userName\";  \n            password = \"******\";  \n            ClientContext ctx = new ClientContext(webAppUrl); \/\/creating ClientContext from given SharePoint Site  \n            credentials = new NetworkCredential(username, password); \/\/creating creadential object for SharePoint site.  \n            ctx.Credentials = credentials;  \n            \/\/ Before executing the below code please ensure Search Service Application is running  \n            KeywordQuery keyQuery = new KeywordQuery(ctx);  \n            keyQuery.QueryText = \"contentclass:\\\"STS_Site\\\"\";  \n            keyQuery.RowLimit = 500; \/\/ the maximum row limit is 500 for KeywordQuery  \n            keyQuery.EnableStemming = true;  \n            keyQuery.TrimDuplicates = false;  \n            SearchExecutor searchExe = new SearchExecutor(ctx);  \n            ClientResult &lt; ResultTableCollection > resultTabColl = searchExe.ExecuteQuery(keyQuery);  \n            ctx.ExecuteQuery();  \n            var siteCollection = resultTabColl.Value.SelectMany(rs => rs.ResultRows.Select(r => r[\"Path\"])).ToList();  \n            if (siteCollection != null &amp;&amp; siteCollection.Count > 0) {  \n                foreach(var site in siteCollection) {  \n                    Console.WriteLine(site);  \n                }  \n                Console.ReadLine();  \n            } else {  \n                Console.WriteLine(\"Sorry, there were no Site Collections for the Web Application.\");  \n                Console.ReadLine();  \n            }  \n        }  \n    }  \n}  <\/code><\/pre>\n\n\n\n<p>In this blog, I have explained how you can get all the site Collection under a SharePoint web application. I hope this blog will help you out in a few scenarios.<\/p>\n\n\n\n<p><strong>This solution is brought to you by our SharePoint professionals.<\/strong><\/p>\n\n\n\n<p><a href=\"https:\/\/www.softreetechnology.com\/\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\"Softree Technology (opens in a new tab)\">Softree Technology<\/a>\u00a0employs SharePoint consultants; we are a technology services provider with the aim to help companies achieve exceptional performance through SharePoint. Our dedicated team of SharePoint consultants has the right bent of mind to understand and execute customer requirements.<\/p>\n\n\n\n<p>Be it\u00a0<a href=\"https:\/\/www.softreetechnology.com\/sharepoint-spfx-developments\/\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\"SPFx or SharePoint\u00a0add-in developments (opens in a new tab)\">SPFx or SharePoint\u00a0add-in developments<\/a>,\u00a0SharePoint 2019 developments, web part developments, migrating from SharePoint 2010\/2013 to SharePoint 2013\/2016\/Office 365, Office 365,\u00a0SharePoint hosted apps development\u00a0or something else in SharePoint, we strive to deliver the best<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this blog, I will explain how to get all the site Collection under a SharePoint web application by the help of SharePoint Search. We can get the subsites under the site using CSOM easily but if we need to get all the site collections under a specific web application from a SharePoint farm then we can&#8217;t get it directly. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[43],"tags":[244,245],"class_list":["post-6293","post","type-post","status-publish","format-standard","hentry","category-sharepoint","tag-get-site-collection-using-csom","tag-using-keywordquery-get-site-collection"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.6 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Get Site Collection from Web Application - Softree Technology<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/softreetechnology.com\/blog\/sharepoint\/get-site-collection-from-web-application\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Get Site Collection from Web Application - Softree Technology\" \/>\n<meta property=\"og:description\" content=\"In this blog, I will explain how to get all the site Collection under a SharePoint web application by the help of SharePoint Search. We can get the subsites under the site using CSOM easily but if we need to get all the site collections under a specific web application from a SharePoint farm then we can&#8217;t get it directly. [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/softreetechnology.com\/blog\/sharepoint\/get-site-collection-from-web-application\/\" \/>\n<meta property=\"og:site_name\" content=\"Softree Technology\" \/>\n<meta property=\"article:published_time\" content=\"2019-04-25T09:36:02+00:00\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Get Site Collection from Web Application - Softree Technology","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/softreetechnology.com\/blog\/sharepoint\/get-site-collection-from-web-application\/","og_locale":"en_US","og_type":"article","og_title":"Get Site Collection from Web Application - Softree Technology","og_description":"In this blog, I will explain how to get all the site Collection under a SharePoint web application by the help of SharePoint Search. We can get the subsites under the site using CSOM easily but if we need to get all the site collections under a specific web application from a SharePoint farm then we can&#8217;t get it directly. [&hellip;]","og_url":"https:\/\/softreetechnology.com\/blog\/sharepoint\/get-site-collection-from-web-application\/","og_site_name":"Softree Technology","article_published_time":"2019-04-25T09:36:02+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"Written by":"admin","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/softreetechnology.com\/blog\/sharepoint\/get-site-collection-from-web-application\/#article","isPartOf":{"@id":"https:\/\/softreetechnology.com\/blog\/sharepoint\/get-site-collection-from-web-application\/"},"author":{"name":"admin","@id":"https:\/\/softreetechnology.com\/blog\/#\/schema\/person\/98740297642f06debccdcee2de84086b"},"headline":"Get Site Collection from Web Application","datePublished":"2019-04-25T09:36:02+00:00","mainEntityOfPage":{"@id":"https:\/\/softreetechnology.com\/blog\/sharepoint\/get-site-collection-from-web-application\/"},"wordCount":230,"commentCount":0,"publisher":{"@id":"https:\/\/softreetechnology.com\/blog\/#organization"},"keywords":["Get site collection using csom","Using KeywordQuery get Site Collection"],"articleSection":["SharePoint"],"inLanguage":"en","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/softreetechnology.com\/blog\/sharepoint\/get-site-collection-from-web-application\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/softreetechnology.com\/blog\/sharepoint\/get-site-collection-from-web-application\/","url":"https:\/\/softreetechnology.com\/blog\/sharepoint\/get-site-collection-from-web-application\/","name":"Get Site Collection from Web Application - Softree Technology","isPartOf":{"@id":"https:\/\/softreetechnology.com\/blog\/#website"},"datePublished":"2019-04-25T09:36:02+00:00","breadcrumb":{"@id":"https:\/\/softreetechnology.com\/blog\/sharepoint\/get-site-collection-from-web-application\/#breadcrumb"},"inLanguage":"en","potentialAction":[{"@type":"ReadAction","target":["https:\/\/softreetechnology.com\/blog\/sharepoint\/get-site-collection-from-web-application\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/softreetechnology.com\/blog\/sharepoint\/get-site-collection-from-web-application\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/softreetechnology.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Get Site Collection from Web Application"}]},{"@type":"WebSite","@id":"https:\/\/softreetechnology.com\/blog\/#website","url":"https:\/\/softreetechnology.com\/blog\/","name":"Softree Technology","description":"Celebrating 10+ Years in SharePoint Consulting !","publisher":{"@id":"https:\/\/softreetechnology.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/softreetechnology.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en"},{"@type":"Organization","@id":"https:\/\/softreetechnology.com\/blog\/#organization","name":"Softree Technology","url":"https:\/\/softreetechnology.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en","@id":"https:\/\/softreetechnology.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/softreetechnology.com\/blog\/wp-content\/uploads\/2023\/03\/cropped-white-logo-soft.png","contentUrl":"https:\/\/softreetechnology.com\/blog\/wp-content\/uploads\/2023\/03\/cropped-white-logo-soft.png","width":844,"height":230,"caption":"Softree Technology"},"image":{"@id":"https:\/\/softreetechnology.com\/blog\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/softreetechnology.com\/blog\/#\/schema\/person\/98740297642f06debccdcee2de84086b","name":"admin","image":{"@type":"ImageObject","inLanguage":"en","@id":"https:\/\/softreetechnology.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/6fc78c8a7aa3fb0bf43c3b9a2e3962d7?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/6fc78c8a7aa3fb0bf43c3b9a2e3962d7?s=96&d=mm&r=g","caption":"admin"},"sameAs":["https:\/\/softreeconsulting.com"],"url":"https:\/\/softreetechnology.com\/blog\/author\/admin\/"}]}},"_links":{"self":[{"href":"https:\/\/softreetechnology.com\/blog\/wp-json\/wp\/v2\/posts\/6293","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/softreetechnology.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/softreetechnology.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/softreetechnology.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/softreetechnology.com\/blog\/wp-json\/wp\/v2\/comments?post=6293"}],"version-history":[{"count":0,"href":"https:\/\/softreetechnology.com\/blog\/wp-json\/wp\/v2\/posts\/6293\/revisions"}],"wp:attachment":[{"href":"https:\/\/softreetechnology.com\/blog\/wp-json\/wp\/v2\/media?parent=6293"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/softreetechnology.com\/blog\/wp-json\/wp\/v2\/categories?post=6293"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/softreetechnology.com\/blog\/wp-json\/wp\/v2\/tags?post=6293"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}