Showing posts with label Tips. Show all posts
Showing posts with label Tips. Show all posts

Wednesday, April 13, 2011

serializing + deserializing your object

Excellent article on this. How to serialize your object to xml and get it back.

Thursday, March 24, 2011

REST WCF

Simple and direct approach

another one

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();
}
}

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
}
}
}