ARTICLES

Home  > Articles  >  Programmatically creating/updating list items on remote SharePoint server
Here is the scenario: You have a client application or Office application add-in that needs to write data to the SharePoint server. Obviously your server is hosted somewhere else but on your client machine; but you have all of the required login credentials to perform write on the sub-site of your interest. How do you go about creating/updating items on your SharePoint server from this client application?

If you have been creating workflows, web parts, or event handlers before you probably don’t see much complexity in creating a list item in the SharePoint list. However, if your code runs locally on the server the situation is way different than having the code run remotely and communicate back to the server. In this case you need to use web services and although they are not meant to be complicated – it’s often than not cumbersome to understand how to work with.

The approach: We’ll be using console application to avoid un-necessary complication of things. I recommend to have the logic that executes actual submission to a SharePoint web service separate from the business logic; therefore I will compile my web service communication wrapper as a library and add it as my reference in console application.
 
static void Main(string[] args)
        {
            const string SERVER_URL = "http://intranet";
            DocLibHelper docLibHelper = new DocLibHelper();
            Dictionary
In this case docLibHelper is an instance of helper object that will handle web service submission logic. At the core of this logic we have the following function that handles submission to the list:

private bool TryToInsert(SPList list, ICredentials credentials, Dictionary
In the above example we get a hold of the list and item information that we are about to insert and execute a web service call updating/inserting the list item. Below is how we upload the file to the document library.

private bool TryToUpload(FileInfo fileInfo, ICredentials credentials)
        {
            try
            {
                WebRequest request = WebRequest.Create(fileInfo.m_URL);
                request.Credentials = credentials;
                request.Method = "PUT";
                byte[] buffer = new byte[1024];
                using (Stream stream = request.GetRequestStream())
                using (MemoryStream ms = new MemoryStream(fileInfo.m_bytes))
                    for (int i = ms.Read(buffer, 0, buffer.Length); i > 0; i = ms.Read(buffer, 0, buffer.Length))
                        stream.Write(buffer, 0, i);
                WebResponse response = request.GetResponse();
                response.Close();
                if (fileInfo.HasProperties)
                {
                    StringBuilder sb = new StringBuilder();
                    sb.Append("");                     sb.AppendFormat("{0}", fileInfo.m_URL);                     foreach (KeyValuePair");
                    System.Xml.XmlElement updates = (new System.Xml.XmlDocument()).CreateElement("Batch");
                    updates.SetAttribute("OnError", "Continue");
                    updates.SetAttribute("ListVersion", fileInfo.m_listInfo.m_version);
                    updates.SetAttribute("PreCalc", "TRUE");
                    updates.InnerXml = sb.ToString();
                    m_listService.Url = fileInfo.m_listInfo.m_webUrl + "/_vti_bin/Lists.asmx";
                    XmlNode updatesResponse = m_listService.UpdateListItems(fileInfo.m_listInfo.m_listName, updates);
                    if (updatesResponse.FirstChild.FirstChild.InnerText != "0x00000000")
                        throw new Exception("Could not update properties.");
                }
                return true;
            }
            catch (WebException)
            {
                return false;
            }

This routine utilizes some of the internal structures to carry over the information about the file such as properties and binary data. This example also handles versioning if versioning is enabled on the destination library. Here is the actual working example of console application and accompanying helper DLL.

This solution will definitely get you started on working with web services in your scenario: http://www.sharemuch.com/wp-content/uploads/2009/06/webservices.zip


If you have any questions, drop me a note using Contact  section at www.sharemuch.com