Trying to access a Microsoft List programmatically using C#

complete

Active member
Joined
Oct 24, 2012
Messages
34
Programming Experience
3-5
I am trying to access a Microsoft List programmatically using C#. It is not going very well just yet. I am using .Net Foundation instead of .Net Core because I read that the Microsoft.Sharepoint.Client namespace is not available with .Net Core.

I should say at the beginning that I am trying to make contact with a Microsoft List, not a SharePoint List. I figure that this will not be an issue since Microsoft List is said to have evolved from SharePoint List.

I will explain what I have done thus far. First of all, from an SDK, I obtained the following DLL's in order to gain the dependency assembly namespaces for SharePoint:

DLLs.png


And I added them as assemblies:

Assemblies.PNG


The variables needed to make this happen include the URL path to the location of the Microsoft List, the name of the Microsoft List and the username and password to the List. To make sure I have all of these things working, I opened an Incongnitio Chrome browser window and put the URL in the address bar. I was immediately prompted to enter the username and password. And it worked without throwing an error.

It-worked.png


But this is not exactly where the List is.

But it is closing time.... I will return once I get home.

Stay tuned...
 
You are accessing anything on sharepoint.com, they are all SharePoint lists.

The easiest way to access SharePoint lists is to use the CSOM library which is what is exposed by that DLL you are referencing above.

But if you want to try to future proof yourself, then start learning how to use the Graph API which will also work with .NET Core.:

Personally, I would use the PnP library which knows to use the Graph API and/or CSOM as needed, and which I feel has a better more consistent interface. Additionally, my gut feel is that PnP will outlive CSOM, Graph API or whatever new API Microsoft comes up with because of the wide support for PnP from both inside and outside of Microsoft:
 
As a quick aside, that DLL you are referencing up there looks to be very old. You'll want to pick up a newer version using Nuget because it will know how to properly talk to SharePoint Online.
 
As a quick aside, that DLL you are referencing up there looks to be very old. You'll want to pick up a newer version using Nuget because it will know how to properly talk to SharePoint Online.

Can I use Nuget with Visual Studio Community version or should I pack up this IDE and use Visual Studio Code.

I just now returned here to see if I can edit the Original Post but it does not look like this web forum allows for that.

Anyway, to cut to the chase and get back to my project, I will continue. Like I said, the program is written in .NET Foundation instead of .Net Core because I read that the Microsoft.Sharepoint.Client namespace is not available with .Net Core. The project is a WinForm project and the UI gets and sets the following text variables:
  • URL address which is stored in the variable txtSiteUrl in and used in the code below with txtSiteUrl.Text.Trim()
  • Username which is stored in the variable txtUserName in and used in the code below with txtUserName.Text.Trim()
  • Password which is stored in the variable txtPassword in and used in the code below with txtPassword.Text.Trim()
  • The title of the List which is stored in the variable txtListTitle in and used in the code below with txtListTitle.Text.Trim()
Now, like I said, I was able to open a browser in incognito mode and go to the site url and, after logging in with the username and password I was able to navigate to the List.

Here is the code that does not work and throws a 404 error
C#:
        private void btnGetListData_Click(object sender, EventArgs e)
        {
            using(ClientContext context = new ClientContext(txtSiteUrl.Text.Trim()))
            {
                context.AuthenticationMode = ClientAuthenticationMode.Default;
                context.Credentials = new NetworkCredential (txtUserName.Text.Trim(), txtPassword.Text.Trim(), txtSiteUrl.Text.Trim());

                Web webObj = context.Web;

                List listObj = webObj.Lists.GetByTitle(txtListTitle.Text.Trim());

                CamlQuery query  = CamlQuery.CreateAllItemsQuery(100);
                ListItemCollection items = listObj.GetItems(query);

                context.Load(items);
                context.ExecuteQuery();
           }
      }

I will post more details as I gather it. But, for now, what do you think?

By the way, this might be an issue. When I log into the website using the url, username, and password, I am directed to a OneDrive page and I have to navigate to where the List exists which is this (I have whited-out any sensitive information)

07.png

So, I decided to add "/List/" at the end of the URL in the UI form of my program:
08.png


If this is proper, shouldn't this line of code contain a full and valid variables?
but after this ine:
ClientContext context = new ClientContext(txtSiteUrl.Text.Trim()
here is the contents of the content variable:
11.png


I will add more information as soon as possible...
 
Last edited:
Moving this out of .NET Framework, since your questions are actually about understanding how SharePoint works rather than how to use .NET Framework.
 
Can I use Nuget with Visual Studio Community version

Yes.

Like I said, the program is written in .NET Foundation instead of .Net Core

It's .NET Framework, not .NET Foundation.

the Microsoft.Sharepoint.Client namespace is not available with .Net Core

No it's not that namespace in not available. It's that the original DLLs were compiled to work with .NET Framework, and not compatible with loading into a .NET Core app domain.
 
No, the site URL that the SharePoint client context is expecting to get is "https://yourtenantname-my.sharepoint.com/personal/dashboard_yourtenantname_com" You are passing in "https://yourtenantname-my.sharepoint.com/personal/dashboard_yourtenantname_com/Lists"

In SharePoint, all the lists (except the document libraries and some system built-in lists) will live under "https://.../site/Lists".

I tried your suggestion and I got a 404 error.

System.Net.WebException
HResult=0x80131509
Message=The remote server returned an error: (400) Bad Request.
Source=System.Net.Requests

Please advise.
 
Make sure you are using the latest CSOM library:

And use the SharePointOnlineCredentials class instead of the NetworkCredentials class:

In response to this post:

report.png


I had a look at what I had available to me in my nuget packages in my Visual Studio Community Edition and this is what I found what looks like a match with the first item you have listed:

which-one.png


I am not having much luck finding the second suggestion:

not-much-luck.png

Please advise.
 

Attachments

  • which-one.png
    which-one.png
    157.1 KB · Views: 13
You found the right Nuget package: "Microsoft.SharePoint.Online.CSOM". That will download the latest version of the Microsoft.SharePoint.Client.dll and related files.

Once you have a reference to that DLL, that will make the SharePointOnlineCredentials class available to you. So instead of setting the Credentials field to a NetworkCredentials object, you would set it to a SharePointOnlineCredentials object. That object knows how to do the appropriate authentication with SharePoint Online. I forget if it actually uses the official OAuth handshaking, or if it uses a Microsoft backdoor.
 
I am sorry I deleted this post by mistake.
It seemed to also delete your reply.

This is the nuget package I installed.
Is it all I need to do?
which-one.png
 
Yes, that is the correct Nuget package.

You would need to remove references to the old DLL you were using and ensure that the compiler is using the one from the Nuget package. In general, if you remove references to the old DLL first, and then install the Nuget package, the correct references will be setup by the IDE.

Then the next thing you would need to do is change from using the NetworkCredential class and use the SharePointOnlineCredential class. (See post #12.)
 

Latest posts

Back
Top Bottom