In this example you’ll see the basics of programmatically connecting .NET code (a simple WebForm) from your local machine to read a Documents list hosted on a SharePoint server. Specifically, in this example I am using Visual Studio 2017 and am connecting to a SharePoint 2016 site on the local network.
Setting up your SharePoint Test List
For starters, you’ll need to create a SharePoint Documents Library on a test site. In the case of this example, the test site is located at: http://TestSite/sites/testing and I’ve left the default name of Documents for the SharePoint documents list.
Then for testing purposes, I’ve added a custom Date and Time column to the SharePoint documents library called INSDATE. Also, to have something to test with, I have manually uploaded three documents and have filled out the Meta-Data for them in SharePoint.
Setting up your Programming Environment:
Ok, now for the details of getting your WebForm to connect to SharePoint. Again, note that you are not running Visual Studio on the same machine as SharePoint, we will use the SharePoint .NET Framework client object model (CSOM) (see: https://docs.microsoft.com/en-us/sharepoint/dev/sp-add-ins/complete-basic-operations-using-sharepoint-client-library-code )
Before you open Visual Studio, you’ll need to make sure you’ve installed the required SharePoint Components SDK. For SharePoint 2016, this is free from Microsoft at:
Next open Visual Studio and create a new project. In this example, create a new VB.NET WebForms project. Obviously this will also work just about the same if you use something other than a WebForm, I just find it quickest to set up a test page using a WebForm.
Now that you have your project open in front of you, you will need to add references to the relevant SharePoint DLLs. To do so:
- Right Click on your WebForms project Name
- Select the ‘Add’ option
- Select the ‘Reference’ option
- Under the ‘Assemblies’ Heading, click the ‘Extensions’ option
- Scroll down to find the two DLLs:
- Click the Checkbox to the Right of each DLL to select them both
- Finally, click the ‘OK’ button at the bottom to add the two DLLs
VB.NET Code for the WebForm
As I mentioned above, I set up my test connection to SharePoint using VB.NET in a WebForm, since I find this the quickest way to test example code. Obviously you can write it in C# if you put the code into a converter, and using a WebForm isn’t necessary either since anything really should work for testing purposes.
So for this example, set up a new VB WebForm and add a TextBox to it with the property TextMode=”MultiLine” set so that you can see each line of the SharePoint Document library as it is read.
Note that if you want to see all of the listItem options available to you, set a breakpoint in the For loop where the TextBox1 is getting set, and mouse over the listItem variable in the loop declaration. ListItem is the Document library row object, and each cell within the row is represented by a FieldValues object. You can expand the FieldValues node in the variable properties to see what is available to you. As you add new custom columns to your SharePoint list, you’ll see these appear in the FieldValues list.
Imports Microsoft.SharePoint.Client Partial Class _Default Inherits Page Private Sub _Default_Load(sender As Object, e As EventArgs) Handles Me.Load Dim context As New ClientContext("http://TestSite/sites/testing") Dim testList As List = context.Web.Lists.GetByTitle("Documents") Dim query As CamlQuery = CamlQuery.CreateAllItemsQuery(100) Dim items As ListItemCollection = testList .GetItems(query) context.Load(items) context.ExecuteQuery() For Each listItem As ListItem In items TextBox1.Text = TextBox1.Text & ControlChars.CrLf & listItem("INSDATE") Next End Sub End Class
Dealing with a Common Column Error
Note that it is very possible that when you are trying to populate data into a new column, you will see an error like this:
Column 'Test Column' does not exist. It may have been deleted by another user.
This is because the name displayed to you as a SharePoint user is different from the internal name used by SharePoint. So while you may have named the column “Test Column”, in reality SharePoint could have named it something quite unexpected.
There are different ways to get the actual name, but each involves working with the column and then looking at the URL in your browser.
Method #1 for Sortable Columns
For sortable single Line Column types, you can just look at the SortField parameter in the URL that happens when you click on the column heading to sort by it. As I mentioned, this will work with any sortable column, but not with non-sortable columns like multi-line text columns.
For example, if you go to your Document library, and then click on the name of the column that you want to see the name for, SharePoint will order by that column, and furthermore, it will show the SortField%3DTest Column1-SortDir parameters for you in the URL. From there it’s a simple matter of figuring out what the Internal Column name in SharePoint actually is.
Method #2 for All Columns
The second way to get the Internal SharePoint Column name is to:
- Go to Site Settings and then to Site Columns (under the heading Web Designer Galleries).
- Find the column you are interested in and click on it.
- Now look at the browser URL for the parameter Field=
- Get the value for the Field= parameter and replace any instance of %5F with an underscore _ .
- You now have the internal SharePoint Column name
As you can see, this way of getting the Internal name isn’t as straightforward since you will need to replace special coded values such as %5F, but it will let you get the ID of non-sortable columns, which is useful.
Note that if you see an x0020, that you need to keep this as part of the internal name. So, for example if the URL parameter says this:
Then you would replace the %5F values with underscores to get the following internal ID: