Skip to main content

Sharepoint 2016 Rest Authentication for on prem from a custom web api

Hi, I am attempting to create an api to repost REST requests to sharepoint.
I created a test that used o365 authentication (SharePointOnlineCredentials) to post to o365 but now need to adapt it to work on premise (SharePoint 2016).

What authentication option would be the easiest to implement for this and how do I go about implementing it?
I have tried looking at a lot of the online resources but none of them seem to give a complete picture of what to do often leaving me missing something like the Authorization: Bearer access_token or the Request Digest Value.

I am using CSOM for sharepoint stuff and RestRequest at the moment.

Hi,
You could create a ‘service’ account so you could authenticate your requests with the ‘service’ account, and get/set SharePoint data by CSOM directly instead of REST API web request.
Sample code:
using (var clientContext = new ClientContext("http://sp:12001/"))
            {
                clientContext.Credentials = new NetworkCredential("user", "pw", "contoso");
                List list = clientContext.Web.Lists.GetByTitle("Users");
                CamlQuery query = new CamlQuery();
                query.ViewXml = "<View><Query><Where><Geq><FieldRef Name='ID'/>" +
                 "<Value Type='Number'>1</Value></Geq></Where></Query><RowLimit>100</RowLimit><ViewFields><FieldRef Name='ID'/><FieldRef Name='Title'/></ViewFields></View>";
                
                var items = list.GetItems(query);

                clientContext.Load(items, eachItem => eachItem.Include(
                item => item.FieldValuesAsText));
                clientContext.ExecuteQuery();
                foreach (ListItem oListItem in items)
                {
                    var values = oListItem.FieldValuesAsText.FieldValues as Dictionary<string, string>;
                    Console.WriteLine(Extensions.FromDictionaryToJson(values));
                }
                Console.WriteLine(items.Count);
              
                Console.ReadKey();
            }
You could check below guideline for more samples.
Bearer access_token used to OAuth2 authorization(SharePoint online).

Comments