1

I have the list of items in object SPListItem item. Is it possible to extract certain data from the ListItems property?

The reason I want to do that is because I have a FieldName "AssignedTo" which returns null, even though I can see its value when debugging when using the following statement:

SPField field = item.Fields.GetFieldByInternalName("AssignedTo");
if (field != null)
{

    SPFieldUserValue fieldValue = field.GetFieldValue(item["AssignedTo"].ToString()) as
                                                                                SPFieldUserValue;

    if (fieldValue != null)
    {
        assignToUser = fieldValue.User;
    }
}

Now, in the ListItems property I can go to ItemsData and I can see the value is there, but how can I get it?

Any help appreciated!

3
  • Do you want to get the value from "AssignedTo" column for specific list item?
    – Hardik
    Jun 20, 2016 at 13:19
  • As your question is somewhat confusing me. So kindly clear it so i can help you. :)
    – Hardik
    Jun 20, 2016 at 13:19
  • @Hardik Yes, that's correct. I'm interested in the value of AssignedTo, but for some unknown reason, it returns null and I can't for the love of me figure out why as I can see the value in 'ItemsData' when debugging. Please ask for more clarification if needed!
    – Khaine775
    Jun 20, 2016 at 13:21

4 Answers 4

3

This code should work: (web is a variable that references the web object)

 SPFieldUserValue value = 
                new SPFieldUserValue(web, item["AssignedTo"].ToString());
 if (value)
    {
        assignToUser = value.User;
    }
2

You can try below code,

using (SPSite siteCol = new SPSite("http://server/sites/Contoso"))
    {
        using (SPWeb web = siteCol.RootWeb)
        {
            SPList list = web.GetList("/sites/Contoso/Lists/Books");

            SPListItem item = list.GetItemByIdSelectedFields(3, "Title", "ISBN", "Retail_x0020_Price");

            String bookISBN = (String)item["ISBN"];
        }
    }
2

Try this

SPFieldUserValue userField = item.Fields["AssignedTo"] as SPFieldUser;
string userName = item["AssignedTo"].ToString();

SPFieldUserValue fieldValue = (SPFieldUserValue)userField.GetFieldValue(userName);
SPUser user = fieldValue.User;

Taken from http://ethan-deng.blogspot.com/2013/03/get-spuser-from-user-column-there-are.html

2

You can try below code to get SPUser in AssignedTo columns for specific item.

If(task["Assigned To"] != null)
{
    string fieldValue = Convert.ToString(task["Assigned To"]);
    SPFieldUserValue assignedTo = (SPFieldUserValue)
    task.Fields["Assigned To"].GetFieldValue(fieldValue);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.