Accessing SharePoint Data Using C# Without Running Code On The SharePoint Server (Part 1)
Accessing SharePoint Data Using C# Without Running Code On The SharePoint Server (Part 1)
Scenes:
How to Access SharePoint Data using C#
Without Running the Code on a
SharePoint Server.
Jennifer Lewis
Accessing SharePoint Behind the Scenes: How to Access SharePoint Data using C# Without Running the Code on a SharePoint
Server.
Part 1: Connecting to the SharePoint List
Overview
SharePoint is an excellent tool for centralizing communications for a project team, department or
company. Developers can also write programs that can take SharePoint’s functionality to the next level.
For example, a developer can write a program to take survey data and create a robust report based on
the survey data.
There are two ways you can reference SharePoint when writing code:
• Reference and use the classes the Microsoft.Sharepoint library
• Make a web reference to the SharePoint services.
While referencing and using the classes in the Microsoft.SharePoint library is the easier coding method of
the two, it requires that you run the program on the same server as SharePoint. You may have a
situation where you don’t want to run the code on the same server as SharePoint. For example, I needed
to write a program that took data from a survey list, make calculations with the data, and put the
calculated and summarized data in a new list. Because of our current SharePoint architecture, I didn’t
really want to run the code on the same server because it would require me to move my code to the
production SharePoint server, log in to the production server, and run the code.
This series will demonstrate how to access SharePoint data “behind the scenes ” without having to run the
program on the SharePoint server. In Part 1 of this series, I will demonstrate how to access the
SharePoint service using a web reference as well as how to access a list. In Part 2 of this series, I will
demonstrate how to actually extract the data in the list. In Part 3 of this series, I will demonstrate how to
add data to a SharePoint list using the program.
Page 2 of 11
Written on 12/3/2008
Accessing SharePoint Behind the Scenes: How to Access SharePoint Data using C# Without Running the Code on a SharePoint
Server.
Part 1: Connecting to the SharePoint List
Directions
1) Open Visual Studio
2) Select File-New-Project
Page 3 of 11
Written on 12/3/2008
Accessing SharePoint Behind the Scenes: How to Access SharePoint Data using C# Without Running the Code on a SharePoint
Server.
Part 1: Connecting to the SharePoint List
4) Select Console Application from the right hand side of the window
5) In the Name field, enter a name for the project. For this illustration, I’ll call it
SPBehindTheScenes.
a. If you want to change where to save the project in the Location field, you may do so.
For this illustration, I will leave it where it is.
b. If you don’t want to create a directory for this solution, you can uncheck the Create
directory for solution. For this illustration, I will leave this field checked.
6) Click OK
The first thing we need to do in our project is add a reference to the SharePoint site.
1) From the Solutions Explorer, right click on your project name.
Page 4 of 11
Written on 12/3/2008
Accessing SharePoint Behind the Scenes: How to Access SharePoint Data using C# Without Running the Code on a SharePoint
Server.
Part 1: Connecting to the SharePoint List
Page 5 of 11
Written on 12/3/2008
Accessing SharePoint Behind the Scenes: How to Access SharePoint Data using C# Without Running the Code on a SharePoint
Server.
Part 1: Connecting to the SharePoint List
Page 6 of 11
Written on 12/3/2008
Accessing SharePoint Behind the Scenes: How to Access SharePoint Data using C# Without Running the Code on a SharePoint
Server.
Part 1: Connecting to the SharePoint List
Figure 3
Be sure that this section contains the URL of the SharePoint site (ex:
http://bogus/somesite /
This statement indicates that you will be using the Lists web service.
3) In the next line, you will be setting up the authentication credentials to the SharePoint site.
a. To use the logged in user’s credentials, add the following line:
listService.Credentials =
System.Net.CredentialCache.DefaultCredentials;
b. To use another set of credentials, add the following line:
listService.Credentials = new NetworkCredential(“userID”,
“password”, “domain”);
i. Replace “userID” with the user ID to log in as. For example, if you are going to
log in as user “Bogus”, enter “Bogus” (include the quotation marks).
Page 7 of 11
Written on 12/3/2008
Accessing SharePoint Behind the Scenes: How to Access SharePoint Data using C# Without Running the Code on a SharePoint
Server.
Part 1: Connecting to the SharePoint List
ii. Replace “password” with the password of the user ID that you’re logging in to
SharePoint as. For example, if Bogus’s password is “password”, enter
“password” (include the quotation marks).
iii. Replace “domain” with the domain of the user ID that you’re logging in to
SharePoint as. For example, if Bogus’s domain is “Contoso”, enter “Contoso”
(include the quotation marks).
For this illustration, I will just use the credentials of the user who is running the program.
4) Only follow this step if you are referencing a sub site. If you are referencing the top level
of the site collection, go to the next step. There is a quirk with SharePoint where you have to
re-set the URL if you are trying to access a sub-site of a site collection. To re-set the URL, add
the following line on the next line:
listService.Url = listService.Url =
"http://<SPSrvr>/<site>/<SubSite>/_vti_bin/lists.asmx";
where <SPSrvr> is your SharePoint server URL, <site> is your site collection, and <SubSite> is
your subsite.
For example, if you are going to access subsite SubSite1 under site collection Site1 on server
Bogus, and you access the site from URL http://Bogus/Site1/SubSite1, you would enter
http://Bogus/Site1/SubSite1/_vti_bin/lists.asmxas the value.
5) To make it easier to call the functions, create two String variables to hold the list GUID and the
view GUID. Type the following in the next line:
String listGUID = "ListGUID";
String activeItemViewGUID = "ViewGUID ";
Remember to substitute the value “ListGUID” with the actual GUID of the list (including the
quotations), and remember to substitute the value “ViewGUID” with the actual GUID of the view
(including the quotations).
Now you are ready to call the service to get the list items
To read the documentation on
6) Add the following line to the next line of your code: this service, go to:
System.Xml.XmlNode activeItemData = http://msdn.microsoft.com/e
listService.GetListItems(listGUID, n-
activeItemViewGUID, null, null, "9000", us/library/lists.lists.getlistite
null, ""); ms.aspx
What this command is doing is calling the GetListItems service to return the list items for the
particular list and view that you pass to the function. In this example, we are interested in all the list
items and all the fields, and we want a maximum of 9000 records.
7) When we call the web service to return the list items, it will return XML to us. For this lesson, we
are going to keep it simple just to illustrate how to view the XML that was returned. Add the
following lines:
// Go through the list to see what was returned
foreach (System.Xml.XmlNode listItem in activeItemData)
{
// Get the attributes
System.Xml.XmlAttributeCollection attrs = listItem.Attributes;
Console.WriteLine(listItem.OuterXml);
}
Page 8 of 11
Written on 12/3/2008
Accessing SharePoint Behind the Scenes: How to Access SharePoint Data using C# Without Running the Code on a SharePoint
Server.
Part 1: Connecting to the SharePoint List
1) From the top menu, select Build – Build SPBehindTheScenes (or whatever you called your
project)
Page 9 of 11
Written on 12/3/2008
Accessing SharePoint Behind the Scenes: How to Access SharePoint Data using C# Without Running the Code on a SharePoint
Server.
Part 1: Connecting to the SharePoint List
2) To test your program, click the start button at the top of your toolbar.
You should see an output containing your list entries that looks similar to this:
If you want to see the full source code, go the Appendix at the end of this document.
Page 10 of 11
Written on 12/3/2008
Accessing SharePoint Behind the Scenes: How to Access SharePoint Data using C# Without Running the Code on a SharePoint
Server.
Part 1: Connecting to the SharePoint List
namespace SPBehindTheScenes
{
class Program
{
static void Main(string[] args)
{
SharePointSvce.Lists listService = new SharePointSvce.Lists();
listService.Credentials = System.Net.CredentialCache.DefaultCredentials;
// Replace the values in brackets with the actual brackets. For example, if the site
// you will be working with is http://Bogus/Site1/SubSite1, enter
// http://Bogus/Site1/SubSite1/_vti_bin/lists.asmx as the value
listService.Url = "http://spdev2/site1/surveys/_vti_bin/lists.asmx";
// Choose the list and the view. Remember to substitute the values with the actual
// List GUID and View GUID that you want to work with.
String listGUID = "FA929542-19F6-47C0-9966-81234F0AA9E9";
String activeItemViewGUID = "C08B3AFA-2FA6-40CD-9DE0-F8CCFBDED6B7";
Page 11 of 11
Written on 12/3/2008