Getting Sharepoint User Job Title, Department

Hi,

I'm trying to retrieve sharepoint user's job title and department in my web application.

I understand to retrieve username, something like the below is done.

 SPWeb theSite = SPControl.GetContextWeb(Context);
        SPUser theUser = theSite.CurrentUser;
        string strUserName = theUser.LoginName;

Anyone knows how to retrieve job title and department?

Thanks!

June 17th, 2015 4:31am

You can try something like below.

using (SPSite Site = new SPSite(SPContext.Current.Web.URL))
{ 
  using (SPWeb Web = Site.OpenWeb()) 
  {
    SPList UserInfoList = Web.Lists[User Information List];
    SPUser User = SPContext.Current.Web.CurrentUser;
    SPListItem UserItem = UserInfoList.GetItemById(User.ID);
    // UserItem[Department] for department
  }
}
Thanks.


  • Marked as answer by Kai Arthur 6 hours 42 minutes ago
Free Windows Admin Tool Kit Click here and download it now
June 17th, 2015 3:23pm

Hi, thanks for your reply!

I tried your code, but it seems to give an error

"Compiler Error Message: CS0117: 'Microsoft.SharePoint.SPWeb' does not contain a definition for 'URL'"

Line 26:         using (SPSite Site = new SPSite(SPContext.Current.Web.URL))
Line 27:         {
Line 28:             using (SPWeb Web = Site.OpenWeb())

It works fine when I change SPContext.Current.Web.Url to my site URL, but is there a way to resolve this?

Thanks!

June 17th, 2015 9:19pm

Hey,

There is a case sensitive issue (URL ----> Url). Try something like below.

using (SPSite site = new SPSite(SPContext.Current.Web.Url))
{
    ...
}

Thanks.

  • Marked as answer by Kai Arthur 1 hour 38 minutes ago
Free Windows Admin Tool Kit Click here and download it now
June 18th, 2015 12:42am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics