none
How to pass networkcredential sharepoint page using client object model (or) serve object model in C#? RRS feed

  • Question

  • Hi,

           I want sharepoint page networkcredntial user account based open the page using sharepoint client object model (or) sharepoint server object model.

    Thanks in advance

    Regards

    by

    Narasimman

    Wednesday, July 17, 2019 7:00 AM

All replies

  • Hi

    You can use the context object.


    Justin Liu Office Apps & Services MVP, MCSE
    Senior Software Engineer
    Please Vote and Mark as Answer if it helps you.

    Thursday, July 18, 2019 1:38 AM
  • Hi Justin Liu ,

                            Thanks for your hlep. i am using client object model i can read list item but page authendication of user account not working sharepoint 2010 site. The below code was not  working to lgoin sharepoint site.

    ClientContext context = new ClientContext("http://sharepoint/");
                    context.AuthenticationMode = ClientAuthenticationMode.Anonymous;
                    Web web = context.Web;
                    List list = web.Lists.GetByTitle("ABS");
                    CamlQuery query = new CamlQuery();
                    ListItem item = list.GetItemById(1);
                    context.Load(web);
                    context.RequestTimeout = 100000;
                    context.Load(list);
                    context.Load(item);
                    context.ExecuteQuery();
                    Microsoft.SharePoint.Client.FieldUserValue userValue = item["Username"] as Microsoft.SharePoint.Client.FieldUserValue;
                    userName = Convert.ToString(userValue.LookupValue);
                    passWord = Convert.ToString(item["Password"]);
                    Domain="SP2010DEV";
                    ClientContext context = new ClientContext("http://sharepoint/");
                    context.Credentials = new NetworkCredential(userName ,passWord ,Domain);
                    Web web = context.Web;
                    context.Load(web);
                    context.ExecuteQuery();
                    Response.redirect("http://sharepoint/SitePages/Test.aspx",false);

    Thanks in advance

    Regards

    by

    Narasimman



    • Edited by SimhaVL Thursday, July 18, 2019 6:46 AM
    Thursday, July 18, 2019 6:42 AM
  • Hi Narasimman,

    You can pass NetworkCredential to ClientContext object like this:

    ClientContext context = new ClientContext(siteUrl);
    Web web = context.Web;
    context.Credentials = new NetworkCredential(username, password, "domain");
    context.Load(web);
    context.ExecuteQuery();
    

    Validate SharePoint credentials using Client Object Model

    Thanks

    Best Regards



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

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.

    Thursday, July 18, 2019 7:22 AM
  • Hi tnmff,

            Thanks for your help. i have tried above code but not working. i need to pass networkCredential to ClientContext object model to accesses the sharepoint site.

    Thanks in advance

    Regards

    by

    Narasimman

    Thursday, July 18, 2019 7:41 AM
  • Hi Narasimman,

    Please pass the valid username and password in NetworkCredential class and the code snippet should be executed before do action to list/listitem object, please see the result run in Console Application:

    In my side, it is working as expected when check in Visual Studio with break point like capture above.

    Thanks

    Best Regards


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

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.

    Thursday, July 18, 2019 7:49 AM
  • Hi tnmff,

                     Thanks for your help. I have used above code only read listitem in sharepoint list. but i want sharepoint page credential user account based login sharepoint site. i have developed custom webpart using client object model. In sharepoint site will maintaing one list username and password. when adding sharepoint anonymous page on custom webpart read username and password on the list after pass credential on the site redirect to without anonymous page . i am fetching username and password passing to networkcredential account based not login sharepont site in any browser.

    concept

            custom webpart is adding to sharepoint anonymous page. the webpart will read listitem username and password in one list. after i will pass the credential authendication user after redirect to without anonymous page credential account based not openning sharepoint site.


    Thanks in advance

    Regards

    by

    Narasimman


    • Edited by SimhaVL Thursday, July 18, 2019 9:23 AM
    Thursday, July 18, 2019 9:18 AM
  • Hi SimhaVL,

    I think you want to access the list item data for username and password with anonymous credential in web part solution.

    This is less related with anonymous page, the page is only to place the custom web part. You will enable the anonymous access in Web Application and Site collection level, please check the steps below:

    http://www.arnoldboersma.nl/post/2014/04/03/Enable-anonymous-access-to-SharePoint-2013-Part-1-WebApplication-settings.aspx

    Then you can use your code snippet posted in another thread to access the list item in ClientContext anonymously:

     context.AuthenticationMode = ClientAuthenticationMode.Anonymous;

    Note, if you want to access the list object with CSOM anonymously, please make sure the list enabled the anonymous access.

    Thanks

    Best Regards


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

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.

    Thursday, July 18, 2019 9:48 AM
  • Hi tnmff,

                 Thanks for your hlep. I heave used above code based fetcing listitem username and password.but In my webpart getting username and apssword passing throw the networkcredential based not opening sharepoint site. I am enabled the anonymous access to sharepoint site list and lisbrary level. 

    Example

                 when entering to  sharepoint 2010 site login to Adminstartor account. I am maintaing one Authendication custom list usernam and password coloumn. Authendication list enter valid Test user name and password created listitem.

    Sharepoint anonymous page my webpart Authendication list read test username and password fetching with client object model. now i am fetching after testuser username and password passing on networkcredential on client object model. sharepoint anonymous page redirect to the anothe page test user based sharepoint site not showing.  Sharepoint anonymous page redirect to another page right corner on login session showing again adminstrator acoount. i want which username and password passing networkcredential based show with sharepoint site.

    Anonymous Pgae

    Redirect Page

    Thanks in advance

    Regards

    by

    Narasimman



    • Edited by SimhaVL Thursday, July 18, 2019 11:09 AM
    Thursday, July 18, 2019 10:14 AM
  • Hi Simha,

    They are different things, CSOM credential is passed in code level, while you want to login the different user credential in browser UI.

    If you want to sign in as a different account, code level not available to achieve, you can redirect user to log out url instead to enter their credential:

    .../_layouts/closeConnection.aspx?loginasanotheruser=true 

    Thanks

    Best Regards


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

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.


    Thursday, July 18, 2019 12:39 PM
  • Hi tnmff,

                        Thanks for your understanding .  So I want exactily sign in as different user account any Code level server (or) Client object modelto login sharepoint 2010 site. If any anotherway to javascript (or) Rest api to lgoin different user credntial based in sharepoint login  site.

    i have tried above url asking signin different useraccount in sharepoint site. but i want programatically pass the credential in this concept. Is it possible to pass login different credential for any code level in sharepoint site browser UI .

    Can please expalin sign in as a different account, code level not available exact reason in sharepoint 2010 site.

    Thanks in advance

    Regards

    by

    Narasimman

                    

    Thursday, July 18, 2019 1:10 PM
  • Hi SimhaVL,

    You can call me Jerry. 

    For SharePoint side, every code level snippet(SSOM/CSOM/JSOM) need to assign user credential to do related action.

    But it's not available to perform UI action inside code level, even with JavaScript. JSOM code also run with current user credential and permission, not able to login into site directly.

    A workaround is that you could find the SharePoint login page inside layouts folder and fill the username and password with Jquery when page loading to make user login without input any character.

    Thanks

    Best Regards


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

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.

    Thursday, July 18, 2019 1:32 PM
  • Hi tnmff,

                        Thanks for your help of understanding. how to contact with Jerry. if any chat option with communicate on jerry in portal site url. i don't know jerrry number. so can you have a jerry number . please give me jerry number i can talk to  this concept on sharepoint site.

    Thanks in advance

    Regards

    by

    Narasimman


    • Edited by SimhaVL Thursday, July 18, 2019 1:58 PM
    Thursday, July 18, 2019 1:57 PM
  • Hi Simha,

    I mean my name is Jerry, if you have any doubt about this question, please post in this thread and we will continue to help you.

    Thanks

    Best Regards


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

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.

    Thursday, July 18, 2019 2:32 PM
  • Hi jerry,

                    Thanks for you help in this concept.

    Regrads

    by

    Narasimman

    Thursday, July 18, 2019 3:34 PM
  • Hi jerry,

                    Thanks for you help in this concept.

    Regrads

    by

    Narasimman

    Hi SimhaVL,

    You are welcome. And for this question, in conclusion, code level is hard to perform login as different user action in Browser UI. You will need to redirect user to sign in as different user page for user to sign in.

    Thanks

    Best Regards


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

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.

    Friday, July 19, 2019 5:08 AM
  • Hi all,

             How to programmatically windows credential based open sharepoint site browser code using server object model (or) client object model (or) any javascript on webpart (or) visual webpart.

    Thanks in advance

    Regards

    by

    Narasimman

    Monday, July 22, 2019 8:09 AM
  • Hi all,

             How to programmatically windows credential based open sharepoint site browser code using server object model (or) client object model (or) any javascript on webpart (or) visual webpart.

    Thanks in advance

    Regards

    by

    Narasimman

    Hi SimhaVL,

    As I have specified above, pass credentail in code level and in Brower UI, they are total different things. You can't perform UI login from code snippet.

    Thanks

    Best Regards


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

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.

    Monday, July 22, 2019 9:50 AM
  • Hi jerry,

                   Thanks for you reply. why can't achieve this credential concept on sharepoint 2010 site.

    can please explain breaflie on this concept. Is there any possible way to achieve this concept using javascript (or) rest api in sharepoint 2010 site.

    Thanks

    Regards

    by

    Narasimman



    • Edited by SimhaVL Monday, July 22, 2019 1:29 PM
    Monday, July 22, 2019 10:00 AM