Get the ID? of the current user like SharePoint can?

2.8k views Asked by At

I'm at a client site where they have an application which began in SharePoint and is slowly migrating away to a very custom ASP.NET application. Some of their data elements are still hosted within SharePoint lists, two of which currently in question are some "Notes" and "Tasks" (fairly simple data elements in their SharePoint setup, nothing special about them). One of the things I need to be able to do from within ASP.NET is to automatically create some new items for these lists and add them from code.

So far it's pretty easy. I found the existing web part which handled the editing for those items, attached the debugger to it, tracked how it got its values and what it added to the list, etc. However, one of the fields being added to the list item isn't quite as obvious. In the existing web part UI, the field looks like this:

Assigned To field

Essentially, it's a field for entering a user from the current Windows domain. The book icon opens a pop-up which allows the user to search for a name, etc. In my current testing, I'm running as local Administrator on a development machine. So I just look for "admin" in the pop-up and it populates the field with "[machine name]\Administrator" as expected. Then, in debugging, the value that gets pulled from the field and entered into the SharePoint list item is "1" as opposed to a name or anything like that.

I assume that "1" is an identifier for the local admin account. Makes sense, after all. But my question is, how can I get that identifier for the current logged-in user in code? I've found code to get the current user's name, but not any kind of numeric (even though it's a string) ID.

Additionally, this wouldn't just be happening inside of an ASP.NET application context. There's also a WPF client application for laptops which would be generating these list items and synchronizing them back to the server when connected. I'm currently operating on the assumption that the client user would be logged in with a proper domain account known to the server.

I imagine this is pretty easy, I just haven't stumbled across what I need quite yet.

1

There are 1 answers

5
goenning On BEST ANSWER

I suppose you're looking for this:

int userId = SPContext.Current.Web.CurrentUser.ID;

By the way, this is the internal Id assigned by SharePoint to the user. To get this Id from a WPF application, you could deploy a WebService inside SharePoint that would return this Id. Or you can even query the SharePoint database, but I'm not sure if it's safe :-)