As we can see, the method returns an object of type GetWebCollection XmlNode containing a fragment of XML, from which we can take the name and address of any sub. Immediately evident that I am not having more to do with the SharePoint API, but with information in the format string, and very small compared with, for example, to what is offered by an object of type SPWeb . Precisely because of this, we lose a lot of useful features, most notably the ability to make us return only sites in which the current user has permissions, or the ability to see the kind of template for each sub-site or, perhaps, the its ID. To overcome these shortcomings, we must necessarily implement a custom web service, which exploits the most appropriate way the object model of SharePoint.
This method GetSubWebsForCurrentUser I thought to develop. The return type is always the same (an object of type XmlNode), and can be read with the same code seen before, the only difference, of course, is that Sharepoint sites are returned only when the user makes the request to the web service has rights.
Code:
[WebMethod]
public XmlNode GetSubWebsForCurrentUser (string siteurl)
{
XmlDocument doc = new XmlDocument ();
doc.AppendChild (doc.CreateElement ("Website"));
try
{
SPWeb web = new SPSite (siteurl). OpenWeb ();
foreach (SPWeb childWeb in web.GetSubwebsForCurrentUser ())
{
Doc.CreateElement XmlNode childNode = ("Web");
Doc.CreateAttribute XmlAttribute name = ("Name");
name.Value = childWeb.Title;
Url = doc.CreateAttribute XmlAttribute ("URL");
url.Value = childWeb.Url;
childNode.Attributes.Append (name);
childNode.Attributes.Append (url);
doc.DocumentElement.AppendChild (childNode);
}
}
catch (Exception exe)
{
throw new Exception (exe.Message, exe);
}
doc.DocumentElement return;
}
Here we have two ways to further expand the capabilities of our custom web service. The first returns the name of the template used to create the site as a parameter and the second will return the ID:
Code:
[WebMethod]
public string GetTemplateName (string siteurl)
{
SPWeb web = new SPSite (siteurl). OpenWeb ();
web.WebTemplate return;
}
[WebMethod]
public int GetTemplateID (string siteurl)
{
SPWeb web = new SPSite (siteurl). OpenWeb ();
web.WebTemplateId return;
}
Bookmarks