PageRenderTime 125ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/CodePlex/EFSPWFActivities/updateItemsByListview/updateItemsByListview.cs

#
C# | 281 lines | 221 code | 25 blank | 35 comment | 12 complexity | 26be90dda391cb822b27f9c359bce2bf MD5 | raw file
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Workflow.ComponentModel;
  8. using Microsoft.SharePoint;
  9. using Microsoft.SharePoint.Workflow;
  10. using Microsoft.SharePoint.WorkflowActions;
  11. namespace EFSPWFActivities
  12. {
  13. public class updateItemsByListview : Activity
  14. {
  15. public static DependencyProperty __ContextProperty = System.Workflow.ComponentModel.DependencyProperty.Register("__Context",
  16. typeof(WorkflowContext), typeof(updateItemsByListview));
  17. [Description("Context")]
  18. [Browsable(true)]
  19. [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
  20. public WorkflowContext __Context
  21. {
  22. get
  23. {
  24. return ((WorkflowContext)(base.GetValue(__ContextProperty)));
  25. }
  26. set
  27. {
  28. base.SetValue(__ContextProperty, value);
  29. }
  30. }
  31. //public static DependencyProperty __ListIdProperty = System.Workflow.ComponentModel.DependencyProperty.Register("__ListId",
  32. // typeof(string), typeof(updateItemsByListview));
  33. //[Description("ListId")]
  34. //[Browsable(true)]
  35. //[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
  36. //public string __ListId
  37. //{
  38. // get
  39. // {
  40. // return ((string)(base.GetValue(__ListIdProperty)));
  41. // }
  42. // set
  43. // {
  44. // base.SetValue(__ListIdProperty, value);
  45. // }
  46. //}
  47. //public static DependencyProperty __ListItemProperty = System.Workflow.ComponentModel.DependencyProperty.Register("__ListItem",
  48. // typeof(int), typeof(updateItemsByListview));
  49. //[Description("ListItem")]
  50. //[Browsable(true)]
  51. //[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
  52. //public int __ListItem
  53. //{
  54. // get
  55. // {
  56. // return ((int)(base.GetValue(__ListItemProperty)));
  57. // }
  58. // set
  59. // {
  60. // base.SetValue(__ListItemProperty, value);
  61. // }
  62. //}
  63. public static DependencyProperty ListTitleProperty = DependencyProperty.Register("ListTitle",
  64. typeof(string), typeof(updateItemsByListview));
  65. [Description("List Title")]
  66. [Category("EF Workflow Activities")]
  67. [Browsable(true)]
  68. [DesignerSerializationVisibility
  69. (DesignerSerializationVisibility.Visible)]
  70. public string ListTitle
  71. {
  72. get
  73. {
  74. return ((string)
  75. (base.GetValue(ListTitleProperty)));
  76. }
  77. set
  78. {
  79. base.SetValue(ListTitleProperty, value);
  80. }
  81. }
  82. public static DependencyProperty ListViewTitleProperty = DependencyProperty.Register("ListViewTitle",
  83. typeof(string), typeof(updateItemsByListview));
  84. [Description("ListView Title")]
  85. [Category("EF Workflow Activities")]
  86. [Browsable(true)]
  87. [DesignerSerializationVisibility
  88. (DesignerSerializationVisibility.Visible)]
  89. public string ListViewTitle
  90. {
  91. get
  92. {
  93. return ((string)
  94. (base.GetValue(ListViewTitleProperty)));
  95. }
  96. set
  97. {
  98. base.SetValue(ListViewTitleProperty, value);
  99. }
  100. }
  101. public static DependencyProperty FieldNameProperty = DependencyProperty.Register("FieldName",
  102. typeof(string), typeof(updateItemsByListview));
  103. [Description("Field Name")]
  104. [Category("EF Workflow Activities")]
  105. [Browsable(true)]
  106. [DesignerSerializationVisibility
  107. (DesignerSerializationVisibility.Visible)]
  108. public string FieldName
  109. {
  110. get
  111. {
  112. return ((string)
  113. (base.GetValue(FieldNameProperty)));
  114. }
  115. set
  116. {
  117. base.SetValue(FieldNameProperty, value);
  118. }
  119. }
  120. public static DependencyProperty FieldValueProperty = DependencyProperty.Register("FieldValue",
  121. typeof(string), typeof(updateItemsByListview));
  122. [Description("Field Value")]
  123. [Category("EF Workflow Activities")]
  124. [Browsable(true)]
  125. [DesignerSerializationVisibility
  126. (DesignerSerializationVisibility.Visible)]
  127. public string FieldValue
  128. {
  129. get
  130. {
  131. return ((string)
  132. (base.GetValue(FieldValueProperty)));
  133. }
  134. set
  135. {
  136. base.SetValue(FieldValueProperty, value);
  137. }
  138. }
  139. public static DependencyProperty NewVersionProperty = DependencyProperty.Register("NewVersion",
  140. typeof(string), typeof(updateItemsByListview));
  141. [Description("New Version (yes or no)")]
  142. [Category("EF Workflow Activities")]
  143. [Browsable(true)]
  144. [DesignerSerializationVisibility
  145. (DesignerSerializationVisibility.Visible)]
  146. public string NewVersion
  147. {
  148. get
  149. {
  150. return ((string)
  151. (base.GetValue(NewVersionProperty)));
  152. }
  153. set
  154. {
  155. base.SetValue(NewVersionProperty, value);
  156. }
  157. }
  158. public static void WriteDebugInfoToHistoryLog(SPWeb web, Guid workflow, string description)
  159. {
  160. #if DEBUG
  161. WriteInfoToHistoryLog(web, workflow, description);
  162. #endif
  163. }
  164. public static void WriteInfoToHistoryLog(SPWeb web, Guid workflow, string description)
  165. {
  166. TimeSpan ts = new TimeSpan();
  167. SPSecurity.RunWithElevatedPrivileges(delegate()
  168. {
  169. SPWorkflow.CreateHistoryEvent(web, workflow, 0, web.CurrentUser, ts, "updateItemsByListview", description, string.Empty);
  170. });
  171. }
  172. protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
  173. {
  174. SPSite objSPSite = (SPSite)__Context.Site;
  175. SPWeb objSPWeb = (SPWeb)__Context.Web;
  176. //SPField objSPField = null;
  177. WriteDebugInfoToHistoryLog(__Context.Web, __Context.WorkflowInstanceId, @"Site.ServerRelativeUrl=" + objSPSite.ServerRelativeUrl);
  178. WriteDebugInfoToHistoryLog(__Context.Web, __Context.WorkflowInstanceId, @"Web.ServerRelativeUrl=" + objSPWeb.ServerRelativeUrl);
  179. WriteDebugInfoToHistoryLog(__Context.Web, __Context.WorkflowInstanceId, @"ListTitle=" + ListTitle);
  180. WriteDebugInfoToHistoryLog(__Context.Web, __Context.WorkflowInstanceId, @"ListViewTitle=" + ListViewTitle);
  181. WriteDebugInfoToHistoryLog(__Context.Web, __Context.WorkflowInstanceId, @"FieldName=" + FieldName);
  182. WriteDebugInfoToHistoryLog(__Context.Web, __Context.WorkflowInstanceId, @"FieldValue=" + FieldValue);
  183. SPList objSPList = null;
  184. SPListItemCollection objSPListItemCollection = null;
  185. SPView objSPView = null;
  186. bool previousAllowUnsafeUpdates = false;
  187. try
  188. {
  189. objSPList = objSPWeb.Lists[new Guid(ListTitle)];
  190. if (objSPList == null)
  191. {
  192. WriteInfoToHistoryLog(__Context.Web, __Context.WorkflowInstanceId, @"list title(" + ListTitle + ") is invalid");
  193. return ActivityExecutionStatus.Faulting;
  194. }
  195. if (objSPList.Fields.ContainsField(FieldName) == false)
  196. {
  197. WriteInfoToHistoryLog(__Context.Web, __Context.WorkflowInstanceId, @"field name(" + FieldName + ") is invalid");
  198. return ActivityExecutionStatus.Faulting;
  199. }
  200. foreach (SPView item in objSPList.Views)
  201. {
  202. if (item.Title.Equals(ListViewTitle, StringComparison.InvariantCultureIgnoreCase))
  203. {
  204. objSPView = item;
  205. break;
  206. }
  207. }
  208. if (objSPView == null)
  209. {
  210. WriteInfoToHistoryLog(__Context.Web, __Context.WorkflowInstanceId, string.Format(@"View ({0}) doesn't exist in list({1}) under web {2}", ListViewTitle, ListTitle, objSPWeb.ServerRelativeUrl));
  211. return ActivityExecutionStatus.Faulting;
  212. }
  213. previousAllowUnsafeUpdates = objSPWeb.AllowUnsafeUpdates;
  214. if (previousAllowUnsafeUpdates == false)
  215. {
  216. objSPWeb.AllowUnsafeUpdates = true;
  217. }
  218. //objSPField = objSPList.Fields[FieldName];
  219. SPQuery objSPQuery = new SPQuery();
  220. objSPQuery.Query = objSPView.Query;
  221. objSPQuery.RowLimit = 0;
  222. objSPListItemCollection = objSPList.GetItems(objSPQuery);
  223. WriteDebugInfoToHistoryLog(__Context.Web, __Context.WorkflowInstanceId, string.Format(@"SPListItemCollection.Count={0}, NewVersion={1}", objSPListItemCollection.Count, NewVersion));
  224. foreach (SPListItem item in objSPListItemCollection)
  225. {
  226. item[FieldName] = FieldValue;
  227. if (NewVersion.Equals("no", StringComparison.InvariantCultureIgnoreCase))
  228. {
  229. //item.UpdateOverwriteVersion();
  230. item.SystemUpdate(false);
  231. }
  232. else
  233. {
  234. item.Update();
  235. }
  236. }
  237. }
  238. catch (Exception ex)
  239. {
  240. WriteInfoToHistoryLog(__Context.Web, __Context.WorkflowInstanceId, @"updateItemsByListview exception.");
  241. WriteInfoToHistoryLog(__Context.Web, __Context.WorkflowInstanceId, @"ex.Message=" + ex.Message);
  242. WriteInfoToHistoryLog(__Context.Web, __Context.WorkflowInstanceId, @"ex.StackTrace=" + ex.StackTrace);
  243. return ActivityExecutionStatus.Faulting;
  244. }
  245. finally
  246. {
  247. if (previousAllowUnsafeUpdates != objSPWeb.AllowUnsafeUpdates)
  248. {
  249. objSPWeb.AllowUnsafeUpdates = previousAllowUnsafeUpdates;
  250. }
  251. }
  252. WriteDebugInfoToHistoryLog(__Context.Web, __Context.WorkflowInstanceId, @"updateItemsByListview activity completed.");
  253. return ActivityExecutionStatus.Closed;
  254. }
  255. }
  256. }