Sharepoint 2016 Rest API Authentication for on prem from .NET 6

Daniel Turan 46 Reputation points
2022-04-29T14:40:15.387+00:00

I have a .NET 6 app and I want to use CSOM or Rest API to communicate with Sharepoint 2016 on prem.

  1. CSOM library for onprem sharepoint is not available in .NET
  2. I haven't found any example how to authenticate REST api agains onpres sharepoint.

How should I connect from my app .NET 6 to onprem sharepoint, using username/password or windows auth?

SharePoint Development
SharePoint Development
SharePoint: A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.Development: The process of researching, productizing, and refining new or existing technologies.
2,784 questions
SharePoint Server Development
SharePoint Server Development
SharePoint Server: A family of Microsoft on-premises document management and storage systems.Development: The process of researching, productizing, and refining new or existing technologies.
1,594 questions
0 comments No comments
{count} votes

Accepted answer
  1. RaytheonXie_MSFT 33,251 Reputation points Microsoft Vendor
    2022-05-02T02:11:22.417+00:00

    Hi @Daniel Turan ,
    You can use the SharePoint client object model (CSOM) to retrieve, update, and manage data in SharePoint.
    Step 1: Download the latest SharePoint Client Components SDK from Microsoft and install on your local computer.
    198091-image.png
    Step 2: Add references of the portable dlls in your new project, location should be like this C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI.
    198040-image.png
    Step 3: Refer to following code.

    using System;  
    using System.Collections.Generic;  
    using System.Net;  
    using System.Text;  
    using Microsoft.SharePoint.Client;  
    namespace ContosoConsole  
    {  
    class Repository  
    {  
    public void GetApprovedAppsByView(string siteUrl, NetworkCredential credentials, string listName, string viewName, string camlQuery)  
    {  
    try  
    {  
    using (var context = new ClientContext(siteUrl))  
    {  
    context.Credentials = credentials;  
    List list = context.Web.Lists.GetByTitle(listName);  
    View view = list.Views.GetByTitle(viewName);  
    context.Load(view);  
    CamlQuery query = new CamlQuery();  
    if (context.HasPendingRequest)  
    {  
    context.ExecuteQueryAsync()  
    .Wait();  
    }  
    query.ViewXml = string.Format("<View><Query>{0}{1}</Query></View>", view.ViewQuery, camlQuery);  
    ListItemCollection items = list.GetItems(query);  
    context.Load(items);  
    if (context.HasPendingRequest)  
    {  
    context.ExecuteQueryAsync()  
    .Wait();  
    }  
    foreach (ListItem listItem in items)  
    {  
    Console.WriteLine(listItem["Title"]);  
    }  
    Console.ReadKey();  
    }  
    }  
    catch (Exception ex)  
    {  
    Console.WriteLine("Error Message: " + ex.Message);  
    Console.ReadKey();  
    }  
    }  
    }  
    }  
    

    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Daniel Turan 46 Reputation points
    2022-05-02T07:35:50.4+00:00

    @RaytheonXie_MSFT :

    Thanks. Is is possible to download the files from somewhere? I don't have SharePoint installed locally nor do I have access to the server.

    BTW, why isn't there a nuget for this?
    I'm asking because there is a nuget for Sharepoint Online and even Sharepoint 2016 but .NET Framework only