Friday, August 20, 2010

Enabling Forms Authentication in Moss (SharePoint)

Steps to get Forms Authentication working with the ASP.NET SQL provider:

(1)   Install the Application Services Database for SQL Server 
     Either of the following two methods can be used:

      (a) Select Start > All Programs > Microsoft Visual Studio 2008 > Visual Studio Tools > Visual Studio 2008  
           Command Prompt.\
                 enter: aspnet_regsql.exe -E -A all -S localhost\DatabaseServerName

    
(b) You can run the aspnet_regsql utility from c:\windows\microsoft.net\framework\v2.0.50727 to  
create Roles and Membership data on the SQL Server.

          It will create a default database named “aspnetdb”.

(2)  Add the First User
     The database has been created. However, it does not contain any users and therefore we won't be able to  
      log in. We could write a script, or we could access the ASP.NET Web Site Administration Tool to add users

      Option 1: Adding Users by Script
     Simply run the following query in Microsoft SQL Management Studio against the aspnetdb database.

declare @currentTime datetime
set @currentTime = GETDATE()
exec aspnet_Membership_CreateUser 'MossAppName', 'userid','password','','email@domain.com','','',1,@currentTime,@currentTime,0,0,null

Option 2: Adding Users by Creating a Web Application
a. Create a new web application.
b. Add the connection string between the <configuration> tags of the web.config file.

<connectionStrings>
<add name="LocalSqlServer" connectionString="data source=localhost;DATABASE=aspnetdb;TRUSTED_CONNECTION=true;" providerName="System.Data.SqlClient"/>
      </connectionStrings>

             c. From Visual Studio, select Website > ASP.NET Web Configuration.
          d. Click Security > Create User. Add the name of the user as “NewAdmin”

(3)  Modify the moss web.config file
     a. Open the web.config file located in the C:\Inetpub\wwwroot\wss\VirtualDirectories\portnumber folder. 
      b Add the connection string between the <configuration> tags of the web.config file.

<connectionStrings>
<add name="LocalSqlServer" connectionString="data source=localhost;DATABASE=aspnetdb;TRUSTED_CONNECTION=true;" providerName="System.Data.SqlClient"/>
      </connectionStrings>

     Add the Membership and Role Provider details between the <system.web> tag

<membership defaultProvider="AspNetSqlMembershipProvider">
      </membership>
<roleManager enabled="true"   defaultProvider="AspNetSqlRoleProvider">
      </roleManager>

(4)   Modify the Central Admin web.config file 
Add connection and membership provider information to SharePoint Central Administration v3 web.config. We'll need the central administration site to work with forms authentication so that we can add the first administrator of the site. It is important to perform this step, otherwise you won't be able to add the first forms-based administrator later. 

(5)  Configure the SharePoint site for forms authentication
     a. Open SharePoint Central Administration site.
      b. Click Application Management.
      c. In the Application Security section, click Authentication providers. 
      d. Check that you are working with the web application that you wish to configure Forms   
          Authentication for.
      e. Set the the following fields:
      f.       Authentication Mode = Forms 
Membership provider name = AspNetSqlMembershipProvider.
      g. Click Save

(6) Assign the user to be a site collection administrator  
       a.  In SharePoint Central Administration Application Management, click Site Collection Administrators (you  
           can find it in the SharePoint Site Management section).
       b.  Add the user as site collection administrator of the website in which forms authentication will be applied. 

Important Point: One problem that I've faced was mismatch of the Application Name field. It's got to be the same as the one used within the SQL script and in the web.config file.



Custom Pagination in ASP.Net GridView Control

In this Blog I am going to cover the implementation of custom pagination for an ASP.Net Grid View control. It typically explains showing the custom Pager template for a Grid View control. It is based on my  experience and accumulated knowledge from various technical resources on the internet.



Introduction:
ASP.Net Grid View control has built-in feature of pagination which allows the developer to limit the number of rows of data. It is handled by pager template of GridView. The pager template can be shown either at top or bottom of the GridView. Pager template has the following modes of pagination NextPrevious, NextPreviousFirstLast, Numeric and NumericFirstLast.
In this blog, I am going to discuss to create a custom pager template where you can display any type of information as shown below.

Custom Pagination Template






Background:
RowCreated event of GridView is fired when a row is getting created after providing the data source to the GridView control. GridView control has a property to check the type of the current row being created. The basic idea is to check the type of the current row and then programmatically modifying the pager template.

It is a reusable component and can be used across application with minor code change.

I will explain the custom paging with the help of an example. In this example, I am going to add an HTML table to the pager template at runtime with two buttons Next and Prev and showing other information relevant to the current page of the grid.

Example:
Write the following code in RowCreated event of GridView. This event will be fired when each and every row of the GridView is getting created.
I have used some variables to store page specific information to be displayed in the pager template, see the inline comments for details about these variables.

After creating the desired template to be shown in the Pager of GridView, we need to add this HTML table to the GridView’s Pager row. This can be done using the LiteralControl which takes string, in our case HTML table, as a parameter and then it can be added to controls of GridView’s pager row.


Code Snippet:

aspx.cs file code:




//variable to store the total items in the grid view
private int RecordsCount; 

//Row_Created event to add custom pagination logic
protected void grdJobList_RowCreated(object sender, GridViewRowEventArgs e)
{
int iPageSize = grdJobList.PageSize;      // page size of the grid view
int iPageIndex = grdJobList.PageIndex;    // index of current page
int iPageCount = grdJobList.PageCount;    // total no of pages in the grid view
int iRowsInPage = grdJobList.Rows.Count;  // total no of rows for a particular page

RecordsCount = n; // where n is the total no of records in the grid view, get it from
                  //the row count property of the data source binded to grid view


//check if the current row is the pager row
if (e.Row.RowType == DataControlRowType.Pager)
{
//html for NEXT button
string strNext = "&nbsp&nbsp<input type=\"Button\" value=\"Next\" style=\"background-color:Gold; font-  
family:Arial;width:110px;height:30px;font-weight:bold;font-size:Large\" onclick=\"
javascript:__doPostBack('ctl00$cphMain$grdJobList','Page$Next'); \" />\n";

//html for Previous button
string strPrev = "<input type=\"Button\" value=\"Prev\" style=\"background-color:Gold; font-
family:Arial;width:110px;height:30px;font-weight:bold;font-size:Large\" onclick=\"
javascript:__doPostBack('ctl00$cphMain$grdJobList','Page$Prev');\" />\n";

//if it is the first page
if (iPageIndex == 0)
{
//customized pager template in a table format
string sTemplate = null;
sTemplate += "<table style=\"width:100%\">\n";
sTemplate += "<tr>\n";
sTemplate += "<td style=\"width:33%\" align=\"left\">\n";
sTemplate += string.Format("{0} {1} - {2} of {3} ", strGridContents,  
(iPageIndex * iPageSize) + 1, iPageSize + (iPageSize * iPageIndex), RecordsCount);
sTemplate += "</td>\n\n";
sTemplate += "<td style=\"width:34%\" align=\"center\">\n";
sTemplate += strNext;
sTemplate += "</td>\n";
sTemplate += "<td style=\"width:33%\" align=\"right\">\n";
sTemplate += string.Format("Page {0} of {1}", iPageIndex + 1, iPageCount);
sTemplate += "</td>\n</tr>\n</table>\n";

//Adding new pager template to the grid
e.Row.Cells[0].Controls.Add(new LiteralControl(sTemplate));
}
//if its not the first and the last page
if (iPageIndex != 0 && iPageIndex != iPageCount - 1)
{
//customized pager template in a table format
string sTemplate = null;
sTemplate += "<table style=\"width:100%\">\n";
sTemplate += "<tr>\n";
sTemplate += "<td style=\"width:25%\" align=\"left\">\n";
sTemplate += string.Format("{0} {1} - {2} of {3} ", strGridContents,
(iPageIndex * iPageSize) + 1, iPageSize + (iPageSize * iPageIndex), RecordsCount);
sTemplate += "</td>\n";
sTemplate += "<td style=\"width:25%\" align=\"right\">\n";
sTemplate += strPrev;
sTemplate += "</td >\n\n";
sTemplate += "<td style=\"width:25%\" align=\"left\">\n";
sTemplate += strNext;
sTemplate"</td>\n";
sTemplate += "<td style=\"width:25%\" align=\"right\">\n";
sTemplate += string.Format("Page {0} of {1}", iPageIndex + 1, iPageCount);
sTemplate += "</td>\n</tr>\n</table>\n";

//Adding new pager template to the grid
e.Row.Cells[0].Controls.Add(new LiteralControl(sTemplate));
}

//if it is the last page
if (iPageIndex == iPageCount - 1)
{
//customized pager template in a table format
string sTemplate = null;
sTemplate += "<table style=\"width:100%\">\n";
sTemplate += "<tr>\n";
sTemplate += "<td style=\"width:33%\" align=\"left\">\n";
sTemplate += string.Format("{0} {1} - {2} of {3} ", strGridContents,
(iPageIndex * iPageSize) + 1, iRowsInPage + (iPageSize * iPageIndex), RecordsCount);
sTemplate += "</td>\n";
sTemplate += "<td style=\"width:34%\" align=\"center\">\n";
sTemplate += strPrev;
sTemplate += "</td >\n\n";
sTemplate += "<td style=\"width:33%\" align=\"right\">\n";
sTemplate += string.Format("Page {0} of {1}", iPageIndex + 1, iPageCount);
sTemplate += "</td>\n</tr>\n</table>\n";

//Adding new pager template to the grid
e.Row.Cells[0].Controls.Add(new LiteralControl(sTemplate));
}
}
}





aspx file code:




<asp:GridView id="grdJobList" runat="server" Font-Bold="True"
BorderWidth="1px" BorderStyle="None" BorderColor="#DEDFDE"
ForeColor="Black" CellPadding="4" AllowSorting="True"
AutoGenerateColumns="False" DataSourceID="objJobListMethod"

PageSize="9" AllowPaging="True" onrowcreated="grdJobList_RowCreated">
<PagerSettings Mode="NextPrevious" FirstPageText="" LastPageText=""
NextPageText="" PreviousPageText="" />  
<PagerStyle ForeColor="Black" HorizontalAlign="Center" Height="20px">                       
</PagerStyle>




            
To enable the pagination we need to set the PageSize and AllowPaging properties of GridView control. PagerSettings tag is used to provide the mode and other details of pager template. PagerStyle tag is used for providing look and feel as per the user’s requirement.