I am trying to obtain a list representing all sites and subsites of a
given Sharepoint Portal Server.
for example I would like a list of all sitenames in a SPS site
structure
- SPS root (http://sharepoint)
- site1
- site2
site4
- site4
- site5
Using the code below I only get the sites of the root, but not their
subsites
Webs mySPWeb = new Webs();
mySPWeb.Credentials = new NetworkCredential("x","x");
myWebs = mySPWeb.GetAllSubWebCollection();
What must I do in order to get ALL the sites and their subsites???
I am using webservices to access the SPS data because it is a Windows
Form C# application running on workstations, so it's NOT running on the
SPS machine. Therefore I can't use the SPS object model.
I have reached the end of Internet looking for code smaples and/or
documentation, so I hope one of you excellent guru's can help me out ;)
Thanks in advance,
Peter
I've tried that too, but I don't think there's a way to get the Top
Sites list through the Object Model. GetAllSubWebCollection() return
the sites in the current collection only.
What you might consider is using a script to generate the list in a
.xml file using stsadm -o enumsites, and then use that file to create
your top sites list or get the list straight from the Database. Then
retrieve your structure through the Object Model.
Hopes that helps,
public override string BuildWebPartHTML()
{
StringBuilder htmlOutput = new StringBuilder();
//--------------------------------------------------------------------------
------
// The Site names will be temporarily put in an XML document
//--------------------------------------------------------------------------
------
_xmlDoc = new XmlDocument();
XmlDeclaration decl = _xmlDoc.CreateXmlDeclaration("1.0", null, null);
_xmlDoc.AppendChild(decl);
_xmlDoc.AppendChild(_xmlDoc.CreateElement("Sites"));
//--------------------------------------------------------------------------
------
// Retrieve the current user attributes
//--------------------------------------------------------------------------
------
_userPrincipal = (WindowsPrincipal)Thread.CurrentPrincipal;
_CurrentUserLoginName = _userPrincipal.Identity.Name;
SPGlobalAdmin globalAdmin = new SPGlobalAdmin();
_isCurrentUserAdmin = globalAdmin.IsCurrentUserGlobalAdmin();
//--------------------------------------------------------------------------
------
// Must impersonate an admin to enumerate all the sites
//--------------------------------------------------------------------------
------
try
{
string domain = ConfigurationSettings.AppSettings["impersonation_domain"];
string user = ConfigurationSettings.AppSettings["impersonation_user"];
string password =
ConfigurationSettings.AppSettings["impersonation_password"];
WindowsIdentity wi = CreateIdentity(user, domain, password);
_wic = wi.Impersonate();
System.Uri uri = new System.Uri("http://" + System.Environment.MachineName);
SPVirtualServer virtualServer = globalAdmin.OpenVirtualServer(uri);
SPSiteCollection siteCollections = virtualServer.Sites;
int siteCollectionRootLength = _SiteCollectionRoot.Length;
//--------------------------------------------------------------------------
------
// For each site collection under the specified root (ex.: sites)
//--------------------------------------------------------------------------
------
foreach (SPSite siteCollection in siteCollections)
{
if (siteCollection.ServerRelativeUrl.Length < siteCollectionRootLength + 1)
{
siteCollection.Close();
continue;
}
string siteCollectionRoot =
siteCollection.ServerRelativeUrl.Substring(1,siteCollectionRootLength);
if (siteCollectionRoot != _SiteCollectionRoot)
{
siteCollection.Close();
continue;
}
//--------------------------------------------------------------------------
------
// For each site in the site collection
//--------------------------------------------------------------------------
------
SPWebCollection webs = siteCollection.AllWebs;
try
{
//--------------------------------------------------------------------------
------
// Add the site to the XML document
//--------------------------------------------------------------------------
------
foreach(SPWeb web in webs)
{
if (web.IsRootWeb)
{
ListSubWeb(web, _xmlDoc.DocumentElement);
}
web.Close();
}
}
catch
{
if (_ShowInvalidSites)
htmlOutput.Append(String.Format("<BR><a href='{0}'><b>{1}</a> is an invalid
site</b><BR><BR>", siteCollection.Url, siteCollection.Url ));
}
finally
{
siteCollection.Close();
}
}
//--------------------------------------------------------------------------
------
// Sort the sites in the XML document by alphabetical order
//--------------------------------------------------------------------------
------
StringWriter sw = new StringWriter();
try
{
XslTransform xslt = new XslTransform();
xslt.Load(ConfigurationSettings.AppSettings["SiteDirectory XSLT URL"] +
"SiteDirectory.xsl");
xslt.Transform(_xmlDoc, null, sw, null);
}
catch (Exception ex)
{
ExceptionManager.Publish(ex);
htmlOutput.Append(ex.Message);
}
//--------------------------------------------------------------------------
------
// Put the sites in a tree view
//--------------------------------------------------------------------------
------
obout_ASPTreeView_2_NET.Tree tree = BuildTreeView(sw.ToString());
tree.FolderStyle = "/TreeIcons/Styles/" + _folderStyle;
tree.ShowIcons = false;
if (_WebCount > 0)
tree.AddRootNode("Sites", "stsicon.gif");
htmlOutput.Append("<table border=0 width=100%>");
htmlOutput.Append("<TR><TD>");
htmlOutput.Append(tree.HTML());
htmlOutput.Append("</TD></TR></table>");
}
catch (Exception ex)
{
ExceptionManager.Publish(ex);
htmlOutput.Append(base.GetGenericErrorMessage());
}
finally
{
//--------------------------------------------------------------------------
------
// Undo impersonation
//--------------------------------------------------------------------------
------
_wic.Undo();
}
return htmlOutput.ToString();
}
//==========================================================================
====
// Create an XML document with all the Sites
// <Sites>
// <Site userIsMember="True/False">
// <name>site name</name>
// <href>site address</href>
// </Site>
// .
// .
// </Sites>
//==========================================================================
====
private void ListSubWeb(SPWeb web, XmlElement xmlElement)
{
XmlElement siteElement = _xmlDoc.CreateElement("Site");
XmlElement nameElement = _xmlDoc.CreateElement("name");
nameElement.InnerText = web.Title;
siteElement.AppendChild(nameElement);
XmlElement hrefElement = _xmlDoc.CreateElement("href");
hrefElement.InnerText = web.Url;
siteElement.AppendChild(hrefElement);
siteElement.SetAttribute("userIsMember", UserIsMember(_CurrentUserLoginName,
web).ToString());
xmlElement.AppendChild(siteElement);
SPWebCollection webs = web.Webs;
foreach(SPWeb subWeb in webs)
{
ListSubWeb(subWeb, siteElement);
}
web.Close();
}
//==========================================================================
====
// Check to see if the current user is member of the site
//==========================================================================
====
private bool UserIsMember(string LoginName, SPWeb web)
{
bool isMember = false;
SPUserCollection users = web.Users;
foreach (SPUser user in users)
{
if (user.IsDomainGroup == true)
{
if (_userPrincipal.IsInRole(user.Name))
{
isMember = true;
break;
}
}
if (LoginName == user.LoginName)
{
isMember = true;
break;
}
}
return isMember;
}
"Peter Bons" <pe...@giessen.nl> wrote in message
news:1120647893.3...@g43g2000cwa.googlegroups.com...
thanks for your help