locked
How to get the Person object data from retrieve data? RRS feed

  • Question

  • Hi, I'm working with SPO. .net 2017 andC#.  I'm using the code snipet here https://dev.office.com/sharepoint/docs/sp-add-ins/complete-basic-operations-using-sharepoint-client-library-code  for "Retrieve items from a SharePoint list".  Actually, I'm retrieving these information from a Document Library which is a special type of list.

    I am able to add on additional field for "Created" which returns the document created date.  I also need to retrieve the "Created By" which in SP shows as a person or Group.  I want to retrieve the person's name and their email id.  How can I do that?  I tried casting to User but not work.

                foreach (ListItem listItem in items)
                {
                    if (listItem != null)
                    {
                        // We have all the list item data. For example, Title.
                        String temp = listItem["Title"].ToString();
                        DateTime createdDate = Convert.ToDateTime(listItem["Created"]);
                        User person = (User)(listItem["Created By"]);
                    }
                }

    Thank you.


    Thank you


    • Edited by laJasmine Thursday, May 18, 2017 5:45 PM
    Thursday, May 18, 2017 5:44 PM

Answers

All replies

  • Hi,

    We can use the following code snippet to get the "Created By" field data.

    FieldUserValue person = listItem["Author"] as FieldUserValue;
    var personName = person.LookupValue;
    var personEmail = person.Email;
    var personId = person.LookupId;

    Best Regards,

    Dennis


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Wednesday, May 24, 2017 11:11 AM
  • I'm using VS2017.  I use the code snippet but the intellisense only has the Lookup and LookUpValue but no Email as an available attribute.  I added the Microsoft.SharePint.Client reference already to the project and it seems to recognize
    FieldUserValue  with no problem.  How can I fix this?  Thank you.

    Thank you

    Wednesday, May 24, 2017 10:34 PM
  • Hi,

    For SharePoint Online, please download the SharePoint Online Client Components SDK below:

    https://www.microsoft.com/en-us/download/details.aspx?id=42038

    And then add new version dlls references to your project.

    More information:

    https://msdn.microsoft.com/en-us/library/microsoft.sharepoint.client.fielduservalue_members(v=office.15).aspx

    Best Regards,

    Dennis


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com


    • Edited by Dennis Guo Thursday, May 25, 2017 2:14 AM
    Thursday, May 25, 2017 1:42 AM
  • Hi Dennis,

    It's odd that class definition clearly shows the properties and methods that I can't access in VS2017.  I have reference to M  It does recognize the class  but why not all the attributes?  I have installed in Nuget the Microsoft.AharePoint.Client.Online.CSOM  Is this not the right package?  Should I first un-install it first?The link you provided for downloading the SDK doesn't support VS 2017.  Is there a different version?  Thank you.


    Thank you

    Thursday, May 25, 2017 6:05 PM
  • Hi,

    Please download the sharepointclientcomponents_16-4002-1211_x64-en-us.msi file from https://www.microsoft.com/en-us/download/details.aspx?id=42038

    and then install it, then add the Microsoft.SharePoint.Client.dll and Microsoft.SharePoint.Client.Runtime.dll to the References in your project.

    You can find the dll files in the location (C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI).

    Best Regards,

    Dennis


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Friday, May 26, 2017 1:30 AM
  • I assume I have to un-reference and un-install the Microsoft.AharePoint.Client.Online.CSOM from my VS project first, right?  Then download and installed the version you listed.


    Thank you

    Also, on the link you provided, it says the following quoted.  Isn't that the same as the CSOM that I instlled using NuGet?  Also, at the link you provided, it doesn't say it supports Windows 10 which is the OS i'm runing on and I'm using V.S. 2017.  Can you check again?  Is there a version for my environment?  Thank you.

    "The SharePoint Online Client Components SDK can be used to enable development with SharePoint Online. Notice that we do recommend using rather NuGet packages than installing CSOM assemblies to GAC. "

    • Edited by laJasmine Tuesday, May 30, 2017 4:42 PM
    Tuesday, May 30, 2017 4:38 PM
  • Hi,

    Nuget the Microsoft.AharePoint.Client.Online.CSOM  is not the right package.

    Please using this:

    Install-Package Microsoft.SharePointOnline.CSOM
    https://www.nuget.org/packages/Microsoft.SharePointOnline.CSOM

    Best Regards,

    Dennis


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    • Marked as answer by laJasmine Wednesday, May 31, 2017 7:08 PM
    Wednesday, May 31, 2017 2:19 AM
  • Hi Dennis,

    Yes, it's working now with the new CSOM I installed.  I am able to access the email attribute now.  Thank you very much. 

    Thank you.

    Thank you


    • Edited by laJasmine Wednesday, May 31, 2017 7:09 PM
    Wednesday, May 31, 2017 4:38 PM
  • Okay, that was an awkward site to download but I figure it out and it's installed.  However, I still don't get the email attribute after I installed the package using NuGet console. It automatically added the it to my Reference folder. 

    Visual Studio 2017 version and the CSOM installed information is as below. Do I have the correct version now?  Why I still can't see the email attribute of the person object?  Thank you.


    Thank you

    Wednesday, May 31, 2017 6:57 PM