简体   繁体   English

获取ID? 像SharePoint这样的当前用户可以吗?

[英]Get the ID? of the current user like SharePoint can?

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. 我在一个客户站点上,那里有一个始于SharePoint的应用程序,并且正在慢慢迁移到一个非常自定义的ASP.NET应用程序。 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). 他们的某些数据元素仍托管在SharePoint列表中,其中两个当前有问题的是一些“注释”和“任务”(它们在SharePoint设置中非常简单的数据元素,对此没有特殊要求)。 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. 我需要在ASP.NET中执行的操作之一是自动为这些列表创建一些新项,然后从代码中添加它们。

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. 我找到了一个现有的Web部件,用于处理这些项目的编辑,将调试器附加到该Web部件,跟踪它如何获取其值以及它添加到列表中的内容,等等。但是,添加到列表项目中的字段之一是“相当明显。 In the existing web part UI, the field looks like this: 在现有的Web部件UI中,该字段如下所示:

分配给字段

Essentially, it's a field for entering a user from the current Windows domain. 本质上,这是一个用于从当前Windows域输入用户的字段。 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. 因此,我只是在弹出窗口中查找“ admin”,然后按预期的那样用“ [计算机名称] \\ Administrator”填充了该字段。 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. 然后,在调试中,从字段中提取并输入到SharePoint列表项中的值是“ 1”,而不是名称或类似名称。

I assume that "1" is an identifier for the local admin account. 我假设“ 1”是本地管理员帐户的标识符。 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. 我找到了获取当前用户名的代码,但没有任何类型的数字(即使是字符串)ID。

Additionally, this wouldn't just be happening inside of an ASP.NET application context. 此外,这不仅会在ASP.NET应用程序上下文中发生。 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. 还有一个用于笔记本电脑的WPF客户端应用程序,它将生成这些列表项,并在连接时将它们同步回服务器。 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. 我想这很容易,只是我还没有偶然发现我所需要的东西。

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. 顺便说一句,这是SharePoint分配给用户的内部ID。 To get this Id from a WPF application, you could deploy a WebService inside SharePoint that would return this Id. 若要从WPF应用程序获取此ID,可以在SharePoint中部署WebService并返回此ID。 Or you can even query the SharePoint database, but I'm not sure if it's safe :-) 或者甚至可以查询SharePoint数据库,但是我不确定它是否安全:-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM