I'm trying to debug the following code against a moss 2007 portal, using vs 2005. It's a web application with an .aspx page that contains a label, a text box and a button. The user writes a user account in the text box and on button click the groups for the user are returned. The error that it appears is
'An exception of type 'System.NullReferenceException' occurred in WebApplication2.DLL but was not handled in user code
Additional information: Object reference not set to an instance of an object'
highlighting the code line:
SPWebCollection allSites = mySite.AllWebs;
The whole code follows:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.Utilities;
namespace WebApplication2
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
string userList = TextBox1.Text + " is a user in the following webs:<br>";
SPSite mySite = SPControl.GetContextSite(Context);
SPWebCollection allSites = mySite.AllWebs;
foreach (SPWeb subSite in allSites)
{
string listGroups = "";
SPUserCollection allUsers = subSite.Users;
foreach (SPUser user in allUsers)
{
if (user.LoginName == TextBox1.Text)
{
SPRoleCollection allGroups = user.Roles;
foreach (SPRole group in allGroups)
{
listGroups += SPEncode.HtmlEncode(group.Name) + " ";
}
userList += subSite.ServerRelativeUrl.ToString() + " -- " + listGroups + "<BR>";
}
}
}
Label1.Text = userList;
}
}
}
Any suggestions would be more than welcomed...