1

I am currently using REST to query a discussion board list. The results I am then displaying nicely in a table using bootstrap.

We are using office 365. The REST query returns an authorID. How can we use csom to get the username from the authorID returned from the REST query.

3 Answers 3

2

In fact, there is no need to perform an additional request to retrieve User details by its Id. Since you are using SharePoint REST API, the following single query returns Discussion List data including Author details (Id, Title) using the projection for an Author column:

/_api/web/lists/getbytitle('Discussions List')/items?$select=Title,Author/ID,Author/Title&$expand=Author/ID,Author/Title

Please follow Getting User Information with the SharePoint 2013 REST API post for a more details.

1
  • Great, glad this helped you. Commented Jun 20, 2014 at 11:24
1

You can use CSOM but also the REST API to get all your information.

<app web url>/_api/web/GetUserBy(<userid>)

or

<app web url>/_api/SP.AppContextSite(@target)/web/getuserbyid(<userid>)?@target='<host web url>'

For more information see MSDN documentation

2
  • As I am retrieving items from a discussion board list the no of id's coming back is huge therefore .Remko I get your solution maybe efficient for a single userID but for many ids how can I optimise this. Aanchal we can't hardcode passwords in any solution just a huge no no. Commented Jun 20, 2014 at 9:46
  • You dont need to hardcode the passwords, you can make it configurable and pass to API. For using CSOM you need Credentials object. Commented Jun 20, 2014 at 10:31
0

You can try following code:

var targetSite = new Uri("site");
            var login = "username";
            var password = "password";
            var securePassword = new SecureString();
            foreach (char c in password)
            {
                securePassword.AppendChar(c);
            }

            var onlineCredentials = new SharePointOnlineCredentials(login, securePassword);

            using (ClientContext clientContext = new ClientContext(targetSite))
            {


                clientContext.Credentials = onlineCredentials;

                Web web = clientContext.Web;
    Microsoft.SharePoint.Client.ListItem userInfo =   web.SiteUserInfoList.GetItemById(lookupid);
    clientContext.Load(userInfo);
    clientContext.ExecuteQuery();
    Useremail = userInfo["EMail"].ToString();
    UserName = userInfo["Title"].ToString();
}
2
  • Hi @Aanchal, Is Hard Coding credentials in nice idea? May be we can use ClientAuthenticationMode.Default Commented Jun 20, 2014 at 9:34
  • For CSOM, You nees this creds. Commented Jun 20, 2014 at 9:38

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.