Beruflich Dokumente
Kultur Dokumente
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 3: Adding Data to the SharePoint List Using Data from Another 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 demonstrated 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 demonstrated 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.
If you have already followed the tutorial for Parts 1 and 2 of this series, you should have already created a
project called SPBehindTheScenes. We will be modifying that project for this tutorial.
Page 2 of 18
Written on 12/9/2008
Accessing SharePoint Behind the Scenes: How to Access SharePoint Data using C# Without Running the Code on a SharePoint
Server.
Part 3: Adding Data to the SharePoint List Using Data from Another SharePoint List
Directions
Tip: From Windows Explorer,
1) Open Visual Studio you can also go directly to the
directory where you saved
2) Select File-Open-Project/Solution your project and double-click
on the Solution file to open
Visual Studio and
automatically open your
project.
Page 3 of 18
Written on 12/9/2008
Accessing SharePoint Behind the Scenes: How to Access SharePoint Data using C# Without Running the Code on a SharePoint
Server.
Part 3: Adding Data to the SharePoint List Using Data from Another SharePoint List
3) In the Open File dialog window, change the directory to where you saved your project.
Page 4 of 18
Written on 12/9/2008
Accessing SharePoint Behind the Scenes: How to Access SharePoint Data using C# Without Running the Code on a SharePoint
Server.
Part 3: Adding Data to the SharePoint List Using Data from Another SharePoint List
Page 5 of 18
Written on 12/9/2008
Accessing SharePoint Behind the Scenes: How to Access SharePoint Data using C# Without Running the Code on a SharePoint
Server.
Part 3: Adding Data to the SharePoint List Using Data from Another SharePoint List
Page 6 of 18
Written on 12/9/2008
Accessing SharePoint Behind the Scenes: How to Access SharePoint Data using C# Without Running the Code on a SharePoint
Server.
Part 3: Adding Data to the SharePoint List Using Data from Another SharePoint List
Your information will vary depending on the list that you are using to access the information.
listService.Credentials = System.Net.CredentialCache.DefaultCredentials;
3) Move your cursor to the last line of the foreach (XmlNode node in xl) block (before the
closing bracket [}] ) and press enter to go to the next line. If you are following a similar path to
the tutorial, the last line should be Console.WriteLine();
We need to keep track of the item number for adding records to the list.
5) We are going to instantiate a StringBuilder object to build the text for the XML. Note that
your field names for the XML may vary from this example. Replace the field names with the field
names of your list.
StringBuilder sbXML = new StringBuilder();
sbXML.Append("<Method ID='" + itemCounter + "' Cmd='New'>");
sbXML.Append("<Field Name='Title'>" + fname + "</Field>");
sbXML.Append("<Field Name='Favorite_x0020_Python'>" + python +
"</Field>");
sbXML.Append("</Method>");
String strBatch = sbXML.ToString();
/*
* If you don't have extraneous characters to remove from the values from SharePoint, you don't
* need to use the Regex functions. You would only need to do this if you want to strip the
Page 7 of 18
Written on 12/9/2008
Accessing SharePoint Behind the Scenes: How to Access SharePoint Data using C# Without Running the Code on a SharePoint
Server.
Part 3: Adding Data to the SharePoint List Using Data from Another SharePoint List
/*
* In this area, this is where you build the XML to add information on your list. You
* will reference the field names that are on your list. For example, if there is a field name
* on your list called "First Name", the field will probably be known in SharePoint as
* First_x0020_Name. You will need to verify the field names in SharePoint by running
* CAML Viewer or another tool that will return the information for you.
*/
StringBuilder sbXML = new StringBuilder();
sbXML.Append("<Method ID='" + itemCounter + "' Cmd='New'>");
sbXML.Append("<Field Name='Title'>" + fname + "</Field>");
sbXML.Append("<Field Name='Favorite_x0020_Python'>" + python + "</Field>");
sbXML.Append("</Method>");
String strBatch = sbXML.ToString();
}
Notice the Cmd parameter in the Method element of the XML. The Cmd parameter dictates what action
to take with the update. The value of New indicates that you are adding items.
6) We are going to continue to build the XML that the UpdateListItems method needs to add the
items to the list. Add the following code on the next line after the String strBatch =
sbXML.ToString(); line:
Remember to substitute the GUID values in this example with the List and View
GUID of your list.
What this is doing is you are setting up a few extra attributes of the XML called OnError and
ViewName. The OnError attribute dictates what action to take if the method encounters an error
with adding the items. In this case, we are going to Continue with the process. The ViewName
attribute is the GUID of the view.
7) Finally, we are going to add the call to the UpdateListItems method. We are also going to
add a few “status” lines as well. Add the following lines after the elBatch.InnerXml =
strBatch; line:
XmlNode ndReturn = listService.UpdateListItems(comboListGUID, elBatch);
Console.WriteLine("****************************************************
********");
Console.WriteLine("Response from adding " + fname + ": " +
ndReturn.InnerXml);
Page 8 of 18
Written on 12/9/2008
Accessing SharePoint Behind the Scenes: How to Access SharePoint Data using C# Without Running the Code on a SharePoint
Server.
Part 3: Adding Data to the SharePoint List Using Data from Another SharePoint List
Console.WriteLine("****************************************************
********");
Console.WriteLine();
To get the status of how the item was added, look at the InnerXml property of instantiated XmlNode
object holding the results of the UpdateListItems method. In our example, the reference to the
XmlNode object is called ndReturn.
/*
* If you don't have extraneous characters to remove from the values from SharePoint, you don't
* need to use the Regex functions. You would only need to do this if you want to strip the
* extraneous data from the values.
*/
responseNumber = System.Text.RegularExpressions.Regex.Replace(responseNumber, "#", "");
responseNumber = System.Text.RegularExpressions.Regex.Replace(responseNumber, ";", "");
quest = System.Text.RegularExpressions.Regex.Replace(quest, "<div>", "");
quest = System.Text.RegularExpressions.Regex.Replace(quest, "</div>", "");
/*
* In this area, this is where you build the XML to add information on your list. You
* will reference the field names that are on your list. For example, if there is a field name
* on your list called "First Name", the field will probably be known in SharePoint as
* First_x0020_Name. You will need to verify the field names in SharePoint by running
* CAML Viewer or another tool that will return the information for you.
*/
StringBuilder sbXML = new StringBuilder();
sbXML.Append("<Method ID='" + itemCounter + "' Cmd='New'>");
sbXML.Append("<Field Name='Title'>" + fname + "</Field>");
sbXML.Append("<Field Name='Favorite_x0020_Python'>" + python + “</Field>");
sbXML.Append("</Method>");
String strBatch = sbXML.ToString();
// Note that you will need to substitute the GUIDs with the List and View GUIDs of your
// list
String comboListGUID = "E3AE6AE5-07EE-4883-89E7-3D44485860F7";
String comboListViewGUID = "BF4BEC5C-0DBB-434D-A3DA-4A7D7EB91748";
XmlDocument xmlDoc = new System.Xml.XmlDocument();
System.Xml.XmlElement elBatch = xmlDoc.CreateElement("Batch");
elBatch.SetAttribute("OnError", "Continue");
elBatch.SetAttribute("ViewName", comboListViewGUID);
elBatch.InnerXml = strBatch;
Console.WriteLine("************************************************************");
Page 9 of 18
Written on 12/9/2008
Accessing SharePoint Behind the Scenes: How to Access SharePoint Data using C# Without Running the Code on a SharePoint
Server.
Part 3: Adding Data to the SharePoint List Using Data from Another SharePoint List
Console.WriteLine("************************************************************");
Console.WriteLine();
}
namespace SPBehindTheScenes
{
class Program
{
static void Main(string[] args)
{
int itemCounter = 0;
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://Bogus/Site1/SubSite1/_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";
try
{
System.Xml.XmlNode activeItemData = listService.GetListItems(listGUID,
activeItemViewGUID, null, null, "9000", null, "");
/*
* If you don't have extraneous characters to remove from the values from
Page 10 of 18
Written on 12/9/2008
Accessing SharePoint Behind the Scenes: How to Access SharePoint Data using C# Without Running the Code on a SharePoint
Server.
Part 3: Adding Data to the SharePoint List Using Data from Another SharePoint List
* SharePoint, you don't need to use the Regex functions. You would only
* need to do this if you want to strip the extraneous data from the values.
*/
responseNumber = System.Text.RegularExpressions.Regex.Replace(responseNumber,
"#", "");
responseNumber = System.Text.RegularExpressions.Regex.Replace(responseNumber,
";", "");
quest = System.Text.RegularExpressions.Regex.Replace(quest, "<div>", "");
quest = System.Text.RegularExpressions.Regex.Replace(quest, "</div>", "");
/*
* In this area, this is where you build the XML to add information on your
* list. You will reference the field names that are on your list. For
* example, if there is a field name on your list called "First Name", the
* field will probably be known in SharePoint as First_x0020_Name. You will
* need to verify the field names in SharePoint by running
* CAML Viewer or another tool that will return the information for you.
*/
StringBuilder sbXML = new StringBuilder();
sbXML.Append("<Method ID='" + itemCounter + "' Cmd='New'>");
sbXML.Append("<Field Name='Title'>" + fname + "</Field>");
sbXML.Append("<Field Name='Favorite_x0020_Python'>" + python + "</Field>");
sbXML.Append("</Method>");
String strBatch = sbXML.ToString();
// Note that you will need to substitute the GUIDs with the List and View
// GUIDs of your list
String comboListGUID = "E3AE6AE5-07EE-4883-89E7-3D44485860F7";
String comboListViewGUID = "BF4BEC5C-0DBB-434D-A3DA-4A7D7EB91748";
XmlDocument xmlDoc = new System.Xml.XmlDocument();
System.Xml.XmlElement elBatch = xmlDoc.CreateElement("Batch");
elBatch.SetAttribute("OnError", "Continue");
elBatch.SetAttribute("ViewName", comboListViewGUID);
elBatch.InnerXml = strBatch;
Console.WriteLine("************************************************************");
Console.WriteLine("Response from adding " + fname + ": " +
ndReturn.InnerXml);
Console.WriteLine("************************************************************");
Console.WriteLine();
}
}
catch (Exception e)
{
Console.WriteLine("The following error has occurred: " + e.Message);
}
Compile and test your program by clicking the start button at the top of your toolbar.
Your results should look similar to this:
Page 11 of 18
Written on 12/9/2008
Accessing SharePoint Behind the Scenes: How to Access SharePoint Data using C# Without Running the Code on a SharePoint
Server.
Part 3: Adding Data to the SharePoint List Using Data from Another SharePoint List
To confirm that the items have been added to the list, open the list in SharePoint.
Page 12 of 18
Written on 12/9/2008
Accessing SharePoint Behind the Scenes: How to Access SharePoint Data using C# Without Running the Code on a SharePoint
Server.
Part 3: Adding Data to the SharePoint List Using Data from Another SharePoint List
Page 13 of 18
Written on 12/9/2008
Accessing SharePoint Behind the Scenes: How to Access SharePoint Data using C# Without Running the Code on a SharePoint
Server.
Part 3: Adding Data to the SharePoint List Using Data from Another SharePoint List
Another Trick to Get the Field Names: Editing the Field Names in SharePoint
Sometimes you may not be able to get the field names from one of the tools like CAML Viewer or GUID
Picker because there is no data in the lists. However, you can get the field names by “editing” the field
names directly in SharePoint. If you have the proper permission to edit your list, do the following:
1. Open your list in SharePoint
Page 14 of 18
Written on 12/9/2008
Accessing SharePoint Behind the Scenes: How to Access SharePoint Data using C# Without Running the Code on a SharePoint
Server.
Part 3: Adding Data to the SharePoint List Using Data from Another SharePoint List
Page 15 of 18
Written on 12/9/2008
Accessing SharePoint Behind the Scenes: How to Access SharePoint Data using C# Without Running the Code on a SharePoint
Server.
Part 3: Adding Data to the SharePoint List Using Data from Another SharePoint List
5. Look in the URL for the parameter Field. The value in the Field is your field name without the
ows_ parameter.
http://<site name>/_layouts/FldEdit.aspx?List={E3AE6AE5-07EE-4883-89E7-
3D44485860F7}&Field=Title&Source=http%3A%2F%2Fspdev2%2Fsite1%2Fsurveys%2FLists%2FFav
orite%2520Pythons%2FAllItems%2Easpx%3FShowInGrid%3DTrue%26View%3D%257BBF4BEC5C%
252D0DBB%252D434D%252DA3DA%252D4A7D7EB91748%257D
In this example, the field name is Title. The field name in SharePoint is actually ows_Title.
Page 16 of 18
Written on 12/9/2008
Accessing SharePoint Behind the Scenes: How to Access SharePoint Data using C# Without Running the Code on a SharePoint
Server.
Part 3: Adding Data to the SharePoint List Using Data from Another SharePoint List
namespace SPBehindTheScenes
{
class Program
{
static void Main(string[] args)
{
int itemCounter = 0;
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://Bogus/Site1/SubSite1/_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";
try
{
System.Xml.XmlNode activeItemData = listService.GetListItems(listGUID,
activeItemViewGUID, null, null, "9000", null, "");
/*
* If you don't have extraneous characters to remove from the values from
* SharePoint, you don't need to use the Regex functions. You would only
* need to do this if you want to strip the extraneous data from the values.
*/
responseNumber = System.Text.RegularExpressions.Regex.Replace(responseNumber,
"#", "");
responseNumber = System.Text.RegularExpressions.Regex.Replace(responseNumber,
";", "");
quest = System.Text.RegularExpressions.Regex.Replace(quest, "<div>", "");
Page 17 of 18
Written on 12/9/2008
Accessing SharePoint Behind the Scenes: How to Access SharePoint Data using C# Without Running the Code on a SharePoint
Server.
Part 3: Adding Data to the SharePoint List Using Data from Another SharePoint List
/*
* In this area, this is where you build the XML to add information on your
* list. You will reference the field names that are on your list. For
* example, if there is a field name on your list called "First Name", the
* field will probably be known in SharePoint as First_x0020_Name. You will
* need to verify the field names in SharePoint by running
* CAML Viewer or another tool that will return the information for you.
*/
StringBuilder sbXML = new StringBuilder();
sbXML.Append("<Method ID='" + itemCounter + "' Cmd='New'>");
sbXML.Append("<Field Name='Title'>" + fname + "</Field>");
sbXML.Append("<Field Name='Favorite_x0020_Python'>" + python + "</Field>");
sbXML.Append("</Method>");
String strBatch = sbXML.ToString();
// Note that you will need to substitute the GUIDs with the List and View
// GUIDs of your list
String comboListGUID = "E3AE6AE5-07EE-4883-89E7-3D44485860F7";
String comboListViewGUID = "BF4BEC5C-0DBB-434D-A3DA-4A7D7EB91748";
XmlDocument xmlDoc = new System.Xml.XmlDocument();
System.Xml.XmlElement elBatch = xmlDoc.CreateElement("Batch");
elBatch.SetAttribute("OnError", "Continue");
elBatch.SetAttribute("ViewName", comboListViewGUID);
elBatch.InnerXml = strBatch;
Console.WriteLine("************************************************************");
Console.WriteLine("Response from adding " + fname + ": " +
ndReturn.InnerXml);
Console.WriteLine("************************************************************");
Console.WriteLine();
}
}
catch (Exception e)
{
Console.WriteLine("The following error has occurred: " + e.Message);
}
Page 18 of 18
Written on 12/9/2008