// Stephen Toub // stoub@microsoft.com using System; using System.Xml; using System.Collections; using System.Web.Services; namespace Msdn.Outlook.Calendaring { /// Allows for the generation of list responses as the SharePoint Lists service would do. public class WssListResponse { #region Constants /// Web service namespace. private const string _wssns = "http://schemas.microsoft.com/sharepoint/soap/"; /// xmlns namespace. private const string _xmlns = "http://www.w3.org/2000/xmlns/"; #endregion #region Member Variables /// The root document of the list response. private XmlDocument _doc; /// The list items element in the response. private XmlElement _listItemsNode; /// A list of all events to be added to the response. private ArrayList _events; #endregion #region Construction /// Initialize the generator. public WssListResponse() { // Create the array to hold the events _events = new ArrayList(); // Create and configure the base document _doc = new XmlDocument(); _listItemsNode = _doc.CreateElement(null, "listitems", _wssns); _doc.AppendChild(_listItemsNode); // Add all of the namespace and timestamp information. AddAttribute(_listItemsNode, "xmlns", "s", _xmlns, "uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882"); AddAttribute(_listItemsNode, "xmlns", "dt", _xmlns, "uuid:C2F41010-65B3-11d1-A29F-00AA00C14882"); AddAttribute(_listItemsNode, "xmlns", "rs", _xmlns, "urn:schemas-microsoft-com:rowset"); AddAttribute(_listItemsNode, "xmlns", "z", _xmlns, "#RowsetSchema"); AddAttribute(_listItemsNode, "xmlns", "s", _xmlns, "uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882"); AddAttribute(_listItemsNode, null, "TimeStamp", null, DateTime.UtcNow.ToString("s") + "Z"); } #endregion #region Xml Generation /// /// Adds a Field node to the Fields parent node /// /// XmlDocument parent doc used for creating the new element /// The parent Fields node to which to add the new Field element /// Type attribute /// ColName attribute /// Name attribute /// DisplayName attribute /// ReadOnly attribute /// Hidden attribute /// FromBaseType attribute /// private static XmlElement AddFieldElement(XmlDocument parentDoc, XmlNode parentNode, string type, string colName, string name, string displayName, bool readOnly, bool hidden, bool fromBaseType) { XmlElement fieldEl = parentDoc.CreateElement(null, "Field", _wssns); parentNode.AppendChild(fieldEl); if (type != null) AddAttribute(fieldEl, null, "Type", null, type); if (colName != null) AddAttribute(fieldEl, null, "ColName", null, colName); if (name != null) AddAttribute(fieldEl, null, "Name", null, name); if (displayName != null) AddAttribute(fieldEl, null, "DisplayName", null, displayName); if (readOnly) AddAttribute(fieldEl, null, "ReadOnly", null, "TRUE"); if (hidden) AddAttribute(fieldEl, null, "Hidden", null, "TRUE"); if (fromBaseType) AddAttribute(fieldEl, null, "FromBaseType", null, "TRUE"); return fieldEl; } /// Creates an XML description of a particular list. Suitable to be returned from Lists.asmx/GetList. /// The name of the list. /// The ID of the list. /// public static XmlNode GetListDescription(string title, Guid id) { // Create the response document XmlDocument doc = new XmlDocument(); // Add a List element XmlNode list = doc.CreateElement(null, "List", _wssns); doc.AppendChild(list); // Add attributes about the list to the List element AddAttribute(list, null, "DocTemplateUrl", null, ""); AddAttribute(list, null, "DefaultViewUrl", null, "/Lists/" + id.ToString("N") + "/AllItems.aspx"); AddAttribute(list, null, "ID", null, id.ToString("B")); AddAttribute(list, null, "Title", null, title); AddAttribute(list, null, "Description", null, title); AddAttribute(list, null, "ImageUrl", null, "/_layouts/images/itevent.gif"); AddAttribute(list, null, "Name", null, id.ToString("N")); AddAttribute(list, null, "BaseType", null, "0"); AddAttribute(list, null, "ServerTemplate", null, "106"); AddAttribute(list, null, "Created", null, DateTime.MinValue.AddDays(2).ToString("s").Replace("T", " ")); AddAttribute(list, null, "Modified", null, DateTime.MinValue.AddDays(3).ToString("s").Replace("T", " ")); AddAttribute(list, null, "LastDeleted", null, DateTime.MinValue.AddDays(3).ToString("s").Replace("T", " ")); AddAttribute(list, null, "Version", null, "0"); AddAttribute(list, null, "Direction", null, "none"); AddAttribute(list, null, "ThumbnailSize", null, ""); AddAttribute(list, null, "WebImageWidth", null, ""); AddAttribute(list, null, "WebImageHeight", null, ""); AddAttribute(list, null, "Flags", null, "4096"); AddAttribute(list, null, "ItemCount", null, "1"); // isn't used, so no point in recomputing size of list AddAttribute(list, null, "AnonymousPermMask", null, ""); AddAttribute(list, null, "RootFolder", null, "/Lists/" + id.ToString("N")); AddAttribute(list, null, "ReadSecurity", null, "1"); AddAttribute(list, null, "WriteSecurity", null, "1"); AddAttribute(list, null, "Author", null, "1"); AddAttribute(list, null, "AnonymousPermMask", null, ""); AddAttribute(list, null, "EventSinkAssembly", null, ""); AddAttribute(list, null, "EventSinkClass", null, ""); AddAttribute(list, null, "EventSinkData", null, ""); AddAttribute(list, null, "EmailInsertsFolder", null, ""); AddAttribute(list, null, "AllowDeletion", null, "TRUE"); AddAttribute(list, null, "AllowMultiResponses", null, "FALSE"); AddAttribute(list, null, "EnableAttachments", null, "TRUE"); AddAttribute(list, null, "EnableModeration", null, "FALSE"); AddAttribute(list, null, "EnableVersioning", null, "FALSE"); AddAttribute(list, null, "Hidden", null, "FALSE"); AddAttribute(list, null, "MultipleDataList", null, "FALSE"); AddAttribute(list, null, "Ordered", null, "FALSE"); AddAttribute(list, null, "ShowUser", null, "TRUE"); //Create Fields node XmlElement fieldsNode = doc.CreateElement(null, "Fields", _wssns); list.AppendChild(fieldsNode); XmlElement tmpEl; //Append Fields tmpEl = AddFieldElement(doc, fieldsNode, "Counter", "tp_ID", "ID", "ID", true, false, true); tmpEl = AddFieldElement(doc, fieldsNode, "Text", "nvarchar1", "Title", "Title", false, false, true); tmpEl = AddFieldElement(doc, fieldsNode, "DateTime", "tp_Modified", "Modified", "Modified", true, false, true); AddAttribute(tmpEl, null, "StorageTZ", null, "TRUE"); tmpEl = AddFieldElement(doc, fieldsNode, "DateTime", "tp_Created", "Created", "Created", true, false, true); AddAttribute(tmpEl, null, "StorageTZ", null, "TRUE"); tmpEl = AddFieldElement(doc, fieldsNode, "User", "tp_Author", "Author", "Created By", true, false, true); AddAttribute(tmpEl, null, "List", null, "UserInfo"); tmpEl = AddFieldElement(doc, fieldsNode, "User", "tp_Editor", "Editor", "Modified By", true, false, true); AddAttribute(tmpEl, null, "List", null, "UserInfo"); tmpEl = AddFieldElement(doc, fieldsNode, "Integer", "tp_Version", "owshiddenversion", "owshiddenversion", true, true, true); AddAttribute(tmpEl, null, "SetAs", null, "owshiddenversion"); // tmpEl = AddFieldElement(doc,fieldsNode,"Attachments","tp_HasAttachment","Attachments","Attachments",true,false,true); // tmpEl = AddFieldElement(doc,fieldsNode,"ModStat","tp_ModerationStatus","_ModerationStatus","Approval Status",true,true,true); // AddAttribute(tmpEl,null,"CanToggleHidden",null,"TRUE"); // AddAttribute(tmpEl,null,"Required",null,"FALSE"); // tmpEl = AddFieldElement(doc,fieldsNode,"Note","ntext1","_ModerationComments","Approver Comments",true,true,true); // AddAttribute(tmpEl,null,"CanToggleHidden",null,"TRUE"); // AddAttribute(tmpEl,null,"Sortable",null,"FALSE"); // tmpEl = AddFieldElement(doc,fieldsNode,"Computed",null,"Edit","Edit",true,false,true); // AddAttribute(tmpEl,null,"Sortable",null,"FALSE"); // AddAttribute(tmpEl,null,"Filterable",null,"FALSE"); // AddAttribute(tmpEl,null,"AuthoringInfo",null,"(link to edit item)"); // tmpEl = AddFieldElement(doc,fieldsNode,"Computed",null,"LinkTitleNoMenu","Title",true,false,true); // AddAttribute(tmpEl,null,"AuthoringInfo",null,"(linked to item)"); // AddAttribute(tmpEl,null,"Dir",null,""); // AddAttribute(tmpEl,null,"DisplayNameSrcField",null,"Title"); // tmpEl = AddFieldElement(doc,fieldsNode,"Computed",null,"LinkTitle","Title",true,false,true); // AddAttribute(tmpEl,null,"AuthoringInfo",null,"(linked to item with edit menu)"); // AddAttribute(tmpEl,null,"DisplayNameSrcField",null,"Title"); // AddAttribute(tmpEl,null,"ClassInfo",null,"Menu"); // tmpEl = AddFieldElement(doc,fieldsNode,"Computed",null,"SelectTitle","Select",true,true,true); // AddAttribute(tmpEl,null,"AuthoringInfo",null,"(web part connection)"); // AddAttribute(tmpEl,null,"Dir",null,""); // AddAttribute(tmpEl,null,"Sortable",null,"FALSE"); // AddAttribute(tmpEl,null,"CanToggleHidden",null,"TRUE"); // tmpEl = AddFieldElement(doc,fieldsNode,"Integer","tp_InstanceID","InstanceID","InstanceID",true,true,true); // AddAttribute(tmpEl,null,"Sortable",null,"TRUE"); // AddAttribute(tmpEl,null,"Filterable",null,"TRUE"); // AddAttribute(tmpEl,null,"Min",null,"0"); // AddAttribute(tmpEl,null,"Max",null,"99991231"); // tmpEl = AddFieldElement(doc,fieldsNode,"Number","tp_ItemOrder","Order","Order",false,true,true); tmpEl = AddFieldElement(doc, fieldsNode, "Guid", "tp_Guid", "GUID", "GUID", true, true, true); tmpEl = AddFieldElement(doc, fieldsNode, "DateTime", "datetime1", "EventDate", "Begin", false, false, true); AddAttribute(tmpEl, null, "Format", null, "DateTime"); AddAttribute(tmpEl, null, "Sealed", null, "TRUE"); AddAttribute(tmpEl, null, "Required", null, "TRUE"); AddAttribute(tmpEl, null, "Filterable", null, "FALSE"); AddAttribute(tmpEl, null, "FilterableNoRecurrence", null, "TRUE"); tmpEl = AddFieldElement(doc, fieldsNode, "DateTime", "datetime2", "EndDate", "End", false, false, true); AddAttribute(tmpEl, null, "Format", null, "DateTime"); AddAttribute(tmpEl, null, "Sealed", null, "TRUE"); AddAttribute(tmpEl, null, "Filterable", null, "FALSE"); AddAttribute(tmpEl, null, "FilterableNoRecurrence", null, "TRUE"); tmpEl = AddFieldElement(doc, fieldsNode, "Note", "ntext2", "Description", "Description", false, false, false); AddAttribute(tmpEl, null, "Sealed", null, "TRUE"); AddAttribute(tmpEl, null, "Sortable", null, "False"); tmpEl = AddFieldElement(doc, fieldsNode, "Text", "nvarchar", "Location", "Location", false, false, false); AddAttribute(tmpEl, null, "Sealed", null, "TRUE"); tmpEl = AddFieldElement(doc, fieldsNode, "Recurrence", "bit1", "fRecurrence", "Recurrence", false, false, false); AddAttribute(tmpEl, null, "DisplayImage", null, "recur.gif"); AddAttribute(tmpEl, null, "HeaderImage", null, "recur.gif"); AddAttribute(tmpEl, null, "ClassInfo", null, "Icon"); AddAttribute(tmpEl, null, "Sealed", null, "TRUE"); AddAttribute(tmpEl, null, "NoEditFormBreak", null, "TRUE"); // tmpEl = AddFieldElement(doc,fieldsNode,"CrossProjectLink","bit2","WorkspaceLink","Workspace",false,false,false); // AddAttribute(tmpEl,null,"Format",null,"EventList"); // AddAttribute(tmpEl,null,"DisplayImage",null,"mtgicon.gif"); // AddAttribute(tmpEl,null,"HeaderImage",null,"mtgicnhd.gif"); // AddAttribute(tmpEl,null,"ClassInfo",null,"Icon"); // AddAttribute(tmpEl,null,"Title",null,"Meeting Workspace"); // AddAttribute(tmpEl,null,"Filterable",null,"TRUE"); // AddAttribute(tmpEl,null,"Sealed",null,"TRUE"); tmpEl = AddFieldElement(doc, fieldsNode, "Integer", "int1", "EventType", "Event Type", false, true, false); AddAttribute(tmpEl, null, "Sealed", null, "TRUE"); tmpEl = AddFieldElement(doc, fieldsNode, "Guid", "uniqueidentifier1", "UID", "UID", false, true, false); AddAttribute(tmpEl, null, "Sealed", null, "TRUE"); tmpEl = AddFieldElement(doc, fieldsNode, "DateTime", "datetime3", "RecurrenceID", "Recurrence ID", false, true, false); AddAttribute(tmpEl, null, "Sealed", null, "TRUE"); AddAttribute(tmpEl, null, "CalType", null, "1"); AddAttribute(tmpEl, null, "Format", null, "ISO8601Gregorian"); tmpEl = AddFieldElement(doc, fieldsNode, "Boolean", "bit3", "EventCanceled", "Event Canceled", false, true, false); AddAttribute(tmpEl, null, "Sealed", null, "TRUE"); tmpEl = AddFieldElement(doc, fieldsNode, "Integer", "int2", "Duration", "Duration", false, true, false); AddAttribute(tmpEl, null, "Sealed", null, "TRUE"); tmpEl = AddFieldElement(doc, fieldsNode, "Note", "ntext3", "RecurrenceData", "RecurrenceData", false, true, false); AddAttribute(tmpEl, null, "Sealed", null, "TRUE"); tmpEl = AddFieldElement(doc, fieldsNode, "Integer", "int3", "TimeZone", "TimeZone", false, true, false); AddAttribute(tmpEl, null, "Sealed", null, "TRUE"); tmpEl = AddFieldElement(doc, fieldsNode, "Note", "ntext4", "XMLTZone", "XMLTZone", false, true, false); AddAttribute(tmpEl, null, "Sealed", null, "TRUE"); // tmpEl = AddFieldElement(doc,fieldsNode,"Integer","int4","MasterSeriesItemID","MasterSeriesItemID",false,true,false); // AddAttribute(tmpEl,null,"Sealed",null,"TRUE"); // tmpEl = AddFieldElement(doc,fieldsNode,"URL","nvarchar3","Workspace","WorkspaceUrl",false,true,false); // AddAttribute(tmpEl,null,"Sealed",null,"TRUE"); // AddAttribute(tmpEl,null,"ColName2",null,"nvarchar4"); // Return the XML return doc; } /// Adds an attribute to an XML node. /// The node. /// The prefix of the attribute. /// The name of the attribute. /// The namespace of the attribute. /// The value of the attribute. private static void AddAttribute(XmlNode node, string prefix, string name, string ns, object value) { XmlAttribute attr = node.OwnerDocument.CreateAttribute(prefix, name, ns); attr.Value = value.ToString(); node.Attributes.Append(attr); } /// Adds an event to the response document. /// The ID of the event. /// The title of the event. /// The description of the event. /// The location of the event. /// The author of the event. /// The start time of the event in UTC. /// The end time of the event in UTC. public void AddEvent(int id, string title, string description, string location, string author, DateTime start, DateTime end) { XmlElement node = _doc.CreateElement("z", "row", "#RowsetSchema"); author = "1;#" + author; AddAttribute(node, null, "ows_ID", null, id.ToString()); AddAttribute(node, null, "ows_Title", null, title); AddAttribute(node, null, "ows_Modified", null, DateTime.MinValue.AddDays(2).ToString("s") + "Z"); AddAttribute(node, null, "ows_Created", null, DateTime.MinValue.AddDays(2).ToString("s") + "Z"); AddAttribute(node, null, "ows_Author", null, author); AddAttribute(node, null, "ows_Editor", null, author); AddAttribute(node, null, "ows_owshiddenversion", null, "1"); AddAttribute(node, null, "ows_Attachments", null, "0"); AddAttribute(node, null, "ows__ModerationStatus", null, "0"); AddAttribute(node, null, "ows_LinkTitleNoMenu", null, title); AddAttribute(node, null, "ows_LinkTitle", null, title); AddAttribute(node, null, "ows_SelectTitle", null, title); AddAttribute(node, null, "ows_Order", null, id + "00.000000000000"); AddAttribute(node, null, "ows_GUID", null, Guid.NewGuid().ToString("B")); AddAttribute(node, null, "ows_EventDate", null, start.ToString("s") + "Z"); AddAttribute(node, null, "ows_EndDate", null, end.ToString("s") + "Z"); AddAttribute(node, null, "ows_Description", null, description); AddAttribute(node, null, "ows_Location", null, location); AddAttribute(node, null, "ows_fRecurrence", null, "0"); AddAttribute(node, null, "ows_EventType", null, "0"); AddAttribute(node, null, "ows_Duration", null, (end - start).TotalSeconds.ToString()); _events.Add(node); } /// Completes the list and returns the response XML data. /// The response XML data. public XmlNode FinishXml() { XmlNode rowset1 = _doc.CreateElement("rs", "data", "urn:schemas-microsoft-com:rowset"); AddAttribute(rowset1, null, "ItemCount", null, _events.Count.ToString()); XmlNode rowset2 = _doc.CreateElement("rs", "data", "urn:schemas-microsoft-com:rowset"); AddAttribute(rowset2, null, "ItemCount", null, _events.Count.ToString()); foreach(XmlNode node in _events) { rowset1.AppendChild(node); XmlNode shadowEventNode = _doc.CreateElement("r", "row", "#RowsetSchema"); AddAttribute(shadowEventNode, null, "ows_ID", null, node.Attributes["ows_ID"].Value); AddAttribute(shadowEventNode, null, "ows_owshiddenversion", null, node.Attributes["ows_owshiddenversion"].Value); rowset2.AppendChild(shadowEventNode); } _listItemsNode.AppendChild(rowset1); _listItemsNode.AppendChild(rowset2); return _doc; } #endregion } }