How to fetch O 365 SP Online list items from inside a ASP.NET MVC application.

Brass Contributor

How to fetch O 365 SP Online list items inside a ASP.NET application.

Am having a requirement, wherein an asp.net MVC application is built and deployed on-prem.

Also, as part of this application there is this module, where users need to key-in lot of input.

But, we have already built this module last year as an SPO site in O 365 using JSOM,SP D workflows etc( not with SPFx).  <p>

Now customer wants to pull this list items of this module into the ASP.NET MVC application , when on button click or dropdown change-(of course , some conditions applied - fetch some IDs, names,status columns etc from SPO-SPList).

Am wondering, any programmatic way of achieving this without using any 3rd party tools or without any Server side coding.

 

8 Replies

Within you ASP.Net MVC application, add the csom Nuget Package https://www.nuget.org/packages/Microsoft.SharePointOnline.CSOM/ and then you should be able to perform CRUD operations on a sharepoint list from your ASP.Net Application. Ideally it is doable. 


@Prasad Punneri wrote:

How to fetch O 365 SP Online list items inside a ASP.NET application.

Am having a requirement, wherein an asp.net MVC application is built and deployed on-prem.

Also, as part of this application there is this module, where users need to key-in lot of input.

But, we have already built this module last year as an SPO site in O 365 using JSOM,SP D workflows etc( not with SPFx).  <p>

Now customer wants to pull this list items of this module into the ASP.NET MVC application , when on button click or dropdown change-(of course , some conditions applied - fetch some IDs, names,status columns etc from SPO-SPList).

Am wondering, any programmatic way of achieving this without using any 3rd party tools or without any Server side coding.

 


 

What about the authentication ? since, our ASP.NET is deployed in on-prem and SPO in O 365.

is it possible to interact from a asp.net on-prem appln to SPO cloud?

asp.net is using the windows auth. and SPO is using auth. based on Azure.

Should not it throw error while trying to connect SPSite in SPO from this asp.net appln? 

 

 

You're probably going to want to register your app (AddIn) in SharePoint then use the client ID and Client Secret assigned by SharePoint to authenticate using CSOM

If your fine with not using the User Context, You can create a Service Account and use that to perform the CRUD Operation on the SharePoint List. Sample Code Below.. 

 

string siteURL = "";
            string userName = "";
            string password = "";
            ClientContext clientContext = new ClientContext(siteURL);
            SecureString securePassword = new SecureString();
            foreach (char c in password.ToCharArray()) securePassword.AppendChar(c);
            clientContext.Credentials = new SharePointOnlineCredentials(userName, securePassword);
            Web web = clientContext.Web;
            clientContext.Load(web);
            clientContext.ExecuteQuery();
            Console.WriteLine(web.Title);

creating a service account is not possible, as we have requested to customer for creation of a serviceaccount already, from last year onwards. but unfortunately,they are not ready to provide us this service account id.

At present,client is not giving us necessary permissions to even for accessing the tenant. we dont have site collec.permissions, we dont have powershell access,  and dont have   O 365 Admin center access!

is it possible to create /register an app in SPO without those privileges.

You must have Site Collection access.  If you Don't have SC access, you need to ask the client to Do the Steps. 

 

Step -1 - Register a new app

http://{sharepointsite}/_layouts/15/AppRegNew.aspx

 

Image12.PNG

Step2-  Grant permissions for the app

https://*******************.sharepoint.com/sites/********/_layouts/15/Appinv.aspx

Lookup the client Id from Step 1

 

image13.png

 

In the Permissions use this. -

<AppPermissionRequests AllowAppOnlyPolicy="true">

   <AppPermissionRequest Scope="http://sharepoint/content/sitecollection/web" Right="FullControl" />

</AppPermissionRequests>

 

 

Step3 - 

Step3 - Follow instruction in Blog - http://www.sharepointpals.com/post/How-to-Get-the-Client-Context-Using-App-Access-token-by-passing-C...

 

sample code attached