1

How can i get the current user that loaded a file from a sharepoint library?

When I load a file from a doc library on sharepoint, Word prompts me a window to type my UserName and Password to connect to the sharepoint site.

How can I get this username on the add-in or on the server side?

I've beem trying to use on the server side using:

SPUser user = SPContext.Current.Web.CurrentUser;

I tought that was going to solve my problems, but it seems to get always the same user, it doesn't matter which one I log-in on the MSWord prompt or on the Sharepoint Site for some reason.

Does anyone know what might be the solution?

4
  • SPContext will work only if the code is executed in SharePoint server. In case of office its a client application and used SharePoint web service to communicate. Commented Nov 3, 2014 at 18:27
  • Yes, I'm using a webmethod to access the server, and the server is using SPContext..
    – smith
    Commented Nov 3, 2014 at 18:33
  • Are you suggesting, you created a webservice that is hosted on SharePoint? And you are trying to access that web method from Office? Commented Nov 3, 2014 at 18:34
  • I can access any of the webMethods I did, I just cannot get the user
    – smith
    Commented Nov 3, 2014 at 18:36

1 Answer 1

0

I don't think you will be able to get the username and password (or the token) that the user has provided to Word or any office application.

This will be a risk if such a way exist in the first place.

For the current user (the user who logged into the system) you can invoke the webservice and pass the network credentials as

Uri uri = new Uri("http://tempuri.org/");
ICredentials credentials = CredentialCache.DefaultCredentials;
NetworkCredential credential = credentials.GetCredential(uri, "Basic");

Another option is to create your own form and request the user for credentials so You can cache it somewhere and use it for communication with SharePoint

2
  • This won't work for me, because I need the user who opened the file from sharepoint on the first place, not the user on the system.
    – smith
    Commented Nov 3, 2014 at 18:59
  • @smith then your only option is to create a custom login form and get it from the user. Commented Nov 3, 2014 at 19:00

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.