You can follow the below URL to create and deploy a custom event handler
1. Create basic event handler
http://msdn.microsoft.com/en-us/library/ms437502.aspx
2. custom Event Handler Deployment using Feature..
You need two files.
-Feature.xml
-Elements.xml
http://msdn.microsoft.com/en-us/library/ms475328.aspx
Wednesday, April 22, 2009
adding an entry to user field
Those have spent hour to figure why the list does not store login name directly will know extra effort is required to get / set username from / to user field. In fact, it is stored in the following format:
ID ;# account name
In this case, you need the following code snippet to insert very into user field with the help of SPFieldUserValue
using (SPSite site = new SPSite("http://portal"))
{
using (SPWeb web = site.OpenWeb())
{
SPList list = web.Lists["Example For Users"];
SPListItem item = list.Items[0];
SPFieldUserValue userValue = new SPFieldUserValue(web, web.CurrentUser.ID, web.CurrentUser.LoginName);
item["User Name"] = userValue;
item.Update();
}
}
ID ;# account name
In this case, you need the following code snippet to insert very into user field with the help of SPFieldUserValue
using (SPSite site = new SPSite("http://portal"))
{
using (SPWeb web = site.OpenWeb())
{
SPList list = web.Lists["Example For Users"];
SPListItem item = list.Items[0];
SPFieldUserValue userValue = new SPFieldUserValue(web, web.CurrentUser.ID, web.CurrentUser.LoginName);
item["User Name"] = userValue;
item.Update();
}
}
Get a user from a list item
Very often we need to obtain username or group name from the list which has column type set to "person and group". The below code snippet might be handy
using (SPSite site = new SPSite(http://mysite))
{
using (SPWeb web = site.OpenWeb())
{
SPList list = web.Lists["myList"];
SPListItem item = list.Items[0];
SPFieldUserValue userValue = new SPFieldUserValue(web, item["User Name"].ToString());
if (userValue.User.LoginName == web.CurrentUser.LoginName)
{
//current user found in the list
}
}
}
using (SPSite site = new SPSite(http://mysite))
{
using (SPWeb web = site.OpenWeb())
{
SPList list = web.Lists["myList"];
SPListItem item = list.Items[0];
SPFieldUserValue userValue = new SPFieldUserValue(web, item["User Name"].ToString());
if (userValue.User.LoginName == web.CurrentUser.LoginName)
{
//current user found in the list
}
}
}
Subscribe to:
Posts (Atom)