0
votes

I am trying to access some SharePoint lists from a .Net (C#) web application. I googled the topic and it seemed the consensus was to use WCF Data Service (steps outlined below). After I get the data, I display it in a jQuery datatable, using Ajax web service call to grab the data.

Firt step was to run DataSvcUtil.exe to create a DataService.cs file that contains the client data service classes that are needed to access a data service from a .NET Framework client application. Then added references to Microsoft.Data.OData.dll and Microsoft.Data.Services.Client.dll.

I created a web service API (below). Run locally, from within VS 2017, or from IIS on application server, I get all the data, albeit in 80 seconds (way too long for 16,000 records). However when I ran this from a browser, I got status code 500 error and found out it was due to credential issue.

I was setting credentials to CredentialCache.DefaultCrdentilas (later changed to DefaultNetworkCredentials) but had to use NetworkCrdentials(UserID, pwd, domain).

[WebMethod]
[ScriptMethod(UseHttpGet = false, ResponseFormat = ResponseFormat.Json)]

public string GetCompletedAssessments()
{
    SPService_DataContext dc = new SPService_DataContext(new Uri("http://my_sp_site_.com/sites/nvx/SPService/_vti_bin/listdata.svc/"));
    dc.Credentials = System.Net.CredentialCache.DefaultCredentials;

    // Had to change the above to this to get it to work
    dc.Credentials = new System.Net.NetworkCredential(MyUserID, MyPassword, DomainName);

    var source = dc.CompletedAssessments;
    string JSONresult = JsonConvert.SerializeObject(source);
    return JSONresult;
}

However, I was just testing it using my credentials which is not feasible. Is there anything I can do using the method of accessing data I am using, or should I use some other way of accessing the data that does not require providing credentials?

Application is on a server in same domain as the server SharePoint in on but in a different location.

1

1 Answers

0
votes

If you want the application to read the data, you must supply the credentials.

You could try to use REST or JSOM to fetch the data from the client. That way the users credentials are used and the result will be security trimmed.

REST example: http://my_sp_site_.com/sites/nvx/SPService/_api/web/lists/GetByTitle('Pages')/items

If you add an Accept header with application/json;odata=verbose, the result is in JSON. Otherwise its in XML.

SharePoint REST documentation: https://docs.microsoft.com/en-us/sharepoint/dev/sp-add-ins/working-with-lists-and-list-items-with-rest