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
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.
Scenario for this Illustration The site has a survey where users answer a few questions (see Figures 1 and 2). The assignment is to use a program to access the survey so the results can be evaluated, and some of the survey information can be added to a separate list. Figure 1: The survey
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
Figure 2: Responding to the Survey
Directions 1) Open Visual Studio 2) Select File-New-Project
3) Select Visual C# from the left hand side of the window
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
2) Select Add Web Reference
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
In SharePoint, the web services are located in http://<SharePoint site>/_vti_bin/lists.asmx. For example, if the SharePoint site that you are trying to access is http://Bogus/SomeSite, the URL to the web service is http://Bogus/SomeSite/_vti_bin/lists.asmx. • In the URL field, enter the URL to access the web services. For example, if the SharePoint site that you wish to access is http://Bogus/SomeSite, enter http://Bogus/SomeSite/_vti_bin/lists.asmx. • In the Web reference name, give the reference a meaningful name, like SharePointList, SharePointService, or your site name (with no spaces). In this example, I will be calling it SharePointSvce. • Click the Add Reference button. Figure 3 is an illustration of the completed web reference form.
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 /
You should now see a web reference in your Solution Explorer. The next thing we are going to do is begin coding. Before you begin, you should have the List GUID and the View GUID of the list that you are going to access through the code. 1) Add a reference to the System.Net library. You will need this library to perform authentication to the SharePoint site. using System.Net;
You can get the List GUID and the View GUID using the Stramit CAML Viewer (http://www.codeplex.com/S PCamlViewer) or the GUID Picker (http://blogs.msdn.com/ronal us/archive/2007/09/08/a-littleguid-picker.aspx)
2) In the Main function, add a reference to your web reference with the following statement: SharePointSvce.Lists listService = new SharePointSvce.Lists(); 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 6) Add the following line to the next line of your code: System.Xml.XmlNode activeItemData = listService.GetListItems(listGUID, activeItemViewGUID, null, null, "9000", null, "");
To read the documentation on this service, go to: http://msdn.microsoft.com/e nus/library/lists.lists.getlistite 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
8) To add a “clean close” to the program , add the following lines: Console.WriteLine("Press any key to continue"); Console.ReadLine(); Now you are ready to build your project. 1) From the top menu, select Build – Build SPBehindTheScenes (or whatever you called your project)
Your code should build successfully.
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:
Press any key to continue
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
Appendix: Source Code using using using using
System; System.Collections.Generic; System.Text; System.Net;
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"; System.Xml.XmlNode activeItemData = listService.GetListItems(listGUID, activeItemViewGUID, null, null, "9000", null, ""); // 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); } // Prompts the user to "press a key" when finished Console.WriteLine("Press any key to continue"); Console.ReadLine(); } } }
Page 11 of 11 Written on 12/3/2008