none
C# csom get logged in user on load RRS feed

  • Question

  • Hi  Having an issue with getting logged in user on load.  InfoPath2013 with c# csom behind on SP 2013.  The code will not run (fails with 401 on the first executequery() ) unless we first log in with an account that has higher permissions.  When we log in with that higher level account it seems to overwrite the actual user value.  Must be missing something here.

    Appreciate any help with this .

    Thank you

    Chuck


    Thursday, March 23, 2017 3:42 PM

All replies

  • Hi,

    Try to build the form(user name and password) and set the credential based on user input.

    For example:

    using (var context = new ClientContext("http://sp:12001"))

                {

                    Console.ForegroundColor = ConsoleColor.Green;

                    //you could get the username and password based on user input

                    context.Credentials = new NetworkCredential("contoso\\usera", "a");

    Here is one link for your reference.

    http://stackoverflow.com/questions/24929259/authenticate-network-credential-to-access-sharepoint-site-on-client-object-model

    Best Regards,

    Lee


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Friday, March 24, 2017 2:25 AM
  • Thank you Lee.  We do have that working.  The issue is as follows:

    The code behind that is attached to the InfoPath form is saved as a content type to SharePoint 2013

    So when the user with CONTRIBUTE permission clicks to load the form from a from library the user permissions are not high enough (I suspect) for the code to run.  However, if we use the code you mention with the same user credentials and login in, in code, it works.  However we do not know what user will come in to access the content type ahead of time.  if we use a higher level account the code runs but as soon as we use:

    context.Credentials = new NetWorkCredential("user", "pwd", "domain"); with a higher level account the actual user who came in is over written.  So if jdoe was the user and the code runs on form load and we log in with user the web.current.user now returns user and not jdoe.

    Does this make sense?

    Thank you

    Chuck

    Friday, March 24, 2017 7:01 AM
  • Hi,

    Could you provide your detail solution or requirements so we could try to research.

    Best Regards,

    Lee


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Monday, March 27, 2017 7:22 AM
  • Sure, thank you.

    So we have an environment where we have a production SP 2013 Enterprise environment.  SQL 2014 pn separate box and a stand alone development / test server. 

    We are using c# csom and have an app that uses InfoPath 2103 form services with c# code behind. 

    Have tried using various credentials and it comes down to either having a default credential (I think) that has sufficient permissions or log in via the c# code.  When we log in we cannot retrieve the user who access the SharePoint page because the logged in credentials over write the current user credentials.

    Ideally we would be able to run the page with the code without having to login via code.

    The app copies files to another location and once coped looks up the copied file and updates the metadata.  All of that works, we just have this problem with permissions.  Am doing a test with server side code this afternoon to see if that addresses this issue.

    Thank you for looking at this.

    Chuck

    Monday, March 27, 2017 6:41 PM
  • Hi,

    Thanks for your feedback, if the server side solution works, you could share your solution so it may help others who have similar requirement.

    If the solution you’re testing don’t work, let us know so we could try to research.

    Best Regards,

    Lee


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Tuesday, March 28, 2017 11:26 AM
  • Thank you for the feedback.  It is CSOM cause that environment is distributed and the solution needs to be deployed to multiple places.

    Chuck

    Wednesday, April 5, 2017 3:58 PM
  • Hi,

    Thanks for your sharing, you could mark your solution as answer, so it may help other community members find the helpful information quickly

    Best Regards,

    Lee


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Thursday, April 6, 2017 1:43 AM
  • No answer yet.

    How do we get Default Credentials to work in InfoPath 2013 web form hosted in SHarePoint 2013 using c# csom code behind.  That would be an answer.

    Thank you lee

    Chuck

    Thursday, April 6, 2017 2:57 PM
  • Hi,

    I want to confirm your requirement again, could you provide more details.

    You have a InfoPath 2103 form with c# code behind.(what the code behind will do?)

    And another app(SharePoint provider host app?), it will copy file to other location and updates the metadata( so it will copy the InfoPath form to other lcation?).

    I’m confused about” So if jdoe was the user and the code runs on form load and we log in with user the web.current.user now returns user and not jdoe.”( could you share an example?)

    Best Regards,

    Lee


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Friday, April 7, 2017 5:25 AM
  • For Accessing the SP2013 Online side using CSOM server side code, we need to pass the credential of user:

    https://social.msdn.microsoft.com/Forums/office/en-US/fef7e823-6769-4c2c-a6b1-c84c7fe6be4d/microsoftsharepointclientserverunauthorizedaccessexception-occurred-in?forum=sharepointdevelopment#4a190647-9756-45d9-98e9-bd075e4827a7


    Thanks, Amit Kumar, LinkedIn Profile ** My Blog

    Friday, April 7, 2017 8:17 AM
  • So here is what this does:

    users upload documents (lots of them) to a working form library.  There is an an InfoPath form with c# csom code behind that brings up several drop down boxes that contain selections for metadata.  The users who log in have a drop down box that lists their documents.  They make selections from these drop down lists and then click a button to have the documents copied to a production library.  Once the copy is finished the solution looks up the most recently copied document and updates the metadata fields based upon the selections the user made from the drop down boxes.  Then the solution deletes the moved document from the working from library and repaints the drop down box less the documents that have been moved.  The idea here is to make getting larger amounts of documents correctly tagged with metadata and then moved to a valid production library.

    Everything works except the credentials.  We use this:

    context.Credentials = new NetworkCredential("username", "pwd", "domain");

    seems risky to do this.  The code will not run (401 access denied) if we do not use a statement like above.

    Friday, April 7, 2017 11:42 AM
  • Thank you for that.  The issue still remains that you have a password in the GETSECUREPASSWORD method that, at some point, appears to be clear text.

    Trying to find a way to create credentials from the current logged on user without having to put that data in the code.

    Friday, April 7, 2017 11:43 AM
  • Hi,

    One workaround is updating the metadata of ‘new document’ based on source document.

    If you concern about the clear Text for username and password, you could create a service hosted in SharePoint. So you just need call the service form InfoPath.

    Here is one link for your reference.

    https://social.technet.microsoft.com/wiki/contents/articles/24194.sharepoint-2013-create-a-custom-wcf-rest-service-hosted-in-sharepoint-and-deployed-in-a-wsp.aspx 

    Best Regards,

    Lee


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Monday, April 10, 2017 8:07 AM
  • Thank you Lee.  Opened a case with MS support and they said you HAVE to log in with password OR use Kerebos.  So may look at Kerebos downstream.  Apparently the issue is that NTLM will not pass the credentials more than once and since the form is authenticating it creates a second request to the lists / libraries.

    So we will stick with the log in issue for now but look at Kerebos later on and do a test to see if this in fact works.

    Thanks again

    Chuck

    Saturday, April 15, 2017 2:30 PM
  • Hi,

    Thanks for your feedback.

     

    Best Regards,

    Lee


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com


    Wednesday, April 19, 2017 9:52 AM