Sunday, 21 August 2011

Get a user from a list item:

Get a user from a list item:



To get a user we use the SPFieldUserValue class, which accepts a SPWeb and a string value as parameters. The string value is the value from the list that contains the ID and the account name. Once we have that, we can use the SPFieldUserValue to get information about the user.

Example:

 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, item["User Name"].ToString());
                    if (userValue.User.LoginName == web.CurrentUser.LoginName)
                    {
                        //do something!
                    }
                }
            }

No comments:

Post a Comment