SharePoint: Getting SPFIeldUserValue from SPUser and vice versa


 SPFieldUserValueCollection AssignedTo = (SPFieldUserValueCollection)item["VB"];
 SPUser vB = AssignedTo[0].User;

Here is a simple trick to get a SPFieldUserValue from SPUser. For example, here I was working with a event receiver

SPFieldUserValue strAttendee = new SPFieldUserValue(properties.Web, "-1;#" + properties.Web.CurrentUser.LoginName.ToString());

This trick come in very handy as SPFieldUserValue has a specific format that required a ID as well. Here instead of putting ID, we put -1 and let the system search the right ID for the User.


Update 28/05/2014:
Recently I was working on an event reciever on SharePoint 2013. I tried accessing  the value of a people picker field from AfterProperties inside item updating and item adding event which actually did not work since the claim based enocoding is somehow different from the one in SP2010. Following code snippet did the trick. Do this and this for further information.

public static SPUser GetUserObject(string peoplePickerValue, SPWeb oWeb)
        {
            string strUser = string.Empty;
            //strUser = (string)properties.AfterProperties["ParticipantsPicker"];
            SPFieldUser flduser = (SPFieldUser)oWeb.Lists["Teamkalender"].Fields.GetField("ParticipantsPicker");
            SPFieldUserValue strAttendee = (SPFieldUserValue)flduser.GetFieldValue(peoplePickerValue);
            SPUser user = oWeb.EnsureUser(strAttendee.LookupValue);
            return user;
        }


Vaqar Hyder

E: vaqar.hyder3567@gmail.com

A:

Siebenbürgen Straße 2/1, Korntal-Münchingen, 70825, PK

Hire me on Freelancer.com

Comments

Popular posts from this blog

SPFx: Develop using SharePoint Framework without Installing all the dependecies.

SharePoint Online: Elevated Permissions....with love

Powershell: Filling up an Existing Excel Sheet with data from SQL Server