Sunday, June 14, 2009

My Sites webpart to display what all site collection you have acces to

Scenario:
Situation is very simple, Assuming there are 100s of site collections in the organization and logged in user should be able to be able to see what all site he have access to.

Solution:
Simple webpart


Code:

using System.Data;

using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint.WebControls;

namespace SKN
{
public class MySites : System.Web.UI.WebControls.WebParts.WebPart
{
protected SPGridView _griDView;

protected override void CreateChildControls()
{
_griDView = new SPGridView {AutoGenerateColumns = false};

var hlf = new HyperLinkField
{
HeaderText = "Title",
DataNavigateUrlFields = new[] {"Url"},
DataNavigateUrlFormatString = "{0}",
DataTextField = "Title"
};

_griDView.Columns.Add(hlf);

DataTable table = CreateTable();

base.CreateChildControls();

SPSecurity.RunWithElevatedPrivileges(delegate
{

SPSite site = SPContext.Current.Site;
SPWebApplication webApplication = site.WebApplication;

foreach (SPSite siteCollection in webApplication.Sites)
{
using (SPWeb website = siteCollection.RootWeb)
{
if (website.DoesUserHavePermissions(SPBasePermissions.ViewListItems))
{
var row = table.NewRow();
row["Title"] = website.Title;
row["Url"] = siteCollection.Url;

table.Rows.Add(row);
}
}
siteCollection.Dispose();
}
}
);

Controls.Add(_griDView);

_griDView.DataSource = table.DefaultView;
_griDView.DataBind();
}

private static DataTable CreateTable()
{
var table = new DataTable("MySites");
// Declare variables for DataColumn and DataRow objects.

// Create second column.
var column1 = new DataColumn
{
DataType = System.Type.GetType("System.String"),
ColumnName = "Title",
AutoIncrement = false,
Caption = "Title",
ReadOnly = false,
Unique = false
};
// Add the column to the table.
table.Columns.Add(column1);

// Create new DataColumn, set DataType,
// ColumnName and add to DataTable.
var column2 = new DataColumn
{
DataType = System.Type.GetType("System.String"),
ColumnName = "Url",
AutoIncrement = false,
Caption = "Url",
ReadOnly = false,
Unique = false
};
// Add the column to the table.
table.Columns.Add(column2);

return table;
}
}
}
Possible improvements:
-- Please not in case you have multiple web applications then this solution will not work. You need to iterate through other Web Applications also and make sure you have proper account which can iterate all web applications.

-- If you have really hundreds of site collections. Then consider caching, that was one reason I used DataTable. ( Srini you can do it ;-) )

-- Also Url is not clickable , Pass me the code when you finish doing it for your client because I am tired :-) <-- Implemented

Follow me on Twitter

4 comments:

Anonymous,  January 23, 2013 at 2:51 AM  

How can this code be installed?

Anonymous,  January 28, 2013 at 4:29 AM  

Is there a ready to install webpart for SP foundation 2010 around as I do not have visual studio?

Sandeep January 28, 2013 at 5:25 AM  

I will see if i can help you over weekend.. tx