/// <summary> /// Creates the item. /// </summary> /// <param name="title">Title.</param> /// <param name="fieldValues">Field values.</param> /// <returns></returns> public SharePointListItem CreateItem(string title, Hashtable fieldValues) { // construct batch XmlDocument doc = new XmlDocument(); XmlElement xmlBatch = doc.CreateElement("Batch"); xmlBatch.SetAttribute("OnError", "Continue"); xmlBatch.SetAttribute("ListVersion", version); StringBuilder sb = new StringBuilder(); sb.Append("<Method ID='1' Cmd='New'><Field Name='ID'>New</Field>"); sb.AppendFormat("<Field Name='Title'>{0}</Field>", title); if (null != fieldValues) { foreach (string key in fieldValues.Keys) { // Determine the type of the value being set // and optionally add the appropriate XML string fieldEntry = ""; // TODO: maybe override hashtable to get key, value pair? // Add any new types you want to support here. There must be a // better way to do this! if (fieldValues[key] is DateTime) { // Have to format DateTime into ISO8601 format // (yyyy-mm-ddThh:mm:ssZ) DateTime dtUTC = (DateTime)fieldValues[key]; string dateTimeUTC = dtUTC.ToString("yyyy-MM-ddTHH:mm:ssZ"); fieldEntry = String.Format("<Field Name='{0}'>{1}</Field>", key, dateTimeUTC); } else { // default to string with no type fieldEntry = String.Format("<Field Name='{0}'>{1}</Field>", key, fieldValues[key]); } sb.Append(fieldEntry); } } sb.Append("</Method>"); xmlBatch.InnerXml = sb.ToString(); // submit batch update ListsWS.Lists lws = NewListsWebService(); XmlNode result = lws.UpdateListItems(listName, xmlBatch); // check result return value // Correct way to do this is probably using XPath, but doing things // like result.SelectSingleNode("Result/ErrorCode") barf, with // or without an XmlNamespaceManager. If you can figure it out // feel free to change it. string resultCode = result.SelectSingleNode("*/*").InnerText.ToString(); if ("0x00000000" != resultCode) { return(null); } // OK. Construct new listitem long id = long.Parse(Regex.Match(result.InnerXml, "ows_ID=\"(.*?)\"").Groups[1].Value); Guid guid = new Guid(Regex.Match(result.InnerXml, "ows_GUID=\"(.*?)\"").Groups[1].Value); SharePointListItem item = new SharePointListItem(this, id, guid); item.Credentials = Credentials; return(item); }
/// <summary> /// Creates the item. /// </summary> /// <param name="title">Title.</param> /// <param name="fieldValues">Field values.</param> /// <returns></returns> public SharePointListItem CreateItem(string title, Hashtable fieldValues) { // construct batch XmlDocument doc = new XmlDocument(); XmlElement xmlBatch = doc.CreateElement("Batch"); xmlBatch.SetAttribute("OnError", "Continue"); xmlBatch.SetAttribute("ListVersion", version); StringBuilder sb = new StringBuilder(); sb.Append("<Method ID='1' Cmd='New'><Field Name='ID'>New</Field>"); sb.AppendFormat("<Field Name='Title'>{0}</Field>", title); if (null != fieldValues) { foreach (string key in fieldValues.Keys) { // Determine the type of the value being set // and optionally add the appropriate XML string fieldEntry = ""; // TODO: maybe override hashtable to get key, value pair? // Add any new types you want to support here. There must be a // better way to do this! if(fieldValues[key] is DateTime) { // Have to format DateTime into ISO8601 format // (yyyy-mm-ddThh:mm:ssZ) DateTime dtUTC = (DateTime)fieldValues[key]; string dateTimeUTC = dtUTC.ToString("yyyy-MM-ddTHH:mm:ssZ"); fieldEntry = String.Format("<Field Name='{0}'>{1}</Field>", key, dateTimeUTC); } else { // default to string with no type fieldEntry = String.Format("<Field Name='{0}'>{1}</Field>", key, fieldValues[key]); } sb.Append(fieldEntry); } } sb.Append("</Method>"); xmlBatch.InnerXml = sb.ToString(); // submit batch update ListsWS.Lists lws = NewListsWebService(); XmlNode result = lws.UpdateListItems(listName, xmlBatch); // check result return value // Correct way to do this is probably using XPath, but doing things // like result.SelectSingleNode("Result/ErrorCode") barf, with // or without an XmlNamespaceManager. If you can figure it out // feel free to change it. string resultCode = result.SelectSingleNode("*/*").InnerText.ToString(); if ("0x00000000" != resultCode) return null; // OK. Construct new listitem long id = long.Parse(Regex.Match(result.InnerXml, "ows_ID=\"(.*?)\"").Groups[1].Value); Guid guid = new Guid(Regex.Match(result.InnerXml, "ows_GUID=\"(.*?)\"").Groups[1].Value); SharePointListItem item = new SharePointListItem(this, id, guid); item.Credentials = Credentials; return item; }