Add An Item To A Custom SharePoint List Using Batch XML With A Web Service (C#)

If you have multiple active directory environments and SharePoint instances in both of those environments then you will understand why I posted this.

I received a requirement that while users from a separate domain and SharePoint instance would need to fill out a form on their SharePoint and have the form submission be sent to the other SharePoint instance (on the other domain). This was because they did not want to mix and mingle with forest trusting or having users to have two separate AD accounts to manage the information. Say what?!

After some initial discovery I had reached a couple quick conclusions:

– SharePoints on different domains do not talk well to each other without trust

– Using the built-in lists service on SharePoint has to be done on the server itself unless you are going to have an instance stood up somewhere on the same domain that has all the required SharePoint dlls to be referenced

All being said, here is an example of what I used as a web service I placed on the SharePoint server that calls the lists web service doing a list add using batch XML:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Client;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint.Administration.Claims;
using Microsoft.SharePoint.WebControls;

namespace CopyOverItemFromOtherSP
{

{
public string CopyOverFromOtherDomain(string userName, string Name, string userEmail,
string userPhone, string listColumn1, string listColumn2, string listColumn3,
string Comments)
{
Boolean done = false;

/* Declare and initialize a variable for the Lists Web service. */
sitesWebServiceLists.Lists listService = new sitesWebServiceLists.Lists();

/* Authenticate the current user by passing their default credentials to the Web service from the system credential cache. */
listService.Credentials = System.Net.CredentialCache.DefaultCredentials;

/* Set the Url property of the service for the path to a subsite. */
// Service URLs - MUST CHANGE TO POINT TO SPECIFIC SERVICE ENVIRONMENT
// Change the web reference URL on the sitesWebServiceLists web service reference to be the address
// that you are wanting to used based on the environment that you want to write to
listService.Url = "<a href="#">http://sharepointtocopyitemto/_vti_bin/Lists.asmx</a>";

/* Get Name attribute values (GUIDs) for list and view. */
System.Xml.XmlNode ndListView = listService.GetListAndView("NameOfSharePointList", "");
string strListID = ndListView.ChildNodes[0].Attributes["Name"].Value;
string strViewID = ndListView.ChildNodes[1].Attributes["Name"].Value;

/* Create an XmlDocument object and construct a Batch element and its
attributes. Note that an empty ViewName parameter causes the method to use the default view. */
System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
System.Xml.XmlElement batchElement = doc.CreateElement("Batch");
batchElement.SetAttribute("OnError", "Continue");
batchElement.SetAttribute("ListVersion", "1");
batchElement.SetAttribute("ViewName", strViewID);

/* Specify methods for the batch post using CAML. To update or delete,
specify the ID of the item, and to update or add, specify
the value to place in the specified column. */
batchElement.InnerXml = "<Method ID='1' Cmd='New'>" +
"<Field Name='ID'>New</Field>" +
"<Field Name='Title'>" + userName.ToString() + "</Field>" +
"<Field Name='Name'>" + Name.ToString() + "</Field>" +
"<Field Name='userEmail'>" + userEmail.ToString() + "</Field>" +
"<Field Name='userPhone'>" + userPhone.ToString() + "</Field>" +
"<Field Name='listColumn1'>" + listColumn1.ToString() + "</Field>" +
"<Field Name='listColumn2'>" + listColumn2.ToString() + "</Field>" +
"<Field Name='listColumn3'>" + listColumn3.ToString() + "</Field>" +
"<Field Name='Comments'>" + Comments.ToString() + "</Field>" +
"</Method>";

/* Update list items. This example uses the list GUID, which is recommended,
but the list display name will also work. */
try
{
listService.UpdateListItems(strListID, batchElement);
done = true;
}
catch
{

}
if (done == true)
{
return done.ToString();
}
else
{
return done.ToString();
}
}
}
}
Advertisements