PageRenderTime 75ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/StsSync/ProviderProxy/Service.asmx.cs

http://stssyncprovider.codeplex.com
C# | 570 lines | 417 code | 103 blank | 50 comment | 49 complexity | 8b7d255debfb0ed394a95b78e936e26f MD5 | raw file
  1. using System;
  2. using System.Collections;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Linq;
  6. using System.Web;
  7. using System.Web.Services;
  8. using System.Web.Services.Protocols;
  9. using System.Xml.Linq;
  10. using System.Xml;
  11. using System.Collections.Generic;
  12. using System.Collections.Specialized;
  13. using System.Diagnostics;
  14. namespace ProviderProxy
  15. {
  16. /// <summary>
  17. /// Summary description for WebService1
  18. /// </summary>
  19. [WebService(Namespace = "http://schemas.microsoft.com/sharepoint/soap/")]
  20. //[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
  21. [ToolboxItem(false)]
  22. [System.Web.Script.Services.ScriptService]
  23. public class WebService1 : System.Web.Services.WebService,IListsSoap
  24. {
  25. public static readonly Guid WEB_ID = new Guid("{409F0498-67EA-4120-B5AC-A7FE87828967}");
  26. [WebMethod()]
  27. public string ATestMethod()
  28. {
  29. //ProviderManager pm = new ProviderManager();
  30. //IProvider prov = pm.GetProvider(new Guid("{7765B84F-6D32-4d31-B28E-6BC615D2F187}"));
  31. //ChangeKey ck = new ChangeKey("1; 1; 0ac863f6-3a07-4d5e-bd07-78bebb0a34f1; 633033246547900000; 217 ");
  32. //ChangeKey ck = new ChangeKey(1, 1, Guid.NewGuid(), DateTime.Now, 23);
  33. System.Text.StringBuilder sb = new System.Text.StringBuilder();
  34. //sb.AppendLine("ChangeEventType = " + ck.ChangeEventType.ToString());
  35. //sb.AppendLine("ChangeNumber = " + ck.ChangeNumber.ToString());
  36. //sb.AppendLine("ChangeObjectType = " + ck.ChangeObjectType.ToString());
  37. //sb.AppendLine("ChangeTime = " + ck.ChangeTime.ToString());
  38. //sb.AppendLine("ObjectGuid = " + ck.ObjectGuid.ToString());
  39. //sb.AppendLine("ChangeKey = " + ck.ToString());
  40. return null;
  41. }
  42. static XmlDocument _doc = null;
  43. static XmlNamespaceManager _nsm = null;
  44. static XmlNamespaceManager _snsm = null;
  45. static XmlNamespaceManager snsm
  46. {
  47. get
  48. {
  49. if (_snsm == null) { object throwaway = _Doc; }
  50. return _snsm;
  51. }
  52. }
  53. private static XmlDocument _Doc
  54. {
  55. get
  56. {
  57. if (_doc == null)
  58. {
  59. _doc = new XmlDocument();
  60. _nsm = new XmlNamespaceManager(_doc.NameTable);
  61. _nsm.AddNamespace(string.Empty, "http://schemas.microsoft.com/sharepoint/soap/");
  62. _snsm = new XmlNamespaceManager(_doc.NameTable);
  63. _snsm.AddNamespace("s", _nsm.DefaultNamespace);
  64. }
  65. return _doc;
  66. }
  67. }
  68. #region IListsSoap Members
  69. public System.Xml.XmlNode GetList(string listName)
  70. {
  71. //List name should correspond to the provider ID if the stssync link was constructed correctly.
  72. Guid listId = new Guid(listName); //we'll just crash if it's not a Guid
  73. XmlElement listElement = _Doc.CreateElement("List",_nsm.DefaultNamespace);
  74. ProviderManager pm = new ProviderManager();
  75. Provider prov = pm.GetProvider(listId);
  76. IProvider iProv = pm.GetIProvider(listId);
  77. ListsHelper.FillListAttributes(listName, listElement, prov, this.Context.Request.ApplicationPath);
  78. XmlElement fieldsElement = NewElement(listElement,"Fields",null,true);
  79. //listElement.AppendChild(fieldsElement);
  80. List<Field> fields = ListsHelper.GetFieldsForListType(iProv.GetProviderType(listId));
  81. foreach (Field field in fields)
  82. {
  83. //Works, but I'm going to try something else.
  84. //XmlElement fieldElement = _Doc.CreateElement("Field", listElement.NamespaceURI);
  85. //XmlDocument fDoc = new XmlDocument();
  86. //fDoc.LoadXml(field.FieldDefXml);
  87. //foreach (XmlAttribute attrib in fDoc.DocumentElement.Attributes)
  88. //{
  89. // fieldElement.Attributes.Append((XmlAttribute)_Doc.ImportNode(attrib.Clone(),true));
  90. //}
  91. //fieldsElement.AppendChild(fieldElement);
  92. fieldsElement.AppendChild(field.GetFieldDefXml(_Doc));
  93. }
  94. /*
  95. <RegionalSettings>
  96. <Language>1033</Language>
  97. <Locale>1033</Locale>
  98. <AdvanceHijri>0</AdvanceHijri>
  99. <CalendarType>1</CalendarType>
  100. <Time24>False</Time24>
  101. <TimeZone>300</TimeZone>
  102. <SortOrder>2070</SortOrder>
  103. <Presence>True</Presence>
  104. </RegionalSettings>
  105. <ServerSettings>
  106. <ServerVersion>12.0.0.6219</ServerVersion>
  107. <RecycleBinEnabled>True</RecycleBinEnabled>
  108. <ServerRelativeUrl>/sites/stssync</ServerRelativeUrl>
  109. </ServerSettings>
  110. */
  111. XmlElement regionalSettingsNode = NewElement(listElement, "RegionalSettings",null,true);
  112. NewElement(regionalSettingsNode, "Language", "1033");
  113. NewElement(regionalSettingsNode, "Locale", "1033");
  114. NewElement(regionalSettingsNode, "AdvanceHijri", "0");
  115. NewElement(regionalSettingsNode, "CalendarType", "1");
  116. NewElement(regionalSettingsNode, "Time24", "False");
  117. NewElement(regionalSettingsNode, "TimeZone", "300");
  118. NewElement(regionalSettingsNode, "SortOrder", "2070");
  119. NewElement(regionalSettingsNode, "Presence", "True");
  120. XmlElement serverSettings = NewElement(listElement, "ServerSettings",null,true);
  121. NewElement(serverSettings, "ServerVersion", "12.0.0.6219");
  122. NewElement(serverSettings, "RecycleBinEnabled", "True");
  123. NewElement(serverSettings, "ServerRelativeUrl", this.Context.Request.ApplicationPath);
  124. return listElement;
  125. }
  126. private XmlElement NewElement(XmlElement parentElement, string nodeName, string innerXml)
  127. {
  128. XmlElement retElement = NewElement(parentElement, nodeName, null,true);
  129. if (innerXml != null)
  130. {
  131. retElement.InnerText = innerXml;
  132. }
  133. return retElement;
  134. }
  135. private XmlElement NewElement(XmlElement parentElement, string nodeName, XmlElement refNode, bool appearBefore)
  136. {
  137. XmlElement retElement = NewElement(parentElement, nodeName);
  138. if (refNode != null)
  139. {
  140. if (appearBefore)
  141. parentElement.InsertBefore(retElement, refNode);
  142. else
  143. parentElement.InsertAfter(retElement, refNode);
  144. }
  145. else
  146. {
  147. parentElement.AppendChild(retElement);
  148. }
  149. return retElement;
  150. }
  151. private XmlElement NewElement(XmlElement parentElement, string nodeName)
  152. {
  153. return parentElement.OwnerDocument.CreateElement(nodeName, parentElement.NamespaceURI);
  154. }
  155. public System.Xml.XmlNode GetListAndView(string listName, string viewName)
  156. {
  157. throw new NotImplementedException();
  158. }
  159. public void DeleteList(string listName)
  160. {
  161. throw new NotImplementedException();
  162. }
  163. public System.Xml.XmlNode AddList(string listName, string description, int templateID)
  164. {
  165. throw new NotImplementedException();
  166. }
  167. public System.Xml.XmlNode AddListFromFeature(string listName, string description, Guid featureID, int templateID)
  168. {
  169. throw new NotImplementedException();
  170. }
  171. public System.Xml.XmlNode UpdateList(string listName, System.Xml.XmlNode listProperties, System.Xml.XmlNode newFields, System.Xml.XmlNode updateFields, System.Xml.XmlNode deleteFields, string listVersion)
  172. {
  173. throw new NotImplementedException();
  174. }
  175. public System.Xml.XmlNode GetListCollection()
  176. {
  177. throw new NotImplementedException();
  178. }
  179. public System.Xml.XmlNode GetListItems(string listName, string viewName, System.Xml.XmlNode query, System.Xml.XmlNode viewFields, string rowLimit, System.Xml.XmlNode queryOptions, string webID)
  180. {
  181. throw new NotImplementedException();
  182. }
  183. public System.Xml.XmlNode GetListItemChanges(string listName, System.Xml.XmlNode viewFields, string since, System.Xml.XmlNode contains)
  184. {
  185. throw new NotImplementedException();
  186. }
  187. public System.Xml.XmlNode GetListItemChangesSinceToken(string listName, string viewName, System.Xml.XmlNode query, System.Xml.XmlNode viewFields, string rowLimit, System.Xml.XmlNode queryOptions, string changeToken, System.Xml.XmlNode contains)
  188. {
  189. //List name should correspond to the provider ID if the stssync link was constructed correctly.
  190. const char sTab = '\t';
  191. System.Diagnostics.Debug.Print("GetListItemChangesSinceToken called with params:\n" +
  192. sTab + "listName = " + ((null == listName) ? string.Empty : listName).ToString() + "\n" +
  193. sTab + "viewName = " + ((null == viewName) ? string.Empty : viewName).ToString() +"\n" +
  194. sTab + "query = " + ((query == null) ? string.Empty:query.OuterXml.ToString()).ToString() + "\n" +
  195. sTab + "viewFields = " + ((null == viewFields) ? string.Empty:viewFields.OuterXml.ToString()).ToString() + "\n" +
  196. sTab + "rowLimit = " + ((null == rowLimit)?string.Empty:rowLimit.ToString()).ToString() + "\n" +
  197. sTab + "queryOptions = " + ((null == queryOptions)?string.Empty:queryOptions.OuterXml.ToString()).ToString() + "\n" +
  198. sTab + "changeToken = " + ((null == changeToken)?string.Empty:changeToken).ToString() + "\n" +
  199. sTab + "contains = " + ((contains == null)?string.Empty:contains.OuterXml.ToString()).ToString() + "\n=======================================");
  200. ProviderManager pm = new ProviderManager();
  201. Guid listId = new Guid(listName); //we'll just crash if it's not a Guid
  202. Provider prov = pm.GetProvider(listId);
  203. IProvider iProv = (IProvider)prov.GetIProvider();
  204. ChangeKey changeKey = null;
  205. if (changeToken != null) changeKey = new ChangeKey(changeToken);
  206. DataSet changes = iProv.GetUpdatesSinceToken(listId, changeKey);
  207. ListType listType = iProv.GetProviderType(listId);
  208. StringDictionary fieldMappings = iProv.GetFieldMappingsForListType(listId, listType);
  209. int intRowLimit = (null == rowLimit) ? 0 : Int32.Parse(rowLimit);
  210. int startRow = 0;
  211. string requestThread = null;
  212. if (queryOptions != null)
  213. {
  214. XmlElement xelPaging = (XmlElement)queryOptions.SelectSingleNode("//s:Paging", snsm);
  215. string sPosNext = (xelPaging != null) ? xelPaging.GetAttribute("ListItemCollectionPositionNext") : null;
  216. if (sPosNext != null && sPosNext.IndexOf("&") > -1)
  217. {
  218. string[] sPosNextParts = sPosNext.Split('&');
  219. requestThread = sPosNextParts[0];
  220. startRow = (sPosNext != null) ? Int32.Parse(sPosNextParts[1]) : 0;
  221. }
  222. }
  223. DataSet wssDS = new DataSet();
  224. try
  225. {
  226. object cacheValue = null;
  227. if (requestThread != null)
  228. {
  229. cacheValue = this.Context.Cache.Get(requestThread);
  230. }
  231. if (cacheValue != null)
  232. {
  233. wssDS = (DataSet)cacheValue;
  234. }
  235. else
  236. {
  237. requestThread = Guid.NewGuid().ToString("N");
  238. wssDS = DataSetMapper.MapCustomDSToWSSDS(changes, listType.ToString(), fieldMappings, listType);
  239. this.Context.Cache.Insert(requestThread, wssDS, null, DateTime.MaxValue, new TimeSpan(0, 10, 0));
  240. }
  241. }
  242. catch { }
  243. /*
  244. <listitems MinTimeBetweenSyncs='0' RecommendedTimeBetweenSyncs='180' MaxBulkDocumentSyncSize='500' AlternateUrls='http://initechwss/' EffectivePermMask='FullMask' xmlns:s='uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882'
  245. xmlns:dt='uuid:C2F41010-65B3-11d1-A29F-00AA00C14882'
  246. xmlns:rs='urn:schemas-microsoft-com:rowset'
  247. xmlns:z='#RowsetSchema'>
  248. */
  249. XmlElement listItems = _Doc.CreateElement("listitems",_nsm.DefaultNamespace);
  250. listItems.SetAttribute("MinTimeBetweenSyncs", "0");
  251. listItems.SetAttribute("RecommendedTimeBetweenSyncs", "180");
  252. listItems.SetAttribute("MaxBulkDocumentSyncSize", "500");
  253. listItems.SetAttribute("EffectivePermMask", "FullMask");
  254. listItems.SetAttribute("xmlns:s", "uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882");
  255. listItems.SetAttribute("xmlns:dt", "uuid:C2F41010-65B3-11d1-A29F-00AA00C14882");
  256. listItems.SetAttribute("xmlns:rs", "urn:schemas-microsoft-com:rowset");
  257. listItems.SetAttribute("xmlns:z", "#RowsetSchema");
  258. XmlElement xelChanges = NewElement(listItems, "Changes",null,true);
  259. xelChanges.SetAttribute("LastChangeToken", new ChangeKey(1, 3, prov.ID, DateTime.Now, 1).ToString());
  260. XmlElement xelDataNode = ListsHelper.DataSetToDataNode(listItems, wssDS, listType,intRowLimit,startRow,requestThread);
  261. if (xelDataNode.HasAttribute("ListItemCollectionPositionNext"))
  262. {
  263. xelChanges.SetAttribute("MoreChanges", "TRUE");
  264. }
  265. return listItems;
  266. }
  267. public System.Xml.XmlNode UpdateListItems(string listName, System.Xml.XmlNode updates)
  268. {
  269. XmlElement xelResults = _Doc.CreateElement("Results", _nsm.DefaultNamespace);
  270. ProviderManager pm = new ProviderManager();
  271. Guid listId = new Guid(listName); //we'll just crash if it's not a Guid
  272. Provider prov = pm.GetProvider(listId);
  273. IProvider iProv = (IProvider)prov.GetIProvider();
  274. ListType listType = iProv.GetProviderType(prov.ID);
  275. StringDictionary fieldListMapping = iProv.GetFieldMappingsForListType(prov.ID, listType);
  276. List<Field> fieldList= ListsHelper.GetFieldsForListType(listType);
  277. FieldDictionary fieldDictionary = ListsHelper.GetFieldDictionaryForListType(listType);
  278. XmlElement xelUpdates = (XmlElement)updates;
  279. string onError = xelUpdates.GetAttribute("OnError");
  280. string listVersion = xelUpdates.GetAttribute("ListVersion");
  281. string viewName = xelUpdates.GetAttribute("ViewName");
  282. XmlNodeList xnlMethods = xelUpdates.SelectNodes("//s:Method", snsm);
  283. DataSet emptyDS = iProv.GetEmptyDataSet(prov.ID);
  284. //DataSet wssDS = DataSetMapper.MapCustomDSToWSSDS(updatesDS, listType.ToString(), iProv.GetFieldMappingsForListType(prov.ID, listType), listType);
  285. DataTable emptyDT = emptyDS.Tables[0];
  286. XmlElement xelZRow = null;
  287. foreach (XmlElement xelMethod in xnlMethods)
  288. {
  289. XmlElement xelResult = NewElement(xelResults, "Result",null,true);
  290. string sMethodID = xelMethod.GetAttribute("ID");
  291. string sMethod = xelMethod.GetAttribute("Cmd");
  292. xelResult.SetAttribute("ID", sMethodID + "," + sMethod);
  293. uint errCode = 0;
  294. string errText = null;
  295. DataRow resultDR;
  296. try
  297. {
  298. XmlNodeList xnlFields = xelMethod.SelectNodes("s:Field[@Name != 'MetaInfo']",snsm);
  299. XmlElement xelMetaInfo = MetaInfoHelper.GetMetaInfoXml(xelMethod, snsm);
  300. XmlElement xelIDField = (XmlElement)xelMethod.SelectSingleNode("s:Field[@Name='ID']",snsm);
  301. int id = 0;
  302. string sID = xelIDField.InnerText;
  303. if (sID.ToLower() != "new")
  304. {
  305. try //to get the ID -- won't be there for the "New" method.
  306. {
  307. id = int.Parse(xelIDField.InnerText);
  308. }
  309. catch (FormatException ex)
  310. {
  311. id = 0;
  312. }
  313. }
  314. DataRow updateDR = null;
  315. switch (sMethod.ToLower())
  316. {
  317. case "update":
  318. case "new":
  319. if (sMethod.ToLower() == "update")
  320. {
  321. updateDR = iProv.GetSingleRow(prov.ID, id);
  322. }
  323. else //new
  324. {
  325. DataTable dt = iProv.GetEmptyDataSet(prov.ID).Tables[0];
  326. updateDR = dt.NewRow();
  327. dt.Rows.Add(updateDR);
  328. }
  329. if (updateDR != null)
  330. {
  331. foreach (XmlElement updateField in xnlFields)
  332. {
  333. string wssName = updateField.GetAttribute("Name");
  334. string fieldName = DataSetMapper.GetCustomFieldName(wssName, fieldListMapping);
  335. if (emptyDT.Columns.Contains(fieldName))
  336. {
  337. if (fieldDictionary[wssName].ReadOnly) continue; //don't update readonly fields
  338. Type typeOfColumn = emptyDT.Columns[fieldName].DataType;
  339. object fieldValue = DataSetMapper.GetTypedValue(typeOfColumn, updateField.InnerText);
  340. updateDR[fieldName] = fieldValue;
  341. }
  342. else
  343. {
  344. xelMetaInfo.AppendChild(xelMetaInfo.OwnerDocument.ImportNode(updateField,true));
  345. }
  346. }
  347. if (xelMetaInfo != null)
  348. {
  349. updateDR[MetaInfoHelper.GetMetaInfoColName(fieldListMapping)] = xelMetaInfo.OuterXml;
  350. }
  351. }
  352. resultDR = iProv.Update(prov.ID, updateDR);
  353. xelZRow = ListsHelper.CreateZRow(xelResult, fieldList, fieldListMapping, resultDR);
  354. if(xelMetaInfo != null){
  355. MetaInfoHelper.AddMetaInfoFromMetaInfoXml(xelZRow, xelMetaInfo);
  356. }
  357. xelResult.AppendChild(xelZRow);
  358. break;
  359. case "delete":
  360. updateDR = iProv.GetSingleRow(prov.ID, id);
  361. if (updateDR != null)
  362. {
  363. updateDR.Delete();
  364. iProv.Update(prov.ID, updateDR);
  365. }
  366. break;
  367. }
  368. }
  369. catch (ApplicationException ex)
  370. {
  371. errCode = 0x8007DA7E;
  372. errText = ex.Message;
  373. }
  374. XmlElement xelErrorCode = NewElement(xelResult, "ErrorCode",xelZRow,true);
  375. string sErrCode = errCode.ToString("X").PadLeft(8, '0');
  376. xelErrorCode.InnerText = "0x" + sErrCode;
  377. if (errCode > 0)
  378. {
  379. XmlElement xelErrorText = NewElement(xelResult, "ErrorText",errText);
  380. }
  381. }
  382. #if DEBUG
  383. string resultsFilePath = System.IO.Path.Combine(System.IO.Path.GetTempPath(),Guid.NewGuid().ToString("N") + ".xml");
  384. System.IO.File.WriteAllText(resultsFilePath,xelResults.OuterXml);
  385. System.Text.StringBuilder debugInfo = new System.Text.StringBuilder();
  386. debugInfo.Append("<html><body>");
  387. debugInfo.Append("<table>");
  388. debugInfo.Append("<tr><td>string listName</td><td>" + listName + "</td></tr>");
  389. debugInfo.Append("<tr><td>System.Xml.XmlNode updates</td><td>" + HttpUtility.HtmlEncode(updates.OuterXml) + "</td></tr>");
  390. debugInfo.Append("</table>");
  391. debugInfo.Append("<a href='" + resultsFilePath + "'>Results</a>");
  392. debugInfo.Append("</body></html>");
  393. System.IO.File.WriteAllText(System.IO.Path.Combine(System.IO.Path.GetTempPath(),Guid.NewGuid().ToString("N") + ".html"),debugInfo.ToString());
  394. #endif
  395. return xelResults;
  396. }
  397. public System.Xml.XmlNode AddDiscussionBoardItem(string listName, byte[] message)
  398. {
  399. throw new NotImplementedException();
  400. }
  401. public System.Xml.XmlNode GetVersionCollection(string strlistID, string strlistItemID, string strFieldName)
  402. {
  403. throw new NotImplementedException();
  404. }
  405. public string AddAttachment(string listName, string listItemID, string fileName, byte[] attachment)
  406. {
  407. throw new NotImplementedException();
  408. }
  409. public System.Xml.XmlNode GetAttachmentCollection(string listName, string listItemID)
  410. {
  411. throw new NotImplementedException();
  412. }
  413. public void DeleteAttachment(string listName, string listItemID, string url)
  414. {
  415. throw new NotImplementedException();
  416. }
  417. public bool CheckOutFile(string pageUrl, string checkoutToLocal, string lastmodified)
  418. {
  419. throw new NotImplementedException();
  420. }
  421. public bool UndoCheckOut(string pageUrl)
  422. {
  423. throw new NotImplementedException();
  424. }
  425. public bool CheckInFile(string pageUrl, string comment, string CheckinType)
  426. {
  427. throw new NotImplementedException();
  428. }
  429. public System.Xml.XmlNode GetListContentTypes(string listName, string contentTypeId)
  430. {
  431. throw new NotImplementedException();
  432. }
  433. public System.Xml.XmlNode GetListContentType(string listName, string contentTypeId)
  434. {
  435. throw new NotImplementedException();
  436. }
  437. public string CreateContentType(string listName, string displayName, string parentType, System.Xml.XmlNode fields, System.Xml.XmlNode contentTypeProperties, string addToView)
  438. {
  439. throw new NotImplementedException();
  440. }
  441. public System.Xml.XmlNode UpdateContentType(string listName, string contentTypeId, System.Xml.XmlNode contentTypeProperties, System.Xml.XmlNode newFields, System.Xml.XmlNode updateFields, System.Xml.XmlNode deleteFields, string addToView)
  442. {
  443. throw new NotImplementedException();
  444. }
  445. public System.Xml.XmlNode DeleteContentType(string listName, string contentTypeId)
  446. {
  447. throw new NotImplementedException();
  448. }
  449. public System.Xml.XmlNode UpdateContentTypeXmlDocument(string listName, string contentTypeId, System.Xml.XmlNode newDocument)
  450. {
  451. throw new NotImplementedException();
  452. }
  453. public System.Xml.XmlNode UpdateContentTypesXmlDocument(string listName, System.Xml.XmlNode newDocument)
  454. {
  455. throw new NotImplementedException();
  456. }
  457. public System.Xml.XmlNode DeleteContentTypeXmlDocument(string listName, string contentTypeId, string documentUri)
  458. {
  459. throw new NotImplementedException();
  460. }
  461. public System.Xml.XmlNode ApplyContentTypeToList(string webUrl, string contentTypeId, string listName)
  462. {
  463. throw new NotImplementedException();
  464. }
  465. #endregion
  466. }
  467. }