JG Vimalan's Blog

It is all about coding!

Customize login in WSS 3.0/MOSS 2007 to redirect to a subsite site after login.

In this article we are going to see how to customize the login in WSS3.0/MOSS 2007 so that, when the user login in the parent site, they will be redirect to their respective sub site.

It is assumed that, you have forms authenication for your site and have multiple subsites.Earlier, I have noted the user login name and thier subsite urls in a database. So the concept is, a user can login only to his subsite. So, the basic idea is, a common url (parent url) provides a way to the users to login into their subsite.

See the image below for better understanding,

Hierarchy

Step1:  

Browse to the path: C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\12\TEMPLATE. Locate the login.aspx file and open it in a development environment. I used VS.NET 2005.  

Step 2:   Add <%@ ImportNamespace = “System.Data,SqlClient”%> in the login.aspx  (This requires only if you are going to connect to a SQL Server database)

Step 3:   In the login control, include the OnLoggedIn event as shown below,  

<asp:login id=”login” OnLoggedIn=OnLoggedIn FailureText=………./>

Step 4:   Add the <script> tag between the namespace registration and the Content place holder and put some code as shown below,    

<script runat=server>

protected override void OnLoad(EventArgs e) 

{       

base.OnLoad(e);   Page.Response.Write(this.Context.User.Identity.Name);  

}      

void OnLoggedIn(object sender, EventArgs e) 

{    

string connectionString = @”server=abc\SQLEXPRESS;database =         myDatabase;Integrated Security = SSPI”;     

using (SqlConnection conn = new SqlConnection(connectionString))  

{        

conn.Open();         

SqlCommand cmd = new SqlCommand();

cmd.CommandText = “someProcedure”; 

cmd.CommandType = System.Data.CommandType.StoredProcedure;         cmd.Connection = conn;

SqlParameter param = new SqlParameter(“@UserName”, System.Data.SqlDbType.NChar, 256);        

param.Value = login.UserName; //Name in the login control text box                 

cmd.Parameters.Add(param);

string userPortalName = Convert.ToString(cmd.ExecuteScalar());                 

conn.Close(); 

if (!string.IsNullOrEmpty(userPortalName)) {            

string URL = “http://site/&#8221; + userPortalName.Trim() + “/default.aspx”;              Response.Redirect(URL);        

}            

}  

</script>

<asp:content ControlPlaceHolder………………/>

Step 5:  Key in the user name (who has access to a particular subsite) and password and login. 

Login

Now, you will find that, the user is redirected to his subsite.   For example, if main url is, http://site/ then, he will be redirected to his subsite http://site/user10 . Now you have made a simple change in the login.aspx so that it behaves the way you like!!!

Advertisements

October 24, 2009 Posted by | SharePoint | 4 Comments

Allow only numbers in a ASP.NET textbox using JavaScript

Consider you have declared a textbox. Call the JavaScript function as shown below,

<asp:TextBox ID=”demoNumberTextBox” runat=”server” onkeypress=”return NumeralOnlyWithoutDot( event )”

JavaScript: here is the function which allows only numbers in a textbox,

function NumeralOnlyWithoutDot(evt) {
    evt1 = (evt) ? evt : event;
    var charCode = (evt.charCode) ? evt.charCode : ((evt.keyCode) ? evt.keyCode :
   ((evt.which) ? evt.which : 0));
    if ((charCode > 31 && (charCode < 48 || charCode > 57))) {       
        return false;
    }
    return true;
}

October 16, 2009 Posted by | JavaScript | Leave a comment

Enable/Disable ASP.NET validators using JavaScript

//This java script function disables a required field validator

//setting true, will enable and validate the value in the associated textbox.

 

function EnableValidations() { 

ValidatorEnable(TotalDonationTextBoxValidator, false);

}

October 16, 2009 Posted by | JavaScript | Leave a comment

InputFormTextBox control in Custom WebPart

The following code snippet will give you an idea of using InputFormTextBox in custom webparts in SharePoint.

private InputFormTextBox _DescriptionTxtBox = null;

protected override void CreateChildControls()
        {
            _DescriptionTxtBox = new InputFormTextBox();
            _DescriptionTxtBox.ID = “descriptionTxtBox”;
            _DescriptionTxtBox.TextMode = TextBoxMode.MultiLine;
            _DescriptionTxtBox.RichText = true;
            _DescriptionTxtBox.RichTextMode = SPRichTextMode.Compatible;
            _DescriptionTxtBox.Rows = 10;

            this.Controls.Add(_DescriptionTxtBox);
        }

//Add: Render the ‘_DescriptionTxtBox’

The output will be,

inputForm

October 10, 2009 Posted by | SharePoint | 1 Comment

SPException – One or more field types are not installed properly. Go to the…

Sharepoint developers often face the following exception while querying a custom list or document library using SPQuery.

spex1

for example, I’ve got this exception in the following code snippet,

string lname = memberLeaveAccount[“LoginName”].ToString();

if (string.IsNullOrEmpty(lname))
    return;

int chrIndex = lname.IndexOf(“#”);

if (chrIndex >= 0)
   lname = lname.Substring(chrIndex + 1);

SPQuery spQuery = new SPQuery();
spQuery.Query = “<Where><Eq><FieldRef Name=’Account‘></FieldRef> <Value Type=’Text’>” + lname + “</Value></Eq></Where>”;
SPListItemCollection EmployeeRecord = MyCustomList.GetItems(spQuery);

string gender = string.Empty;

//If an employee record is removed from custom list, just move to next record.
if (EmployeeRecord == null)
   continue;

//Strong check
if (EmployeeRecord != null && EmployeeRecord.Count == 0)
   continue;

I’ve got the exception at ‘EmployeeRecord.Count’. I’ve found that, the field name ‘Account’ is only the Display Name and the internal name is “AccountName”. So, after giving the correct internal name, the SPQuery worked fine.

NOTE: To get the fields (in custom list or document library) Internal Name, 

foreach(SPField field in MyCustomList.Fields)

           MessageBox.Show(field.InternalName);

October 10, 2009 Posted by | SharePoint | Leave a comment

Trim the trailing spaces in a textbox using C#.NET

The following code  gives an idea on how to trim the trailing spaces in a textobox when space bar (empty space) is pressed..

  /// <summary>
  /// Occurs after the user presses a key.  Trims the trailing spaces 
  /// </summary>
  /// <param name=”e”></param>
  protected override void OnKeyPress
   (System.Windows.Forms.KeyPressEventArgs e)
  {
    e.Handled = false;
    char ch = e.KeyChar;
    int Position = base.SelectionStart;
    if(ch == ‘ ‘)
    {
      Position = base.SelectionStart;
    if(Position == 0)
    {
       e.Handled = true;
       return;
    }
    
    int Length = base.Text.Length;    
    int EndPosition = Position + SelectionLength;
    if (EndPosition == Length)
    {
     if (base.Text[Position-1] == ‘ ‘)
     {
        e.Handled = true;
     }
     return;
    }
    
    else if ((base.Text[Position-1] == ‘ ‘) ||
     (base.Text[EndPosition] == ‘ ‘))
    {     
       e.Handled = true;
       return;
    }
   }   
  }

October 9, 2009 Posted by | C#.NET | Leave a comment