I posted a similiar question some time ago, but never got a good
response so I am trying again.
I would like to build a web part that I can drop onto any site that
would show the sites on the the logged in user has access. So if I
have the following:
http://<server>/sites/Mainsite
http://<server>/sites/Mainsite/Sub1
http://<server>/sites/Mainsite/Sub2
http://<server>/sites/qa
http://<server>/sites/it
etc...
I would like to drop the webpart on /Mainsite and within the webpart
it would list /Mainsite, /qa, /it, etc if that user has access to it.
All I can seem to get at this point is the list of subsites under /
Mainsite.
I am not sure if maybe I need to somehow grab all the site collections
first and then enumerate through them.
Any thoughts?
Thanks.
Otherwise, the best way to do this is to query the SharePoint search
service (provided it is configured correctly) which will return the
sites very quickly no matter how many sites the user can access. This
number could be in the hundreds depending on the size of your farm.
This is some example code (only works in MOSS or Search Server) to query
the search service for the user's allowed sites:
private DataTable GetSitesForUser() {
SPWebApplication webapp =
SPControl.GetContextWebApplication(HttpContext.Current);
//For "ServerContext" to work, a zone must be defined for
the incoming URL
using (FullTextSqlQuery fullTextSqlQuery = new
FullTextSqlQuery(ServerContext.Current)) {
fullTextSqlQuery.StartRow = 0;
fullTextSqlQuery.RowLimit = 0;
fullTextSqlQuery.EnableStemming = true;
fullTextSqlQuery.TrimDuplicates = true;
fullTextSqlQuery.Culture = CultureInfo.CurrentCulture;
fullTextSqlQuery.KeywordInclusion =
KeywordInclusion.AnyKeyword;
if (SPSecurity.AuthenticationMode !=
AuthenticationMode.Windows) {
fullTextSqlQuery.AuthenticationType =
QueryAuthenticationType.PluggableAuthenticatedQuery;
} else {
fullTextSqlQuery.AuthenticationType =
QueryAuthenticationType.NtAuthenticatedQuery;
}
fullTextSqlQuery.ResultTypes =
ResultType.RelevantResults;
// Returns only sites and site collections that are not used in shared
service provider web applications
fullTextSqlQuery.QueryText = "select title, path from
scope() where (contentclass = 'STS_Web' or contentclass = 'STS_Site')
and path not like '%ssp/admin%' order by path";
ResultTableCollection resultTableCollection =
fullTextSqlQuery.Execute();
ResultTable resultTable =
resultTableCollection[resultTypes];
DataTable dtResults = new DataTable();
dtResults.Load(resultTable,
LoadOption.OverwriteChanges);
DataView dv = new DataView(dtResults);
dv.Sort = "path";
return dv.ToTable();
}
}
The resulting DataTable will have a column named Title for the site's
title and a column named Path for the site's url.
--
Bryan Phillips
MCT, MCSD, MCDBA, MCSE
Microsoft MVP - Client Application Development
Blog: http://bphillips76.spaces.live.com
Web Site: http://www.composablesystems.net
"greg...@alpinebiomed.com" <greg...@alpinebiomed.com> wrote in
message
news:34fd40cc-3d2e-4148...@k9g2000prh.googlegroups.com:
Thanks Bryan-I appreciate the fast response. I'll take a look at the
code you posted. We are using WSS 3.0. I had installed Search
Server, but I right now I have it disabled as it didn't seem to work
in the way I had hoped. I would suspect we are not going to have any
more than 25-30 sites at one time.
Thanks again,
Greg
Why not try the GetSubwebsForCurrentUser () method... Returns the
collection of subsites beneath the current site of which the current
user is a member
http://msdn.microsoft.com/en-us/library/ms478651.aspx
Michael
Hi Michael,
That only gives me the sites underneath the site I am on. What I am
trying to do is get all the sites on my server from any site I am on.
Bsaically, I want a webpart on the homepage of my WSS based Intranet
that will show each individual user his or her sites that he is a
member. I have managed to accomplish this with the
following code. Maybe it will help someone:
Label webServiceInfo = new Label();
Label browserInfo = new Label();
SPSecurity.RunWithElevatedPrivileges(delegate(){
string siteLink = "http://" +
HttpContext.Current.Request.ServerVariables["SERVER_NAME"] + ":" +
HttpContext.Current.Request.ServerVariables["SERVER_PORT"];
SPWebApplication oWebApplication = SPWebApplication.Lookup
(new Uri(siteLink));
SPSiteCollection allSites = oWebApplication.Sites;
webServiceInfo.Text = "<table cellspacing=0 cellpadding=0
width=245px style=border-color:#CDCDCD;background-color: white;>";
foreach (SPSite siteCollection in allSites)
{
using (SPWeb web = siteCollection.OpenWeb())
{
try
{
webServiceInfo.Text = webServiceInfo.Text +
"<tr><td width=100% style=background-color:#f2f8ff>";
SPUser oUser = web.AllUsers
[HttpContext.Current.User.Identity.Name];
webServiceInfo.Text = webServiceInfo.Text +
" <a href=" + siteCollection.Url + ">" +
siteCollection.RootWeb.Title + "</a><br />";
foreach (SPWeb mySubs in
web.GetSubwebsForCurrentUser())
{
webServiceInfo.Text = webServiceInfo.Text
+ "   <a href=" + mySubs.Url + ">" + mySubs.Title
+ "</a><br />";
}
webServiceInfo.Text = webServiceInfo.Text + "</
td></tr>";
webServiceInfo.Text = webServiceInfo.Text +
"<tr><td class=ms-consolehr width=100%></td></tr>";
}
catch
{
}
}
}
webServiceInfo.Text = webServiceInfo.Text + "</td></tr></
table>";
this.Controls.Add(webServiceInfo);
});
Probably not a great solution if I had a lot of sites, but it's
pretty
quick right now.