programmatically set the value of a Lookup column field or People Picker / Person & Group column field of a SharePoint List / Library
Problem
Sometimes we need to programmatically set the value of a Lookup column field or People Picker / Person & Group column field of a SharePoint List / Library. But this is a problem because these columns are initially set to be in a read-only mode by default.Solution
So when you need to change (or set) the value for any one of them, you need to follow the following solution.
Assume we have a SharePoint custom list called "Employee" which has the following columns or fields:
- "Title " => Single line of text data type default initial column.
- "Username" => People Picker (Person & Group) data type column.
- "Unit" => Lookup filed column (Single Line of text data type column from a "Department" custom list)



Let's open Visual Studio.NET 2005 /2008 or even 2010 IDE and create a new Windows application sample project.
(You can use the best practice snippet code in your web parts or other desired solutions) which will contain the initial wrong solution and the best practice snippet code for adding & setting "Username" column value with the current logged in authorized user and also "Unit" with another single text value :
1. File > New > Project > Visual C# (Language) : Windows : Windows Application.
2. Add a button in order to fire the setting event handler for "Username" & "Unit" : Open "Toolbox" > Drag and drop "Button" control to the form :

3. Add "Microsoft. SharePoint DLL" as a reference : Open "Solution Explorer" > Right click "References" > Add reference.
4. Browse (Tab) > Go to the ISAPI 12 hive directory destination "<Drive>:\Program Files\Common Files\Microsoft Shared\web server extensions\12\ISAPI" > Microsoft. SharePoint.dll >
Ok :

5. Right click on the empty white form > View Code :

6. Add / Register namespace reference "Using Microsoft. SharePoint" :

7. Double click on the "Fire" button to go to the code behind "Click" event handler.
8. Let's try the initial wrong solution snippet code and see the result then we'll see the best practice :
Then let's build our solution "CRTL + Shift + B" and wait until we get the "Build Successes" message - - then let's insert some breakpoints...
Go to the targeted line of code and click "F9" ... at certain lines of code, for example: "empItem.Update()" , then press "F5" for changing to the debugging mode and then click on the "Fire" button to see what are the results :

You'll get the following error message:
"Invalid data has been used to update the list item . The field you are trying to update may be read only".
That's what we are talking about. So we need to remove the read only setting from both field before update and then rollback.
And here's the result :
Assume we have a SharePoint custom list called "Employee" which has the following columns or fields:
- "Title " => Single line of text data type default initial column.
- "Username" => People Picker (Person & Group) data type column.
- "Unit" => Lookup filed column (Single Line of text data type column from a "Department" custom list)
Let's open Visual Studio.NET 2005 /2008 or even 2010 IDE and create a new Windows application sample project.
(You can use the best practice snippet code in your web parts or other desired solutions) which will contain the initial wrong solution and the best practice snippet code for adding & setting "Username" column value with the current logged in authorized user and also "Unit" with another single text value :
1. File > New > Project > Visual C# (Language) : Windows : Windows Application.
2. Add a button in order to fire the setting event handler for "Username" & "Unit" : Open "Toolbox" > Drag and drop "Button" control to the form :
3. Add "Microsoft. SharePoint DLL" as a reference : Open "Solution Explorer" > Right click "References" > Add reference.
4. Browse (Tab) > Go to the ISAPI 12 hive directory destination "<Drive>:\Program Files\Common Files\Microsoft Shared\web server extensions\12\ISAPI" > Microsoft. SharePoint.dll >
Ok :
5. Right click on the empty white form > View Code :
6. Add / Register namespace reference "Using Microsoft. SharePoint" :
7. Double click on the "Fire" button to go to the code behind "Click" event handler.
8. Let's try the initial wrong solution snippet code and see the result then we'll see the best practice :
Wrong solution snippet code
Then let's build our solution "CRTL + Shift + B" and wait until we get the "Build Successes" message - - then let's insert some breakpoints...
Go to the targeted line of code and click "F9" ... at certain lines of code, for example: "empItem.Update()" , then press "F5" for changing to the debugging mode and then click on the "Fire" button to see what are the results :
You'll get the following error message:
"Invalid data has been used to update the list item . The field you are trying to update may be read only".
That's what we are talking about. So we need to remove the read only setting from both field before update and then rollback.
Best practice snippet code
Now we'll follow this snippet code which is the best practice to add & set the value for both "Username" with the current logged in authorized user and "Unit" with another single text value:And here's the result :