Other Posts in SharePoint

  1. Creating a SharePoint Site Programmatically
  2. Fix Access Denied Errors in SharePoint Search
  3. Web Page to Get Basic Information from SharePoint

Web Page to Get Basic Information from SharePoint

7/3/2011

As of late I've been working mostly on a SharePoint install at work. We use it for our extranet already and we're just planning on switching our intranet over to it. One of the biggest issues that we've run into with the extranet is simply figuring out what the heck we had out there. I had access to log into the server along with a couple of other admins but when our help desk would get a call, they'd be out of luck. So I made a small app to give them basic information about any site collection that someone might call about. Since it's generic enough, I figured I might as well share it with the world:

   1:  
   2: <html xmlns="http://www.w3.org/1999/xhtml">
   3: <head runat="server">
   4:     <title>SharePoint Manager Overview</title>
   5: </head>
   6: <body>
   7:     <form id="form1" runat="server">
   8:         <table>
   9:             <thead>
  10:                 <tr>
  11:                     <th>Site Collections</th>
  12:                     <th>Web Sites</th>
  13:                 </tr>
  14:             </thead>
  15:             <tbody>
  16:                 <tr>
  17:                     <td>
  18:                         <asp:DropDownList ID="Sites" runat="server" 
  19:                             onselectedindexchanged="Sites_SelectedIndexChanged" autopostback="true">
  20:                         </asp:DropDownList>
  21:                     </td>
  22:                     <td>
  23:                         <asp:DropDownList ID="SubSites" runat="server" 
  24:                             onselectedindexchanged="SubSites_SelectedIndexChanged" autopostback="true">
  25:                         </asp:DropDownList>
  26:                     </td>
  27:                 </tr>
  28:             </tbody>
  29:         </table>
  30:         <table>
  31:             <thead>
  32:                 <tr>
  33:                     <th>Basic Information</th>
  34:                 </tr>
  35:             </thead>
  36:             <tbody>
  37:                 <tr>
  38:                     <td>
  39:                         <div id="Test" runat="server">
  40:     
  41:                         </div>
  42:                     </td>
  43:                 </tr>
  44:             </tbody>
  45:         </table>
  46:         <table>
  47:             <thead>
  48:                 <tr>
  49:                     <th>Unused Sites</th>
  50:                 </tr>
  51:             </thead>
  52:             <tbody>
  53:                 <tr>
  54:                     <td>
  55:                         <div id="Unused" runat="server">
  56:     
  57:                         </div>
  58:                     </td>
  59:                 </tr>
  60:             </tbody>
  61:         </table>
  62:     
  63:     </form>
  64: </body>
  65: </html>

And along with that, we have a bit of code behind:

   1: using System;
   2: using System.Collections.Generic;
   3: using System.Linq;
   4: using System.Web;
   5: using System.Web.UI;
   6: using System.Web.UI.WebControls;
   7: using Microsoft.SharePoint;
   8: using Microsoft.SharePoint.Administration;
   9: using System.Data;
  10:  
  11: namespace SiteInfo
  12: {
  13:     public partial class _Default : System.Web.UI.Page
  14:     {
  15:         protected void Page_Load(object sender, EventArgs e)
  16:         {
  17:             if (!Page.IsPostBack)
  18:                 LoadLists();
  19:         }
  20:  
  21:         private void LoadLists()
  22:         {
  23:             try
  24:             {
  25:                 Sites.Items.Clear();
  26:                 Sites.Items.Add(new ListItem(" ", ""));
  27:                 using (SPSite WebSite = new SPSite("LOCAL SITE LOCATION"))
  28:                 {
  29:                     foreach (SPSite TempSite in WebSite.WebApplication.Sites)
  30:                     {
  31:                         Sites.Items.Add(new ListItem(TempSite.Url, TempSite.ID.ToString()));
  32:                         if (TempSite.LastContentModifiedDate.CompareTo(DateTime.Now.AddDays(-30)) < 0)
  33:                             Unused.InnerHtml += "<a href=\"" + TempSite.Url + "\">" + TempSite.Url + "</a><br />";
  34:                     }
  35:                 }
  36:             }
  37:             catch (Exception a) { Test.InnerHtml = a.ToString(); }
  38:         }
  39:  
  40:         protected void Sites_SelectedIndexChanged(object sender, EventArgs e)
  41:         {
  42:             try
  43:             {
  44:                 SubSites.Items.Clear();
  45:                 SubSites.Items.Add(new ListItem(" ", ""));
  46:                 if (string.IsNullOrEmpty(Sites.SelectedValue))
  47:                     return;
  48:                 using (SPSite TempSite = new SPSite(Guid.Parse(Sites.SelectedValue)))
  49:                 {
  50:                     foreach (SPWeb Web in TempSite.AllWebs)
  51:                     {
  52:                         if (Web != null)
  53:                         {
  54:                             SubSites.Items.Add(new ListItem(Web.Title, Web.ID.ToString()));
  55:                         }
  56:                     }
  57:                 }
  58:             }
  59:             catch (Exception a) { Test.InnerHtml = a.ToString(); }
  60:         }
  61:  
  62:         protected void SubSites_SelectedIndexChanged(object sender, EventArgs e)
  63:         {
  64:             try
  65:             {
  66:                 if (string.IsNullOrEmpty(Sites.SelectedValue))
  67:                     return;
  68:                 if (string.IsNullOrEmpty(SubSites.SelectedValue))
  69:                     return;
  70:                 Guid TempGuid = Guid.Parse(SubSites.SelectedValue);
  71:                 using (SPSite TempSite = new SPSite(Guid.Parse(Sites.SelectedValue)))
  72:                 {
  73:                     Test.InnerHtml = "<table>";
  74:                     Test.InnerHtml += "<tr><td>Site collection URL: </td><td><a href=\"" + TempSite.Url + "\">" + TempSite.Url + "</a></td></tr>";
  75:                     Test.InnerHtml += "<tr><td>Site collection owner: </td><td>" + TempSite.Owner.Name + "</td></tr>";
  76:                     Test.InnerHtml += "<tr><td>Site collection last modified: </td><td>" + TempSite.LastContentModifiedDate + "</td></tr>";
  77:                     Test.InnerHtml += "<tr><td>Site collection size (in MB): </td><td>" + (TempSite.Usage.Storage / 1048576).ToString() + "</td></tr>";
  78:                     using (SPWeb Web = TempSite.OpenWeb(TempGuid))
  79:                     {
  80:                         if (Web != null)
  81:                         {
  82:                             Test.InnerHtml += "<tr><td>Web site title: </td><td>" + Web.Title + "</td></tr>";
  83:                             Test.InnerHtml += "<tr><td>Web site URL: </td><td><a href=\"" + Web.Url + "\">" + Web.Url + "</a></td></tr>";
  84:                             Test.InnerHtml += "<tr><td>Web site description: </td><td>" + Web.Description + "</td></tr>";
  85:                             Test.InnerHtml += "<tr><td>Web site date created: </td><td>" + Web.Created + "</td></tr>";
  86:                             Test.InnerHtml += "<tr><td colspan=\"2\"><b>Users: </b></td>";
  87:                             foreach (SPUser User in Web.AllUsers)
  88:                             {
  89:                                 Test.InnerHtml += "<tr><td colspan=\"2\"><a href=\"mailto:" + User.Email + "\">" + User.Name + "</a></td></tr>";
  90:                             }
  91:                             Test.InnerHtml += "<tr><td colspan=\"2\"><b>Properties: </b></td>";
  92:                             foreach (object Key in Web.AllProperties.Keys)
  93:                             {
  94:                                 Test.InnerHtml += "<tr><td>" + Key.ToString() + "</td><td>" + Web.AllProperties[Key].ToString() + "</td></tr>";
  95:                             }
  96:                         }
  97:                     }
  98:                     Test.InnerHtml += "</table>";
  99:                 }
 100:             }
 101:             catch (Exception a) { Test.InnerHtml = a.ToString(); }
 102:         }
 103:     }
 104: }

And that was all there was to it. With a minimal amount of code I was able to give our help desk at least a decent amount of info to help them with any calls that they get. So try it out, leave feedback, and happy coding.



Comments