Upload
jennifer
View
3.063
Download
1
Embed Size (px)
DESCRIPTION
In Part 3 of the series, this document demonstrates how to add items to a list on SharePoint.
Citation preview
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 a SharePoint List Using Data from Another 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 3: Adding Data to the SharePoint List Using Data from Another SharePoint List
Page 2 of 18 Written on 12/9/2008
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.
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 (see Figure 3). 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.
Figure 1: The survey
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 3 of 18 Written on 12/9/2008
Directions 1) Open Visual Studio 2) Select File-Open-Project/Solution
Figure 2: Responding to the Survey
Tip: From Windows Explorer, you can also go directly to the directory where you saved your project and double-click on the Solution file to open Visual Studio and automatically open your project.
Figure 3: The destination list
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 4 of 18 Written on 12/9/2008
3) In the Open File dialog window, change the directory to where you saved your project.
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
4) Select the SPBehindTheScenes solution from the open file dialog. 5) Click the Open button.
6) In the Solution Explorer, double-click on Program.cs to open it for editing.
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
Now we are going to make a few changes to our code. The method that we are going to be using is the UpdateListItems method. This method accepts two
parameters: • listname is the GUID of the list where you will be
“updating” items • updates is the XML containing the items to update as
well as additional information such as the view GUID. In order to get the values that the method needs, you will need to know the List and View GUID of the list that will be updated, as well as the actual fields names that SharePoint uses for the fields in the destination list. Nearly all the fields names start with ows_, followed with the field name that
was used in the list definition. Note that SharePoint will substitute special characters like spaces with the hexadecimal value. For example, if you have a field on your list called First Name, SharePoint will reference the field name as ows_First_x0020_Name. Also note that if your field names are longer that what SharePoint supports for field names, it will truncate the names. HOWEVER, when referencing the field names for the UpdateListItems method, you don’t need to reference
the ows_ prefix in the field names.
You can get the list and view GUIDS as well as the field names by using the Stramit CAML Viewer (http://www.codeplex.com/SPCamlViewer) or the GUID Picker (http://blogs.msdn.com/ronalus/archive/2007/09/08/a-little-guid-picker.aspx)
For another way to get the field names, see the section Another Trick to Get the Field Names: Editing the Field Name in SharePoint in this document.
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 7 of 18 Written on 12/9/2008
In this illustration, the list’s field names are the following: Title
Favorite_x0020_Python
Your information will vary depending on the list that you are using to access the information. Now we’re ready to make a code change.
1) Move your cursor to the beginning of the coding block for the Main method and press Enter to go
to the next line. 2) Add the following line: int itemCounter = 0;
The beginning of your Main method should look similar to this: 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.
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();
4) Add the following line to the code:
itemCounter++;
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(); Your foreach code block should look similar to this:
foreach (XmlNode node in xl)
{
/*
* In this area, this is where you put the code to access the 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
* ows_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.
*/
String responseNumber = node.Attributes["ows_MetaInfo"].InnerText;
String fname = node.Attributes["ows_What_x0020_is_x0020_Your_x0020_N"].InnerText;
String quest = node.Attributes["ows_What_x0020_is_x0020_your_x0020_q"].InnerText;
String python = node.Attributes["ows_Who_x0020_is_x0020_your_x0020_fa"].InnerText;
/*
* 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
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 8 of 18 Written on 12/9/2008
* 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>", "");
Console.WriteLine("Response #: " + responseNumber);
Console.WriteLine("What is your name? " + fname);
Console.WriteLine("What is your quest? " + quest);
Console.WriteLine("Who is your favorite Python? " + python);
Console.WriteLine();
itemCounter++;
/*
* 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:
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; 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);
Remember to substitute the GUID values in this example with the List and View GUID of your list.
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 9 of 18 Written on 12/9/2008
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. Your foreach code block should look similar to this: foreach (XmlNode node in xl)
{
/*
* In this area, this is where you put the code to access the 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
* ows_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.
*/
String responseNumber = node.Attributes["ows_MetaInfo"].InnerText;
String fname = node.Attributes["ows_What_x0020_is_x0020_Your_x0020_N"].InnerText;
String quest = node.Attributes["ows_What_x0020_is_x0020_your_x0020_q"].InnerText;
String python = node.Attributes["ows_Who_x0020_is_x0020_your_x0020_fa"].InnerText;
/*
* 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>", "");
Console.WriteLine("Response #: " + responseNumber);
Console.WriteLine("What is your name? " + fname);
Console.WriteLine("What is your quest? " + quest);
Console.WriteLine("Who is your favorite Python? " + python);
Console.WriteLine();
itemCounter++;
/*
* 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;
XmlNode ndReturn = listService.UpdateListItems(comboListGUID, elBatch);
Console.WriteLine("************************************************************");
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 10 of 18 Written on 12/9/2008
Console.WriteLine("Response from adding " + fname + ": " + ndReturn.InnerXml);
Console.WriteLine("************************************************************");
Console.WriteLine();
}
Your entire source code should look similar to this: using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Xml;
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, "");
// Get the records from the list
XmlDocument doc = new XmlDocument();
String temp = activeItemData.InnerXml.Replace("\r\r", "");
doc.LoadXml(temp);
XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
nsmgr.AddNamespace("z", "#RowsetSchema");
nsmgr.AddNamespace("rs", "urn:schemas-microsoft-com:rowset");
XmlNodeList xl = doc.SelectNodes("/rs:data/z:row", nsmgr);
foreach (XmlNode node in xl)
{
/*
* In this area, this is where you put the code to access the 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 ows_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.
*/
String responseNumber = node.Attributes["ows_MetaInfo"].InnerText;
String fname =
node.Attributes["ows_What_x0020_is_x0020_Your_x0020_N"].InnerText;
String quest =
node.Attributes["ows_What_x0020_is_x0020_your_x0020_q"].InnerText;
String python =
node.Attributes["ows_Who_x0020_is_x0020_your_x0020_fa"].InnerText;
/*
* If you don't have extraneous characters to remove from the values from
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 11 of 18 Written on 12/9/2008
* 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>", "");
Console.WriteLine("Response #: " + responseNumber);
Console.WriteLine("What is your name? " + fname);
Console.WriteLine("What is your quest? " + quest);
Console.WriteLine("Who is your favorite Python? " + python);
Console.WriteLine();
itemCounter++;
/*
* 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;
XmlNode ndReturn = listService.UpdateListItems(comboListGUID, elBatch);
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);
}
// Prompts the user to "press a key" when finished
Console.WriteLine("Press any key to continue");
Console.ReadLine();
}
}
}
Compile and test your program by clicking the start button at the top of your toolbar. Your results should look similar to this:
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 12 of 18 Written on 12/9/2008
To confirm that the items have been added to the list, open the list in SharePoint.
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
Page 14 of 18 Written on 12/9/2008
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
2. Select Actions – Edit in Datasheet
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
3. Right-click on any of the fields 4. Select Edit/Delete Column
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 16 of 18 Written on 12/9/2008
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.
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 17 of 18 Written on 12/9/2008
Appendix: Source Code using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Xml;
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, "");
// Get the records from the list
XmlDocument doc = new XmlDocument();
String temp = activeItemData.InnerXml.Replace("\r\r", "");
doc.LoadXml(temp);
XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
nsmgr.AddNamespace("z", "#RowsetSchema");
nsmgr.AddNamespace("rs", "urn:schemas-microsoft-com:rowset");
XmlNodeList xl = doc.SelectNodes("/rs:data/z:row", nsmgr);
foreach (XmlNode node in xl)
{
/*
* In this area, this is where you put the code to access the 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 ows_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.
*/
String responseNumber = node.Attributes["ows_MetaInfo"].InnerText;
String fname =
node.Attributes["ows_What_x0020_is_x0020_Your_x0020_N"].InnerText;
String quest =
node.Attributes["ows_What_x0020_is_x0020_your_x0020_q"].InnerText;
String python =
node.Attributes["ows_Who_x0020_is_x0020_your_x0020_fa"].InnerText;
/*
* 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>", "");
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 18 of 18 Written on 12/9/2008
quest = System.Text.RegularExpressions.Regex.Replace(quest, "</div>", "");
Console.WriteLine("Response #: " + responseNumber);
Console.WriteLine("What is your name? " + fname);
Console.WriteLine("What is your quest? " + quest);
Console.WriteLine("Who is your favorite Python? " + python);
Console.WriteLine();
itemCounter++;
/*
* 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;
XmlNode ndReturn = listService.UpdateListItems(comboListGUID, elBatch);
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);
}
// Prompts the user to "press a key" when finished
Console.WriteLine("Press any key to continue");
Console.ReadLine();
}
}
}