Wednesday, April 16, 2008

Reseting user personalization state

To manage personalization state of users, you must use the PersonalizationAdministration class. The following example shows how te reset state of the current user:


WebPartManager mgr = WebPartManager.GetCurrentWebPartManager(Page);
mgr.Personalization.ResetPersonalizationState();


To reset state of a particular user, use the following code snippet:


For managing user state, refer to http://msdn2.microsoft.com/en-us/library/ms366517.aspx


// Reset the user.
if (! PersonalizationAdministration.ResetUserState("~/Default.aspx", txtUser.Text))
{
Response.Write("The user could not be found or the user has not personalized this page");
}

Wednesday, April 9, 2008

IsInRole issue with FormsAuthentication

If rolemanager is enabled in the web.config, the GenericPrinciple is automatically cast to RolePrincipal dismissing all of the GenericPrincipal's previous set roles. To disable this automatic cast, disable rolemanager in the web.config.

<rolemanager enabled = "false">

Friday, April 4, 2008

Reading/Writing Excel in C# using Interop

Add references to :
Microsoft Excel 10.0/11 Object Library using "add reference->com" tab. The following code shows example of reading and writing the xls:


using System;
using System.Collections.Generic;
using System.Text;
using Excel;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string path = @"C:\Documents and Settings\shivps\Desktop\cbit_codef\CBIT_CODEF\New Microsoft Excel Worksheet.xls";
Excel.ApplicationClass app = new ApplicationClass();
app.ShowWindowsInTaskbar = false;

Excel.Workbook wb = app.Workbooks.Open(
path,
0,
false,
5,
"",
"",
true,
Excel.XlPlatform.xlWindows,
"\t",
true,
false,
0,
true,
1,
0);

Excel.Worksheet wsAuthor = null;
foreach (Excel.Worksheet ws in wb.Worksheets)
{
if (string.Compare("author", ws.Name, true) == 0)
{
wsAuthor = ws;
break;
}
}

// read all records
int row = 2; // leave first row
while (true)
{
if (((Excel.Range)wsAuthor.Cells[row, 1]).Value2 == null ||
((Excel.Range)wsAuthor.Cells[row, 2]).Value2 == null ||
((Excel.Range)wsAuthor.Cells[row, 3]).Value2 == null ||
((Excel.Range)wsAuthor.Cells[row, 4]).Value2 == null)
{
break;
}
string first = ((Excel.Range)wsAuthor.Cells[row, 1]).Value2.ToString();
string second = ((Excel.Range)wsAuthor.Cells[row, 2]).Value2.ToString();
string login = ((Excel.Range)wsAuthor.Cells[row, 3]).Value2.ToString();
string pass = ((Excel.Range)wsAuthor.Cells[row, 4]).Value2.ToString();

// create user
string status = "pass";
string error = "no error";

// update xls
wsAuthor.Cells[row, 5] = status;
wsAuthor.Cells[row, 6] = error;
row++;
}

wb.Save();
wb.Close(false, null, null);
app.Quit();
}

}
}

Wednesday, April 2, 2008

Setting Title of a dynamic created webpart

My portal adds and deleted webparts dynamically. I was facing difficulty in setting title of dynamically created webparts. Most of the methods somehow did not work. The following did work for me:


Control c = LoadControl(controlName);
c.ID =id;
wp = WebPartManager1.CreateWebPart(c);
this.WebPartManager1.AddWebPart(wp, wz, index);
wz.WebParts[index].Title = title;

Tuesday, April 1, 2008

Dynamic Webparts: missed events and twice load issue

While developing a portal which has dynamic webparts created, it was observed that control events were not firing. Also page_load function was called more than once. These issues got resolved on setting the personalization state dirty after adding or deleting a webpart. The personalization state can be set Dirty by calling WebPartManager SetPersonalizationDirty function. Since this method is protected, it can not be called directly. To call this function, a custom WebPartManager is required. The following explains it.

1) Create a custom WebPartManager which exposes only a single function SetDirty.



using System;
using System.Collections.Generic;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Security.Permissions;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

namespace Samples.AspNet.CS.Controls
{
public class NewWebPartManager : WebPartManager
{
public NewWebPartManager() { }

public void SetDirty()
{
this.SetPersonalizationDirty();
}
}

}


2) Use the custom webpart in the page as given below:


<%@ Register Namespace="Samples.AspNet.CS.Controls" TagPrefix="sample" %>
...
<sample:NewWebPartManager ID="WebPartManager1" runat="server" >
</sample:NewWebPartManager>